Soma cover battery level attribute (#44459)

pull/44597/head
badguy99 2020-12-28 16:10:49 +00:00 committed by GitHub
parent 50e11773ee
commit d95696e4f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 2 deletions

View File

@ -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/*

View File

@ -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

View File

@ -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