Skip to content

Commit

Permalink
Try to fix sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
azerty9971 committed Jan 26, 2025
1 parent 8a61d5c commit 059c821
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@
import custom_components.tuya as tuya_integration # type: ignore
except ImportError:
import homeassistant.components.tuya as tuya_integration # noqa: F401
try:
from custom_components.tuya.const import (
DPCode as TuyaDPCode,
DPType as TuyaDPType,
)
except ImportError:
from homeassistant.components.tuya.const import (
DPCode as TuyaDPCode,
DPType as TuyaDPType,
)
try:
from custom_components.tuya.entity import ( # type: ignore
TuyaEntity as TuyaEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
TuyaVacuumEntity as TuyaVacuumEntity,
)
import homeassistant.components.tuya as tuya_integration # noqa: F401
from homeassistant.components.tuya.const import (
DPCode as TuyaDPCode,
DPType as TuyaDPType,
)
from homeassistant.components.tuya.entity import (
TuyaEntity as TuyaEntity,
ElectricityTypeData as TuyaElectricityTypeData,
Expand Down
30 changes: 29 additions & 1 deletion custom_components/xtend_tuya/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .const import (
TUYA_DISCOVERY_NEW,
DPCode,
DPType,
VirtualStates, # noqa: F401
)
from .entity import (
Expand All @@ -45,6 +46,7 @@
from .ha_tuya_integration.tuya_integration_imports import (
TuyaSensorEntity,
TuyaSensorEntityDescription,
TuyaDPCode,
)

@dataclass(frozen=True)
Expand Down Expand Up @@ -921,10 +923,36 @@ def __init__(
description: XTSensorEntityDescription,
) -> None:
"""Init XT sensor."""
super(XTSensorEntity, self).__init__(device, device_manager, description)
description_copy = description
need_adjust = False
try:
#Verify if the DPCode is in Tuya's DPCode table, otherwise the init of Tuya will fail
TuyaDPCode(description.key)
except Exception:
need_adjust = True
description_copy = XTSensorEntityDescription(key=None)
super(XTSensorEntity, self).__init__(device, device_manager, description_copy)
self.device = device
self.device_manager = device_manager
self.entity_description = description
if need_adjust:
#If DPCode was not in Tuya's DPCode table then fix the initialization
self._attr_unique_id = (
f"{super().unique_id}{description.key}{description.subkey or ''}"
)

if int_type := self.find_dpcode(description.key, dptype=DPType.INTEGER):
self._type_data = int_type
self._type = DPType.INTEGER
if description.native_unit_of_measurement is None:
self._attr_native_unit_of_measurement = int_type.unit
elif enum_type := self.find_dpcode(
description.key, dptype=DPType.ENUM, prefer_function=True
):
self._type_data = enum_type
self._type = DPType.ENUM
else:
self._type = self.get_dptype(DPCode(description.key))

def reset_value(self, _: datetime, manual_call: bool = False) -> None:
if manual_call and self.cancel_reset_after_x_seconds:
Expand Down

0 comments on commit 059c821

Please sign in to comment.