mirror of
https://github.com/Andre0512/hon.git
synced 2025-08-03 23:31:15 +00:00
fix scheduled update
This commit is contained in:
parent
70eb6c0111
commit
1b5cf9eb96
|
@ -1,12 +1,15 @@
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import voluptuous as vol # type: ignore[import-untyped]
|
import voluptuous as vol # type: ignore[import-untyped]
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||||
from homeassistant.helpers import config_validation as cv, aiohttp_client
|
from homeassistant.helpers import config_validation as cv, aiohttp_client
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
from pyhon import Hon
|
from pyhon import Hon
|
||||||
|
|
||||||
|
@ -27,47 +30,93 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
"""Set up Hon from a config entry."""
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
if (config_dir := hass.config.config_dir) is None:
|
|
||||||
raise ValueError("Missing Config Dir")
|
try:
|
||||||
hon = await Hon(
|
# Create Hon instance
|
||||||
|
hon = Hon(
|
||||||
email=entry.data[CONF_EMAIL],
|
email=entry.data[CONF_EMAIL],
|
||||||
password=entry.data[CONF_PASSWORD],
|
password=entry.data[CONF_PASSWORD],
|
||||||
mobile_id=MOBILE_ID,
|
mobile_id=MOBILE_ID,
|
||||||
session=session,
|
session=session,
|
||||||
test_data_path=Path(config_dir),
|
test_data_path=Path(hass.config.config_dir),
|
||||||
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""),
|
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""),
|
||||||
).create()
|
)
|
||||||
|
|
||||||
|
# Initialize Hon in executor
|
||||||
|
hon = await hon.create()
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
_LOGGER.error("Error creating Hon instance: %s", exc)
|
||||||
|
raise
|
||||||
|
|
||||||
|
async def async_update_data() -> dict[str, Any]:
|
||||||
|
"""Fetch data from API."""
|
||||||
|
try:
|
||||||
|
for appliance in hon.appliances:
|
||||||
|
await appliance.update()
|
||||||
|
return {"last_update": hon.api.auth.refresh_token}
|
||||||
|
except Exception as exc:
|
||||||
|
_LOGGER.error("Error updating Hon data: %s", exc)
|
||||||
|
raise
|
||||||
|
|
||||||
|
coordinator = DataUpdateCoordinator(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name=DOMAIN,
|
||||||
|
update_method=async_update_data,
|
||||||
|
update_interval=timedelta(seconds=60),
|
||||||
|
)
|
||||||
|
|
||||||
|
def _mqtt_update() -> None:
|
||||||
|
"""Handle MQTT update in event loop."""
|
||||||
|
coordinator.async_set_updated_data({"last_update": hon.api.auth.refresh_token})
|
||||||
|
|
||||||
|
def handle_update(_: Any) -> None:
|
||||||
|
"""Handle updates from MQTT subscription in a thread-safe way."""
|
||||||
|
hass.loop.call_soon_threadsafe(_mqtt_update)
|
||||||
|
|
||||||
|
# Subscribe to MQTT updates
|
||||||
|
hon.subscribe_updates(handle_update)
|
||||||
|
|
||||||
|
# Initial data fetch
|
||||||
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
# Save the new refresh token
|
# Save the new refresh token
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token}
|
entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token}
|
||||||
)
|
)
|
||||||
|
|
||||||
coordinator: DataUpdateCoordinator[dict[str, Any]] = DataUpdateCoordinator(
|
|
||||||
hass, _LOGGER, name=DOMAIN
|
|
||||||
)
|
|
||||||
hon.subscribe_updates(coordinator.async_set_updated_data)
|
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
hass.data[DOMAIN][entry.unique_id] = {"hon": hon, "coordinator": coordinator}
|
hass.data[DOMAIN][entry.unique_id] = {"hon": hon, "coordinator": coordinator}
|
||||||
|
|
||||||
for platform in PLATFORMS:
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.async_forward_entry_setup(entry, platform)
|
|
||||||
)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
refresh_token = hass.data[DOMAIN][entry.unique_id]["hon"].api.auth.refresh_token
|
"""Unload a config entry."""
|
||||||
|
hon = hass.data[DOMAIN][entry.unique_id]["hon"]
|
||||||
|
|
||||||
|
# Store refresh token
|
||||||
|
refresh_token = hon.api.auth.refresh_token
|
||||||
|
|
||||||
|
# Unsubscribe from updates
|
||||||
|
try:
|
||||||
|
hon.subscribe_updates(None) # Remove subscription
|
||||||
|
except Exception as exc:
|
||||||
|
_LOGGER.warning("Error unsubscribing from updates: %s", exc)
|
||||||
|
|
||||||
|
# Update entry with latest refresh token
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
|
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
|
||||||
)
|
)
|
||||||
unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
||||||
if unload:
|
# Unload platforms
|
||||||
if not hass.data[DOMAIN]:
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data.pop(DOMAIN, None)
|
if unload_ok:
|
||||||
return unload
|
hass.data[DOMAIN].pop(entry.unique_id)
|
||||||
|
|
||||||
|
return unload_ok
|
|
@ -834,6 +834,7 @@ class HonSensorEntity(HonEntity, SensorEntity):
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self, update: bool = True) -> None:
|
def _handle_coordinator_update(self, update: bool = True) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
value = self._device.get(self.entity_description.key, "")
|
value = self._device.get(self.entity_description.key, "")
|
||||||
if self.entity_description.key == "programName":
|
if self.entity_description.key == "programName":
|
||||||
if not (options := self._device.settings.get("startProgram.program")):
|
if not (options := self._device.settings.get("startProgram.program")):
|
||||||
|
@ -844,6 +845,7 @@ class HonSensorEntity(HonEntity, SensorEntity):
|
||||||
value = str(get_readable(self.entity_description, value))
|
value = str(get_readable(self.entity_description, value))
|
||||||
if not value and self.entity_description.state_class is not None:
|
if not value and self.entity_description.state_class is not None:
|
||||||
self._attr_native_value = 0
|
self._attr_native_value = 0
|
||||||
|
else:
|
||||||
self._attr_native_value = value
|
self._attr_native_value = value
|
||||||
if update:
|
if update:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@ -854,6 +856,7 @@ class HonConfigSensorEntity(HonEntity, SensorEntity):
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self, update: bool = True) -> None:
|
def _handle_coordinator_update(self, update: bool = True) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
sensor = self._device.settings.get(self.entity_description.key, None)
|
sensor = self._device.settings.get(self.entity_description.key, None)
|
||||||
value: float | str
|
value: float | str
|
||||||
if self.entity_description.state_class is not None:
|
if self.entity_description.state_class is not None:
|
||||||
|
|
Loading…
Reference in a new issue