From fd66120d6d13ff4719b8042e2cd6479741206124 Mon Sep 17 00:00:00 2001 From: Matt Krasowski <4535195+mkrasowski@users.noreply.github.com> Date: Sun, 29 Aug 2021 08:52:12 -0400 Subject: [PATCH] Handle incorrect values reported by some Shelly devices (#55042) --- homeassistant/components/shelly/binary_sensor.py | 6 ++++-- homeassistant/components/shelly/sensor.py | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/shelly/binary_sensor.py b/homeassistant/components/shelly/binary_sensor.py index 96d62152830..f4b2daf8159 100644 --- a/homeassistant/components/shelly/binary_sensor.py +++ b/homeassistant/components/shelly/binary_sensor.py @@ -1,7 +1,7 @@ """Binary sensor for Shelly.""" from __future__ import annotations -from typing import Final +from typing import Final, cast from homeassistant.components.binary_sensor import ( DEVICE_CLASS_CONNECTIVITY, @@ -46,7 +46,9 @@ SENSORS: Final = { name="Overpowering", device_class=DEVICE_CLASS_PROBLEM ), ("sensor", "dwIsOpened"): BlockAttributeDescription( - name="Door", device_class=DEVICE_CLASS_OPENING + name="Door", + device_class=DEVICE_CLASS_OPENING, + available=lambda block: cast(bool, block.dwIsOpened != -1), ), ("sensor", "flood"): BlockAttributeDescription( name="Flood", device_class=DEVICE_CLASS_MOISTURE diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 13cf56d3b3d..d8d530ed94c 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -40,6 +40,7 @@ SENSORS: Final = { device_class=sensor.DEVICE_CLASS_BATTERY, state_class=sensor.STATE_CLASS_MEASUREMENT, removal_condition=lambda settings, _: settings.get("external_power") == 1, + available=lambda block: cast(bool, block.battery != -1), ), ("device", "deviceTemp"): BlockAttributeDescription( name="Device Temperature", @@ -176,6 +177,7 @@ SENSORS: Final = { unit=LIGHT_LUX, device_class=sensor.DEVICE_CLASS_ILLUMINANCE, state_class=sensor.STATE_CLASS_MEASUREMENT, + available=lambda block: cast(bool, block.luminosity != -1), ), ("sensor", "tilt"): BlockAttributeDescription( name="Tilt",