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,17 +35,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
session = aiohttp_client.async_get_clientsession(hass)
try:
# Create Hon instance
hon = Hon(
email=entry.data[CONF_EMAIL],
password=entry.data[CONF_PASSWORD],
mobile_id=MOBILE_ID,
session=session,
test_data_path=Path(hass.config.config_dir),
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""),
)
# 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,
session=session,
test_data_path=Path(hass.config.config_dir),
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."""
coordinator.async_set_updated_data({"last_update": hon.api.auth.refresh_token})
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
hon.subscribe_updates(handle_update)
# 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
await coordinator.async_config_entry_first_refresh()
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,25 +115,29 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""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
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(
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
)
# Unload platforms
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.unique_id)
return unload_ok
except Exception as exc:
_LOGGER.warning("Error unsubscribing from updates: %s", exc)
# Update entry with latest refresh token
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
)
# Unload platforms
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.unique_id)
return unload_ok
_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"
}