From e673340229a1148996075b6f3c493c24ffaceb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20St=C3=A5hl?= Date: Mon, 21 Sep 2020 09:30:54 +0200 Subject: [PATCH] Fix missing attributes bug --- custom_components/localtuya/common.py | 5 +++++ custom_components/localtuya/switch.py | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index ea3bdd0..86d156a 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -184,6 +184,11 @@ class LocalTuyaEntity(Entity): """Return unique device identifier.""" return f"local_{self._device.unique_id}_{self._dps_id}" + def has_config(self, attr): + """Return if a config parameter has a valid value.""" + value = self._config.get(attr, "-1") + return value is not None and value != "-1" + @property def available(self): """Return if device is available or not.""" diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index e9cb01d..7c88584 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -80,13 +80,13 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity): def device_state_attributes(self): """Return device state attributes.""" attrs = {} - if self._config.get(CONF_CURRENT, "-1") != "-1": + if self.has_config(CONF_CURRENT): attrs[ATTR_CURRENT] = self.dps(self._config[CONF_CURRENT]) - if self._config.get(ATTR_CURRENT_CONSUMPTION, "-1") != "-1": + if self.has_config(CONF_CURRENT_CONSUMPTION): attrs[ATTR_CURRENT_CONSUMPTION] = ( self.dps(self._config[CONF_CURRENT_CONSUMPTION]) / 10 ) - if self._config.get(CONF_VOLTAGE, "-1") != "-1": + if self.has_config(CONF_VOLTAGE): attrs[ATTR_VOLTAGE] = self.dps(self._config[CONF_VOLTAGE]) / 10 return attrs