mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
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 isort
This commit is contained in:
parent
6325bc8bfe
commit
b9e4d5988f
@ -1,9 +1,7 @@
|
|||||||
"""Support for Soma Smartshades."""
|
"""Support for Soma Smartshades."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
|
|
||||||
from api.soma_api import SomaApi
|
from api.soma_api import SomaApi
|
||||||
from requests import RequestException
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
@ -17,8 +15,6 @@ from .const import API, DOMAIN, HOST, PORT
|
|||||||
|
|
||||||
DEVICES = "devices"
|
DEVICES = "devices"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
DOMAIN: vol.Schema(
|
DOMAIN: vol.Schema(
|
||||||
@ -113,43 +109,3 @@ class SomaEntity(Entity):
|
|||||||
"name": self.name,
|
"name": self.name,
|
||||||
"manufacturer": "Wazombi Labs",
|
"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
|
import logging
|
||||||
|
|
||||||
|
from requests import RequestException
|
||||||
|
|
||||||
from homeassistant.components.cover import ATTR_POSITION, CoverEntity
|
from homeassistant.components.cover import ATTR_POSITION, CoverEntity
|
||||||
from homeassistant.components.soma import API, DEVICES, DOMAIN, SomaEntity
|
from homeassistant.components.soma import API, DEVICES, DOMAIN, SomaEntity
|
||||||
|
|
||||||
@ -67,3 +69,23 @@ class SomaCover(SomaEntity, CoverEntity):
|
|||||||
def is_closed(self):
|
def is_closed(self):
|
||||||
"""Return if the cover is closed."""
|
"""Return if the cover is closed."""
|
||||||
return self.current_position == 0
|
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."""
|
"""Support for Soma sensors."""
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from requests import RequestException
|
||||||
|
|
||||||
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
|
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
from . import DEVICES, SomaEntity
|
from . import DEVICES, SomaEntity
|
||||||
from .const import API, DOMAIN
|
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):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up the Soma sensor platform."""
|
"""Set up the Soma sensor platform."""
|
||||||
@ -38,3 +48,30 @@ class SomaSensor(SomaEntity, Entity):
|
|||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit of measurement this sensor expresses itself in."""
|
"""Return the unit of measurement this sensor expresses itself in."""
|
||||||
return PERCENTAGE
|
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…
x
Reference in New Issue
Block a user