diff --git a/.coveragerc b/.coveragerc index 64a22ef275f..c16b6ecb986 100644 --- a/.coveragerc +++ b/.coveragerc @@ -828,8 +828,9 @@ omit = homeassistant/components/solaredge_local/sensor.py homeassistant/components/solarlog/* homeassistant/components/solax/sensor.py - homeassistant/components/soma/cover.py homeassistant/components/soma/__init__.py + homeassistant/components/soma/cover.py + homeassistant/components/soma/sensor.py homeassistant/components/somfy/* homeassistant/components/somfy_mylink/* homeassistant/components/sonos/* diff --git a/homeassistant/components/soma/__init__.py b/homeassistant/components/soma/__init__.py index 93ee4fc9b8f..3439684f977 100644 --- a/homeassistant/components/soma/__init__.py +++ b/homeassistant/components/soma/__init__.py @@ -27,7 +27,7 @@ CONFIG_SCHEMA = vol.Schema( extra=vol.ALLOW_EXTRA, ) -SOMA_COMPONENTS = ["cover"] +SOMA_COMPONENTS = ["cover", "sensor"] async def async_setup(hass, config): @@ -74,6 +74,7 @@ class SomaEntity(Entity): self.device = device self.api = api self.current_position = 50 + self.battery_state = 0 self.is_available = True @property @@ -120,4 +121,25 @@ class SomaEntity(Entity): self.is_available = False return self.current_position = 100 - response["position"] + try: + response = await self.hass.async_add_executor_job( + self.api.get_battery_level, self.device["mac"] + ) + except RequestException: + _LOGGER.error("Connection to SOMA Connect failed") + self.is_available = False + return + if response["result"] != "success": + _LOGGER.error( + "Unable to reach device %s (%s)", self.device["name"], response["msg"] + ) + self.is_available = False + return + # https://support.somasmarthome.com/hc/en-us/articles/360026064234-HTTP-API + # battery_level response is expected to be min = 360, max 410 for + # 0-100% levels above 410 are consider 100% and below 360, 0% as the + # device considers 360 the minimum to move the motor. + _battery = round(2 * (response["battery_level"] - 360)) + battery = max(min(100, _battery), 0) + self.battery_state = battery self.is_available = True diff --git a/homeassistant/components/soma/sensor.py b/homeassistant/components/soma/sensor.py new file mode 100644 index 00000000000..2d37a0b0dce --- /dev/null +++ b/homeassistant/components/soma/sensor.py @@ -0,0 +1,40 @@ +"""Support for Soma sensors.""" +from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE +from homeassistant.helpers.entity import Entity + +from . import DEVICES, SomaEntity +from .const import API, DOMAIN + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the Soma sensor platform.""" + + devices = hass.data[DOMAIN][DEVICES] + + async_add_entities( + [SomaSensor(sensor, hass.data[DOMAIN][API]) for sensor in devices], True + ) + + +class SomaSensor(SomaEntity, Entity): + """Representation of a Soma cover device.""" + + @property + def device_class(self): + """Return the class of this device, from component DEVICE_CLASSES.""" + return DEVICE_CLASS_BATTERY + + @property + def name(self): + """Return the name of the device.""" + return self.device["name"] + " battery level" + + @property + def state(self): + """Return the state of the entity.""" + return self.battery_state + + @property + def unit_of_measurement(self): + """Return the unit of measurement this sensor expresses itself in.""" + return PERCENTAGE