update manifest

This commit is contained in:
Jan Kozak 2024-12-29 10:55:29 +01:00
parent 1b5cf9eb96
commit ea404bb477
2 changed files with 62 additions and 40 deletions

View file

@ -35,8 +35,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
session = aiohttp_client.async_get_clientsession(hass)
try:
# Create Hon instance
hon = Hon(
# Initialize Hon instance in executor
def init_hon():
"""Initialize Hon instance."""
return Hon(
email=entry.data[CONF_EMAIL],
password=entry.data[CONF_PASSWORD],
mobile_id=MOBILE_ID,
@ -45,7 +47,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""),
)
# Initialize Hon in executor
# Create Hon instance in executor
hon = await hass.async_add_executor_job(init_hon)
# Create and initialize
hon = await hon.create()
except Exception as exc:
@ -70,19 +74,32 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
update_interval=timedelta(seconds=60),
)
def _mqtt_update() -> None:
"""Handle MQTT update in event loop."""
def _handle_mqtt_update(_: Any) -> None:
"""Handle MQTT updates."""
try:
coordinator.async_set_updated_data({"last_update": hon.api.auth.refresh_token})
except Exception as exc:
_LOGGER.error("Error handling MQTT update: %s", exc)
def handle_update(_: Any) -> None:
def handle_update(msg: Any) -> None:
"""Handle updates from MQTT subscription in a thread-safe way."""
hass.loop.call_soon_threadsafe(_mqtt_update)
try:
hass.loop.call_soon_threadsafe(_handle_mqtt_update, msg)
except Exception as exc:
_LOGGER.error("Error scheduling MQTT update: %s", exc)
# Subscribe to MQTT updates
# Subscribe to MQTT updates with error handling
try:
hon.subscribe_updates(handle_update)
except Exception as exc:
_LOGGER.error("Error subscribing to MQTT updates: %s", exc)
# Initial data fetch
try:
await coordinator.async_config_entry_first_refresh()
except Exception as exc:
_LOGGER.error("Error during initial refresh: %s", exc)
raise
# Save the new refresh token
hass.config_entries.async_update_entry(
@ -98,6 +115,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
try:
hon = hass.data[DOMAIN][entry.unique_id]["hon"]
# Store refresh token
@ -120,3 +138,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.unique_id)
return unload_ok
except Exception as exc:
_LOGGER.error("Error unloading entry: %s", exc)
return False

View file

@ -2,7 +2,8 @@
"domain": "hon",
"name": "Haier hOn",
"codeowners": [
"@Andre0512"
"@Andre0512",
"@galvani"
],
"config_flow": true,
"documentation": "https://github.com/Andre0512/hon/",
@ -11,5 +12,5 @@
"requirements": [
"pyhOn==0.17.5"
],
"version": "0.14.0"
"version": "0.14.1"
}