Make energy attributes in switch optional

This commit is contained in:
Pierre Ståhl
2020-09-09 22:05:23 +02:00
parent 43a36f458c
commit aa1ed67f94

View File

@@ -94,10 +94,11 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
} }
) )
DPS_FIELDS = [ DPS_FIELDS = [
vol.Required(CONF_CURRENT), vol.Optional(CONF_CURRENT),
vol.Required(CONF_CURRENT_CONSUMPTION), vol.Optional(CONF_CURRENT_CONSUMPTION),
vol.Required(CONF_VOLTAGE), vol.Optional(CONF_VOLTAGE),
] ]
@@ -126,9 +127,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
TuyaCache(pytuyadevice), TuyaCache(pytuyadevice),
device_config[CONF_FRIENDLY_NAME], device_config[CONF_FRIENDLY_NAME],
device_config[CONF_ID], device_config[CONF_ID],
device_config[CONF_CURRENT], device_config.get(CONF_CURRENT),
device_config[CONF_CURRENT_CONSUMPTION], device_config.get(CONF_CURRENT_CONSUMPTION),
device_config[CONF_VOLTAGE], device_config.get(CONF_VOLTAGE),
) )
) )
@@ -265,20 +266,14 @@ class TuyaDevice(SwitchEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
attrs = {} attrs = {}
try: if self._attr_current:
attrs[ATTR_CURRENT] = "{}".format(self._status["dps"][self._attr_current]) attrs[ATTR_CURRENT] = self._status["dps"][self._attr_current]
attrs[ATTR_CURRENT_CONSUMPTION] = "{}".format( if self._attr_consumption:
attrs[ATTR_CURRENT_CONSUMPTION] = (
self._status["dps"][self._attr_consumption] / 10 self._status["dps"][self._attr_consumption] / 10
) )
attrs[ATTR_VOLTAGE] = "{}".format( if self._attr_voltage:
self._status["dps"][self._attr_voltage] / 10 attrs[ATTR_VOLTAGE] = self._status["dps"][self._attr_voltage] / 10
)
# print('attrs[ATTR_CURRENT]: [{}]'.format(attrs[ATTR_CURRENT]))
# print('attrs[ATTR_CURRENT_CONSUMPTION]: [{}]'.format(attrs[ATTR_CURRENT_CONSUMPTION]))
# print('attrs[ATTR_VOLTAGE]: [{}]'.format(attrs[ATTR_VOLTAGE]))
except KeyError:
pass
return attrs return attrs
def turn_on(self, **kwargs): def turn_on(self, **kwargs):