Fix Tado unloading (#126910)
This commit is contained in:
parent
308f25fe4c
commit
20a57d6381
8 changed files with 23 additions and 55 deletions
|
@ -1,9 +1,7 @@
|
|||
"""Support for the (unofficial) Tado API."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import requests.exceptions
|
||||
|
||||
|
@ -22,9 +20,6 @@ from .const import (
|
|||
CONST_OVERLAY_TADO_MODE,
|
||||
CONST_OVERLAY_TADO_OPTIONS,
|
||||
DOMAIN,
|
||||
UPDATE_LISTENER,
|
||||
UPDATE_MOBILE_DEVICE_TRACK,
|
||||
UPDATE_TRACK,
|
||||
)
|
||||
from .services import setup_services
|
||||
from .tado_connector import TadoConnector
|
||||
|
@ -55,17 +50,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
type TadoConfigEntry = ConfigEntry[TadoRuntimeData]
|
||||
|
||||
|
||||
@dataclass
|
||||
class TadoRuntimeData:
|
||||
"""Dataclass for Tado runtime data."""
|
||||
|
||||
tadoconnector: TadoConnector
|
||||
update_track: Any
|
||||
update_mobile_device_track: Any
|
||||
update_listener: Any
|
||||
type TadoConfigEntry = ConfigEntry[TadoConnector]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool:
|
||||
|
@ -99,26 +84,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool
|
|||
await hass.async_add_executor_job(tadoconnector.update)
|
||||
|
||||
# Poll for updates in the background
|
||||
update_track = async_track_time_interval(
|
||||
hass,
|
||||
lambda now: tadoconnector.update(),
|
||||
SCAN_INTERVAL,
|
||||
entry.async_on_unload(
|
||||
async_track_time_interval(
|
||||
hass,
|
||||
lambda now: tadoconnector.update(),
|
||||
SCAN_INTERVAL,
|
||||
)
|
||||
)
|
||||
|
||||
update_mobile_devices = async_track_time_interval(
|
||||
hass,
|
||||
lambda now: tadoconnector.update_mobile_devices(),
|
||||
SCAN_MOBILE_DEVICE_INTERVAL,
|
||||
entry.async_on_unload(
|
||||
async_track_time_interval(
|
||||
hass,
|
||||
lambda now: tadoconnector.update_mobile_devices(),
|
||||
SCAN_MOBILE_DEVICE_INTERVAL,
|
||||
)
|
||||
)
|
||||
|
||||
update_listener = entry.add_update_listener(_async_update_listener)
|
||||
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
||||
|
||||
entry.runtime_data = TadoRuntimeData(
|
||||
tadoconnector=tadoconnector,
|
||||
update_track=update_track,
|
||||
update_mobile_device_track=update_mobile_devices,
|
||||
update_listener=update_listener,
|
||||
)
|
||||
entry.runtime_data = tadoconnector
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
|
@ -147,15 +131,6 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> Non
|
|||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id][UPDATE_TRACK]()
|
||||
hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER]()
|
||||
hass.data[DOMAIN][entry.entry_id][UPDATE_MOBILE_DEVICE_TRACK]()
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -121,7 +121,7 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the Tado sensor platform."""
|
||||
|
||||
tado: TadoConnector = entry.runtime_data.tadoconnector
|
||||
tado = entry.runtime_data
|
||||
devices = tado.devices
|
||||
zones = tado.zones
|
||||
entities: list[BinarySensorEntity] = []
|
||||
|
|
|
@ -105,7 +105,7 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the Tado climate platform."""
|
||||
|
||||
tado: TadoConnector = entry.runtime_data.tadoconnector
|
||||
tado = entry.runtime_data
|
||||
entities = await hass.async_add_executor_job(_generate_entities, tado)
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
|
|
@ -38,8 +38,6 @@ TADO_HVAC_ACTION_TO_HA_HVAC_ACTION = {
|
|||
CONF_FALLBACK = "fallback"
|
||||
CONF_HOME_ID = "home_id"
|
||||
DATA = "data"
|
||||
UPDATE_TRACK = "update_track"
|
||||
UPDATE_MOBILE_DEVICE_TRACK = "update_mobile_device_track"
|
||||
|
||||
# Weather
|
||||
CONDITIONS_MAP = {
|
||||
|
@ -207,8 +205,6 @@ DEFAULT_NAME = "Tado"
|
|||
TADO_HOME = "Home"
|
||||
TADO_ZONE = "Zone"
|
||||
|
||||
UPDATE_LISTENER = "update_listener"
|
||||
|
||||
# Constants for Temperature Offset
|
||||
INSIDE_TEMPERATURE_MEASUREMENT = "INSIDE_TEMPERATURE_MEASUREMENT"
|
||||
TEMP_OFFSET = "temperatureOffset"
|
||||
|
|
|
@ -28,7 +28,7 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the Tado device scannery entity."""
|
||||
_LOGGER.debug("Setting up Tado device scanner entity")
|
||||
tado: TadoConnector = entry.runtime_data.tadoconnector
|
||||
tado = entry.runtime_data
|
||||
tracked: set = set()
|
||||
|
||||
# Fix non-string unique_id for device trackers
|
||||
|
|
|
@ -71,10 +71,8 @@ def get_automatic_geofencing(data: dict[str, str]) -> bool:
|
|||
|
||||
def get_geofencing_mode(data: dict[str, str]) -> str:
|
||||
"""Return Geofencing Mode based on Presence and Presence Locked attributes."""
|
||||
tado_mode = ""
|
||||
tado_mode = data.get("presence", "unknown")
|
||||
|
||||
geofencing_switch_mode = ""
|
||||
if "presenceLocked" in data:
|
||||
if data["presenceLocked"]:
|
||||
geofencing_switch_mode = "manual"
|
||||
|
@ -199,7 +197,7 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the Tado sensor platform."""
|
||||
|
||||
tado: TadoConnector = entry.runtime_data.tadoconnector
|
||||
tado = entry.runtime_data
|
||||
zones = tado.zones
|
||||
entities: list[SensorEntity] = []
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ from .const import (
|
|||
DOMAIN,
|
||||
SERVICE_ADD_METER_READING,
|
||||
)
|
||||
from .tado_connector import TadoConnector
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
SCHEMA_ADD_METER_READING = vol.Schema(
|
||||
|
@ -44,7 +43,7 @@ def setup_services(hass: HomeAssistant) -> None:
|
|||
if entry is None:
|
||||
raise ServiceValidationError("Config entry not found")
|
||||
|
||||
tadoconnector: TadoConnector = entry.runtime_data.tadoconnector
|
||||
tadoconnector = entry.runtime_data
|
||||
|
||||
response: dict = await hass.async_add_executor_job(
|
||||
tadoconnector.set_meter_reading, call.data[CONF_READING]
|
||||
|
|
|
@ -67,7 +67,7 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the Tado water heater platform."""
|
||||
|
||||
tado: TadoConnector = entry.runtime_data.tadoconnector
|
||||
tado = entry.runtime_data
|
||||
entities = await hass.async_add_executor_job(_generate_entities, tado)
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
|
Loading…
Add table
Reference in a new issue