Pass config entry to LocalTuyaEntity
This commit is contained in:
committed by
rospogrigio
parent
bd98b7cd93
commit
9973c5fdcb
@@ -92,18 +92,22 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def get_entity_config(config_entry, dps_id):
|
||||||
|
"""Return entity config for a given DPS id."""
|
||||||
|
for entity in config_entry.data[CONF_ENTITIES]:
|
||||||
|
if entity[CONF_ID] == dps_id:
|
||||||
|
return entity
|
||||||
|
raise Exception(f"missing entity config for id {dps_id}")
|
||||||
|
|
||||||
|
|
||||||
class LocalTuyaEntity(Entity):
|
class LocalTuyaEntity(Entity):
|
||||||
"""Representation of a Tuya entity."""
|
"""Representation of a Tuya entity."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, device, config_entry, dps_id, **kwargs):
|
||||||
self,
|
|
||||||
device,
|
|
||||||
friendly_name,
|
|
||||||
dps_id,
|
|
||||||
):
|
|
||||||
"""Initialize the Tuya entity."""
|
"""Initialize the Tuya entity."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._name = friendly_name
|
self._config_entry = config_entry
|
||||||
|
self._config = get_entity_config(config_entry, dps_id)
|
||||||
self._available = False
|
self._available = False
|
||||||
self._dps_id = dps_id
|
self._dps_id = dps_id
|
||||||
self._status = None
|
self._status = None
|
||||||
@@ -111,7 +115,7 @@ class LocalTuyaEntity(Entity):
|
|||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Get name of Tuya entity."""
|
"""Get name of Tuya entity."""
|
||||||
return self._name
|
return self._config[CONF_FRIENDLY_NAME]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
|
@@ -104,11 +104,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
switches.append(
|
switches.append(
|
||||||
LocaltuyaSwitch(
|
LocaltuyaSwitch(
|
||||||
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]),
|
||||||
device_config[CONF_FRIENDLY_NAME],
|
config_entry,
|
||||||
device_config[CONF_ID],
|
device_config[CONF_ID],
|
||||||
device_config.get(CONF_CURRENT),
|
|
||||||
device_config.get(CONF_CURRENT_CONSUMPTION),
|
|
||||||
device_config.get(CONF_VOLTAGE),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -194,21 +191,16 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
device,
|
device,
|
||||||
friendly_name,
|
config_entry,
|
||||||
switchid,
|
switchid,
|
||||||
attr_current,
|
**kwargs,
|
||||||
attr_consumption,
|
|
||||||
attr_voltage,
|
|
||||||
):
|
):
|
||||||
"""Initialize the Tuya switch."""
|
"""Initialize the Tuya switch."""
|
||||||
super().__init__(device, friendly_name, switchid)
|
super().__init__(device, config_entry, switchid, **kwargs)
|
||||||
self._attr_current = attr_current
|
|
||||||
self._attr_consumption = attr_consumption
|
|
||||||
self._attr_voltage = attr_voltage
|
|
||||||
self._state = None
|
self._state = None
|
||||||
print(
|
print(
|
||||||
"Initialized tuya switch [{}] with switch status [{}] and state [{}]".format(
|
"Initialized tuya switch [{}] with switch status [{}] and state [{}]".format(
|
||||||
self._name, self._status, self._state
|
self.name, self._status, self._state
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -225,6 +217,7 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
|
|||||||
"sw_version": "3.3",
|
"sw_version": "3.3",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Check if Tuya switch is on."""
|
"""Check if Tuya switch is on."""
|
||||||
return self._state
|
return self._state
|
||||||
@@ -232,12 +225,14 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
attrs = {}
|
attrs = {}
|
||||||
if self._attr_current:
|
if CONF_CURRENT in self._config:
|
||||||
attrs[ATTR_CURRENT] = self.dps(self._attr_current)
|
attrs[ATTR_CURRENT] = self.dps(self._config[CONF_CURRENT])
|
||||||
if self._attr_consumption:
|
if CONF_CURRENT_CONSUMPTION in self._config:
|
||||||
attrs[ATTR_CURRENT_CONSUMPTION] = self.dps(self._attr_consumption) / 10
|
attrs[ATTR_CURRENT_CONSUMPTION] = (
|
||||||
if self._attr_voltage:
|
self.dps(self._config[CONF_CURRENT_CONSUMPTION]) / 10
|
||||||
attrs[ATTR_VOLTAGE] = self.dps(self._attr_voltage) / 10
|
)
|
||||||
|
if CONF_VOLTAGE in self._config:
|
||||||
|
attrs[ATTR_VOLTAGE] = self.dps(self._config[CONF_VOLTAGE]) / 10
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
|
Reference in New Issue
Block a user