mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Improve seperation of lookin udp listener and typing (#64742)
This commit is contained in:
parent
3360a95156
commit
e6affb8b88
@ -30,9 +30,7 @@ from .models import LookinData
|
|||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
UDP_LOCK = "udp_lock"
|
UDP_MANAGER = "udp_manager"
|
||||||
UDP_LISTENER = "udp_listener"
|
|
||||||
UDP_SUBSCRIPTIONS = "udp_subscriptions"
|
|
||||||
|
|
||||||
|
|
||||||
def _async_climate_updater(
|
def _async_climate_updater(
|
||||||
@ -59,42 +57,35 @@ def _async_remote_updater(
|
|||||||
return _async_update
|
return _async_update
|
||||||
|
|
||||||
|
|
||||||
async def async_start_udp_listener(hass: HomeAssistant) -> LookinUDPSubscriptions:
|
class LookinUDPManager:
|
||||||
"""Start the shared udp listener."""
|
"""Manage the lookin UDP subscriptions."""
|
||||||
domain_data = hass.data[DOMAIN]
|
|
||||||
if UDP_LOCK not in domain_data:
|
|
||||||
udp_lock = domain_data[UDP_LOCK] = asyncio.Lock()
|
|
||||||
else:
|
|
||||||
udp_lock = domain_data[UDP_LOCK]
|
|
||||||
|
|
||||||
async with udp_lock:
|
def __init__(self) -> None:
|
||||||
if UDP_LISTENER not in domain_data:
|
"""Init the manager."""
|
||||||
lookin_udp_subs = domain_data[UDP_SUBSCRIPTIONS] = LookinUDPSubscriptions()
|
self._lock = asyncio.Lock()
|
||||||
domain_data[UDP_LISTENER] = await start_lookin_udp(lookin_udp_subs, None)
|
self._listener: Callable | None = None
|
||||||
else:
|
self._subscriptions: LookinUDPSubscriptions | None = None
|
||||||
lookin_udp_subs = domain_data[UDP_SUBSCRIPTIONS]
|
|
||||||
return lookin_udp_subs
|
|
||||||
|
|
||||||
|
async def async_get_subscriptions(self) -> LookinUDPSubscriptions:
|
||||||
|
"""Get the shared LookinUDPSubscriptions."""
|
||||||
|
async with self._lock:
|
||||||
|
if not self._listener:
|
||||||
|
self._subscriptions = LookinUDPSubscriptions()
|
||||||
|
self._listener = await start_lookin_udp(self._subscriptions, None)
|
||||||
|
return self._subscriptions
|
||||||
|
|
||||||
async def async_stop_udp_listener(hass: HomeAssistant) -> None:
|
async def async_stop(self) -> None:
|
||||||
"""Stop the shared udp listener."""
|
"""Stop the listener."""
|
||||||
domain_data = hass.data[DOMAIN]
|
async with self._lock:
|
||||||
async with domain_data[UDP_LOCK]:
|
assert self._listener is not None
|
||||||
loaded_entries = [
|
self._listener()
|
||||||
entry
|
self._listener = None
|
||||||
for entry in hass.config_entries.async_entries(DOMAIN)
|
self._subscriptions = None
|
||||||
if entry.state == ConfigEntryState.LOADED
|
|
||||||
]
|
|
||||||
if len(loaded_entries) > 1:
|
|
||||||
return
|
|
||||||
domain_data[UDP_LISTENER]()
|
|
||||||
del domain_data[UDP_LISTENER]
|
|
||||||
del domain_data[UDP_SUBSCRIPTIONS]
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up lookin from a config entry."""
|
"""Set up lookin from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
domain_data = hass.data.setdefault(DOMAIN, {})
|
||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
lookin_protocol = LookInHttpProtocol(
|
lookin_protocol = LookInHttpProtocol(
|
||||||
api_uri=f"http://{host}", session=async_get_clientsession(hass)
|
api_uri=f"http://{host}", session=async_get_clientsession(hass)
|
||||||
@ -148,7 +139,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
meteo.update_from_value(event.value)
|
meteo.update_from_value(event.value)
|
||||||
meteo_coordinator.async_set_updated_data(meteo)
|
meteo_coordinator.async_set_updated_data(meteo)
|
||||||
|
|
||||||
lookin_udp_subs = await async_start_udp_listener(hass)
|
if UDP_MANAGER not in domain_data:
|
||||||
|
manager = domain_data[UDP_MANAGER] = LookinUDPManager()
|
||||||
|
else:
|
||||||
|
manager = domain_data[UDP_MANAGER]
|
||||||
|
|
||||||
|
lookin_udp_subs = await manager.async_get_subscriptions()
|
||||||
|
|
||||||
entry.async_on_unload(
|
entry.async_on_unload(
|
||||||
lookin_udp_subs.subscribe_event(
|
lookin_udp_subs.subscribe_event(
|
||||||
@ -175,5 +171,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
await async_stop_udp_listener(hass)
|
loaded_entries = [
|
||||||
|
entry
|
||||||
|
for entry in hass.config_entries.async_entries(DOMAIN)
|
||||||
|
if entry.state == ConfigEntryState.LOADED
|
||||||
|
]
|
||||||
|
if len(loaded_entries) == 1:
|
||||||
|
manager: LookinUDPManager = hass.data[DOMAIN][UDP_MANAGER]
|
||||||
|
await manager.async_stop()
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
Loading…
x
Reference in New Issue
Block a user