diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index 1eef42f..a72899b 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -92,18 +92,22 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): 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): """Representation of a Tuya entity.""" - def __init__( - self, - device, - friendly_name, - dps_id, - ): + def __init__(self, device, config_entry, dps_id, **kwargs): """Initialize the Tuya entity.""" 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._dps_id = dps_id self._status = None @@ -111,7 +115,7 @@ class LocalTuyaEntity(Entity): @property def name(self): """Get name of Tuya entity.""" - return self._name + return self._config[CONF_FRIENDLY_NAME] @property def unique_id(self): diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index b6298b8..0f157c7 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -104,11 +104,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities): switches.append( LocaltuyaSwitch( TuyaCache(device, config_entry.data[CONF_FRIENDLY_NAME]), - device_config[CONF_FRIENDLY_NAME], + config_entry, 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__( self, device, - friendly_name, + config_entry, switchid, - attr_current, - attr_consumption, - attr_voltage, + **kwargs, ): """Initialize the Tuya switch.""" - super().__init__(device, friendly_name, switchid) - self._attr_current = attr_current - self._attr_consumption = attr_consumption - self._attr_voltage = attr_voltage + super().__init__(device, config_entry, switchid, **kwargs) self._state = None print( "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", } + @property def is_on(self): """Check if Tuya switch is on.""" return self._state @@ -232,12 +225,14 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity): @property def device_state_attributes(self): attrs = {} - if self._attr_current: - attrs[ATTR_CURRENT] = self.dps(self._attr_current) - if self._attr_consumption: - attrs[ATTR_CURRENT_CONSUMPTION] = self.dps(self._attr_consumption) / 10 - if self._attr_voltage: - attrs[ATTR_VOLTAGE] = self.dps(self._attr_voltage) / 10 + if CONF_CURRENT in self._config: + attrs[ATTR_CURRENT] = self.dps(self._config[CONF_CURRENT]) + if CONF_CURRENT_CONSUMPTION in self._config: + attrs[ATTR_CURRENT_CONSUMPTION] = ( + self.dps(self._config[CONF_CURRENT_CONSUMPTION]) / 10 + ) + if CONF_VOLTAGE in self._config: + attrs[ATTR_VOLTAGE] = self.dps(self._config[CONF_VOLTAGE]) / 10 return attrs def turn_on(self, **kwargs):