Reduce lagging update

This commit is contained in:
Andre Basche 2023-06-08 20:01:55 +02:00
parent c433714a94
commit 80b3741f2f
10 changed files with 57 additions and 24 deletions

View file

@ -62,6 +62,7 @@ Translation of internal names like programs are available for all languages whic
## Supported Models
Support has been confirmed for these models, but many more will work. Please add already supported devices [with this form to complete the list](https://forms.gle/bTSD8qFotdZFytbf8).
- Haier AD105S2SM3FA
- Haier AS20HPL1HRA
- Haier AS25PBAHRA
- Haier AS25S2SF1FA-WH
- Haier AS25TADHRA-2

View file

@ -271,9 +271,10 @@ class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
)
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
self._attr_native_value = (
self._device.get(self.entity_description.key, "")
== self.entity_description.on_value
)
if update:
self.async_write_ha_state()

View file

@ -7,6 +7,7 @@ from homeassistant.components.climate import (
)
DOMAIN = "hon"
UPDATE_INTERVAL = 10
PLATFORMS = [
"sensor",

View file

@ -1,12 +1,13 @@
import logging
from datetime import timedelta
from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from pyhon.appliance import HonAppliance
from .const import DOMAIN
from .const import DOMAIN, UPDATE_INTERVAL
_LOGGER = logging.getLogger(__name__)
@ -21,13 +22,14 @@ class HonEntity(CoordinatorEntity):
self._hon = hass.data[DOMAIN][entry.unique_id]
self._hass = hass
self._coordinator = coordinator
self._device = device
self._device: HonAppliance = device
if description is not None:
self.entity_description = description
self._attr_unique_id = f"{self._device.unique_id}{description.key}"
else:
self._attr_unique_id = self._device.unique_id
self._handle_coordinator_update(update=False)
@property
def device_info(self):
@ -41,6 +43,11 @@ class HonEntity(CoordinatorEntity):
sw_version=self._device.get("fwVersion", ""),
)
@callback
def _handle_coordinator_update(self, update: bool = True) -> None:
if update:
self.async_write_ha_state()
class HonCoordinator(DataUpdateCoordinator):
def __init__(self, hass, device: HonAppliance):
@ -49,7 +56,7 @@ class HonCoordinator(DataUpdateCoordinator):
hass,
_LOGGER,
name=device.unique_id,
update_interval=timedelta(seconds=30),
update_interval=timedelta(seconds=UPDATE_INTERVAL),
)
self._device = device

View file

@ -9,7 +9,7 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/Andre0512/hon/issues",
"requirements": [
"pyhOn==0.12.1"
"pyhOn==0.12.2"
],
"version": "0.8.0-beta.9"
"version": "0.8.0-beta.10"
}

View file

@ -9,7 +9,7 @@ from homeassistant.components.number import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTime, UnitOfTemperature
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory, Entity
from homeassistant.helpers.entity import EntityCategory
from pyhon.parameter.range import HonParameterRange
from .const import DOMAIN
@ -223,13 +223,14 @@ class HonNumberEntity(HonEntity, NumberEntity):
await self.coordinator.async_refresh()
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
setting = self._device.settings[self.entity_description.key]
if isinstance(setting, HonParameterRange):
self._attr_native_max_value = setting.max
self._attr_native_min_value = setting.min
self._attr_native_step = setting.step
self._attr_native_value = setting.value
if update:
self.async_write_ha_state()
@property

View file

@ -7,7 +7,7 @@ from homeassistant.components.select import SelectEntity, SelectEntityDescriptio
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory, Entity
from homeassistant.helpers.entity import EntityCategory
from pyhon.appliance import HonAppliance
from pyhon.parameter.fixed import HonParameterFixed
@ -179,7 +179,7 @@ class HonSelectEntity(HonEntity, SelectEntity):
await self.coordinator.async_refresh()
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
setting = self._device.settings.get(self.entity_description.key)
if setting is None:
self._attr_available = False
@ -189,6 +189,7 @@ class HonSelectEntity(HonEntity, SelectEntity):
self._attr_available = True
self._attr_options: list[str] = setting.values
self._attr_native_value = setting.value
if update:
self.async_write_ha_state()
@property

View file

@ -1,8 +1,6 @@
import logging
from dataclasses import dataclass
from pyhon.appliance import HonAppliance
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
@ -22,7 +20,8 @@ from homeassistant.const import (
)
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.typing import StateType
from pyhon.appliance import HonAppliance
from . import const
from .const import DOMAIN
from .hon import HonEntity, unique_entities
@ -635,11 +634,12 @@ class HonSensorEntity(HonEntity, SensorEntity):
).values + ["No Program"]
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
value = self._device.get(self.entity_description.key, "")
if not value and self.entity_description.state_class is not None:
self._attr_native_value = 0
self._attr_native_value = value
if update:
self.async_write_ha_state()
@ -647,7 +647,7 @@ class HonConfigSensorEntity(HonEntity, SensorEntity):
entity_description: HonConfigSensorEntityDescription
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
value = self._device.settings.get(self.entity_description.key, None)
if self.entity_description.state_class is not None:
if value and value.value:
@ -658,4 +658,5 @@ class HonConfigSensorEntity(HonEntity, SensorEntity):
self._attr_native_value = 0
else:
self._attr_native_value = value.value
if update:
self.async_write_ha_state()

View file

@ -1,5 +1,6 @@
import logging
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Any
from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity
@ -394,9 +395,10 @@ class HonSwitchEntity(HonEntity, SwitchEntity):
)
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
value = self._device.get(self.entity_description.key, "0")
self._attr_state = value == "1"
if update:
self.async_write_ha_state()
@ -410,9 +412,13 @@ class HonControlSwitchEntity(HonEntity, SwitchEntity):
async def async_turn_on(self, **kwargs: Any) -> None:
await self._device.commands[self.entity_description.turn_on_key].send()
self._device.attributes[self.entity_description.key] = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
await self._device.commands[self.entity_description.turn_off_key].send()
self._device.attributes[self.entity_description.key] = False
self.async_write_ha_state()
@property
def available(self) -> bool:
@ -423,6 +429,18 @@ class HonControlSwitchEntity(HonEntity, SwitchEntity):
and self._device.get("attributes.lastConnEvent.category") != "DISCONNECTED"
)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the optional state attributes."""
result = {}
if remaining_time := int(self._device.get("remainingTimeMM", 0)):
delay_time = int(self._device.get("delayTime", 0))
result["start_time"] = datetime.now() + timedelta(minutes=delay_time)
result["end_time"] = datetime.now() + timedelta(
minutes=delay_time + remaining_time
)
return result
class HonConfigSwitchEntity(HonEntity, SwitchEntity):
entity_description: HonConfigSwitchEntityDescription
@ -454,7 +472,8 @@ class HonConfigSwitchEntity(HonEntity, SwitchEntity):
await self.coordinator.async_refresh()
@callback
def _handle_coordinator_update(self):
def _handle_coordinator_update(self, update=True) -> None:
value = self._device.settings.get(self.entity_description.key, "0")
self._attr_state = value == "1"
if update:
self.async_write_ha_state()

View file

@ -51,6 +51,7 @@ Translation of internal names like programs are available for all languages whic
## Supported Models
Support has been confirmed for these models, but many more will work. Please add already supported devices [with this form to complete the list](https://forms.gle/bTSD8qFotdZFytbf8).
- Haier AD105S2SM3FA
- Haier AS20HPL1HRA
- Haier AS25PBAHRA
- Haier AS25S2SF1FA-WH
- Haier AS25TADHRA-2