Handle missing values in Shelly sensors (#39515)

This commit is contained in:
On Freund 2020-09-01 15:04:42 +03:00 committed by GitHub
parent b541abc551
commit dde0dab3db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,6 +88,10 @@ class ShellySensor(ShellyBlockEntity, Entity):
@property @property
def state(self): def state(self):
"""Value of sensor.""" """Value of sensor."""
value = getattr(self.block, self.attribute)
if value is None:
return None
if self.attribute in [ if self.attribute in [
"deviceTemp", "deviceTemp",
"extTemp", "extTemp",
@ -95,13 +99,13 @@ class ShellySensor(ShellyBlockEntity, Entity):
"overpowerValue", "overpowerValue",
"power", "power",
]: ]:
return round(getattr(self.block, self.attribute), 1) return round(value, 1)
# Energy unit change from Wmin or Wh to kWh # Energy unit change from Wmin or Wh to kWh
if self.info[aioshelly.BLOCK_VALUE_UNIT] == "Wmin": if self.info[aioshelly.BLOCK_VALUE_UNIT] == "Wmin":
return round(getattr(self.block, self.attribute) / 60 / 1000, 2) return round(value / 60 / 1000, 2)
if self.info[aioshelly.BLOCK_VALUE_UNIT] == "Wh": if self.info[aioshelly.BLOCK_VALUE_UNIT] == "Wh":
return round(getattr(self.block, self.attribute) / 1000, 2) return round(value / 1000, 2)
return getattr(self.block, self.attribute) return value
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):