From 7e50a4999c85d6208f723ac12c876e3501dfd246 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Thu, 3 Sep 2020 10:54:25 +0200 Subject: [PATCH] Add support for Shelly Gas to the Shelly integration (#39478) Co-authored-by: Paulus Schoutsen --- .../components/shelly/binary_sensor.py | 24 ++++++++++++++++++- homeassistant/components/shelly/sensor.py | 11 +++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/shelly/binary_sensor.py b/homeassistant/components/shelly/binary_sensor.py index 52a752a64f9..80e935168f0 100644 --- a/homeassistant/components/shelly/binary_sensor.py +++ b/homeassistant/components/shelly/binary_sensor.py @@ -2,6 +2,7 @@ import aioshelly from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_GAS, DEVICE_CLASS_MOISTURE, DEVICE_CLASS_OPENING, DEVICE_CLASS_SMOKE, @@ -15,6 +16,7 @@ from .const import DOMAIN SENSORS = { "dwIsOpened": DEVICE_CLASS_OPENING, "flood": DEVICE_CLASS_MOISTURE, + "gas": DEVICE_CLASS_GAS, "overpower": None, "overtemp": None, "smoke": DEVICE_CLASS_SMOKE, @@ -66,10 +68,30 @@ class ShellySensor(ShellyBlockEntity, BinarySensorEntity): @property def is_on(self): - """Return true if sensor state is 1.""" + """Return true if sensor state is on.""" + if self.attribute == "gas": + # Gas sensor value of Shelly Gas can be none/mild/heavy/test. We return True + # when the value is mild or heavy. + return getattr(self.block, self.attribute) in ["mild", "heavy"] return bool(getattr(self.block, self.attribute)) @property def device_class(self): """Device class of sensor.""" return self._device_class + + @property + def device_state_attributes(self): + """Return the state attributes.""" + if self.attribute == "gas": + # We return raw value of the gas sensor as an attribute. + return {"detected": getattr(self.block, self.attribute)} + + @property + def available(self): + """Available.""" + if self.attribute == "gas": + # "sensorOp" is "normal" when Shelly Gas is working properly and taking + # measurements. + return super().available and self.block.sensorOp == "normal" + return super().available diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 953499762a0..cfeaea98357 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -3,6 +3,7 @@ import aioshelly from homeassistant.components import sensor from homeassistant.const import ( + CONCENTRATION_PARTS_PER_MILLION, ELECTRICAL_CURRENT_AMPERE, ENERGY_KILO_WATT_HOUR, POWER_WATT, @@ -18,6 +19,7 @@ from .const import DOMAIN SENSORS = { "battery": [UNIT_PERCENTAGE, sensor.DEVICE_CLASS_BATTERY], + "concentration": [CONCENTRATION_PARTS_PER_MILLION, None], "current": [ELECTRICAL_CURRENT_AMPERE, sensor.DEVICE_CLASS_CURRENT], "deviceTemp": [None, sensor.DEVICE_CLASS_TEMPERATURE], "energy": [ENERGY_KILO_WATT_HOUR, sensor.DEVICE_CLASS_ENERGY], @@ -117,3 +119,12 @@ class ShellySensor(ShellyBlockEntity, Entity): def device_class(self): """Device class of sensor.""" return self._device_class + + @property + def available(self): + """Available.""" + if self.attribute == "concentration": + # "sensorOp" is "normal" when the Shelly Gas is working properly and taking + # measurements. + return super().available and self.block.sensorOp == "normal" + return super().available