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) session = aiohttp_client.async_get_clientsession(hass)
try: try:
# Create Hon instance # Initialize Hon instance in executor
hon = Hon( def init_hon():
email=entry.data[CONF_EMAIL], """Initialize Hon instance."""
password=entry.data[CONF_PASSWORD], return Hon(
mobile_id=MOBILE_ID, email=entry.data[CONF_EMAIL],
session=session, password=entry.data[CONF_PASSWORD],
test_data_path=Path(hass.config.config_dir), mobile_id=MOBILE_ID,
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""), 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() hon = await hon.create()
except Exception as exc: except Exception as exc:
@ -70,19 +74,32 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
update_interval=timedelta(seconds=60), update_interval=timedelta(seconds=60),
) )
def _mqtt_update() -> None: def _handle_mqtt_update(_: Any) -> None:
"""Handle MQTT update in event loop.""" """Handle MQTT updates."""
coordinator.async_set_updated_data({"last_update": hon.api.auth.refresh_token}) 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.""" """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
hon.subscribe_updates(handle_update) try:
hon.subscribe_updates(handle_update)
except Exception as exc:
_LOGGER.error("Error subscribing to MQTT updates: %s", exc)
# Initial data fetch # 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 # Save the new refresh token
hass.config_entries.async_update_entry( 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: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """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: 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: except Exception as exc:
_LOGGER.warning("Error unsubscribing from updates: %s", exc) _LOGGER.error("Error unloading entry: %s", exc)
return False
# 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

View file

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