Update __init__.py

This commit is contained in:
balgerion
2025-09-30 16:10:19 +02:00
committed by GitHub
parent be02627f44
commit 4cfcd228c9

View File

@ -3,9 +3,6 @@ import logging
import asyncio import asyncio
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.components import mqtt
from datetime import timedelta
from homeassistant.util import dt as dt_util
from .mqtt_publisher import PstrykMqttPublisher from .mqtt_publisher import PstrykMqttPublisher
from .mqtt_common import setup_periodic_mqtt_publish from .mqtt_common import setup_periodic_mqtt_publish
@ -76,7 +73,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][f"{entry.entry_id}_mqtt"] = mqtt_publisher hass.data[DOMAIN][f"{entry.entry_id}_mqtt"] = mqtt_publisher
# We need to wait until sensors are fully setup # We need to wait until sensors are fully setup
async def start_mqtt_publisher(_): async def start_mqtt_publisher():
"""Start the MQTT publisher after a short delay to ensure coordinators are ready.""" """Start the MQTT publisher after a short delay to ensure coordinators are ready."""
await mqtt_publisher.async_initialize() await mqtt_publisher.async_initialize()
@ -111,7 +108,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Add delay before starting # Add delay before starting
async def delayed_start(): async def delayed_start():
await asyncio.sleep(5) # Wait 5 seconds before starting await asyncio.sleep(5) # Wait 5 seconds before starting
await start_mqtt_publisher(None) await start_mqtt_publisher()
hass.async_create_task(delayed_start()) hass.async_create_task(delayed_start())
@ -170,6 +167,12 @@ async def _cleanup_coordinators(hass: HomeAssistant, entry: ConfigEntry) -> None
# Clean up mqtt 48h mode flag # Clean up mqtt 48h mode flag
hass.data[DOMAIN].pop(f"{entry.entry_id}_mqtt_48h_mode", None) hass.data[DOMAIN].pop(f"{entry.entry_id}_mqtt_48h_mode", None)
# Clean up API client
api_client_key = f"{entry.entry_id}_api_client"
if api_client_key in hass.data[DOMAIN]:
_LOGGER.debug("Cleaning up API client for entry %s", entry.entry_id)
hass.data[DOMAIN].pop(api_client_key, None)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload sensor platform and clear data.""" """Unload sensor platform and clear data."""
# First cancel coordinators' scheduled updates # First cancel coordinators' scheduled updates