Soma: fix battery drain issue caused by excess update requests (#45104)
* split up update and throttle update on sensor * Update imports * Add blank lines for isortpull/45114/head
parent
6325bc8bfe
commit
b9e4d5988f
|
@ -1,9 +1,7 @@
|
|||
"""Support for Soma Smartshades."""
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from api.soma_api import SomaApi
|
||||
from requests import RequestException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
|
@ -17,8 +15,6 @@ from .const import API, DOMAIN, HOST, PORT
|
|||
|
||||
DEVICES = "devices"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
|
@ -113,43 +109,3 @@ class SomaEntity(Entity):
|
|||
"name": self.name,
|
||||
"manufacturer": "Wazombi Labs",
|
||||
}
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the device with the latest data."""
|
||||
try:
|
||||
response = await self.hass.async_add_executor_job(
|
||||
self.api.get_shade_state, 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
|
||||
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
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
import logging
|
||||
|
||||
from requests import RequestException
|
||||
|
||||
from homeassistant.components.cover import ATTR_POSITION, CoverEntity
|
||||
from homeassistant.components.soma import API, DEVICES, DOMAIN, SomaEntity
|
||||
|
||||
|
@ -67,3 +69,23 @@ class SomaCover(SomaEntity, CoverEntity):
|
|||
def is_closed(self):
|
||||
"""Return if the cover is closed."""
|
||||
return self.current_position == 0
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the cover with the latest data."""
|
||||
try:
|
||||
_LOGGER.debug("Soma Cover Update")
|
||||
response = await self.hass.async_add_executor_job(
|
||||
self.api.get_shade_state, 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
|
||||
self.current_position = 100 - response["position"]
|
||||
self.is_available = True
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
"""Support for Soma sensors."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from requests import RequestException
|
||||
|
||||
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
from . import DEVICES, SomaEntity
|
||||
from .const import API, DOMAIN
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up the Soma sensor platform."""
|
||||
|
@ -38,3 +48,30 @@ class SomaSensor(SomaEntity, Entity):
|
|||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement this sensor expresses itself in."""
|
||||
return PERCENTAGE
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
async def async_update(self):
|
||||
"""Update the sensor with the latest data."""
|
||||
try:
|
||||
_LOGGER.debug("Soma Sensor Update")
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue