Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Get data from Smart Weather station via UDP."""
2 
3 from __future__ import annotations
4 
5 from pyweatherflowudp.client import EVENT_DEVICE_DISCOVERED, WeatherFlowListener
6 from pyweatherflowudp.device import EVENT_LOAD_COMPLETE, WeatherFlowDevice
7 from pyweatherflowudp.errors import ListenerError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import EVENT_HOMEASSISTANT_STOP, Platform
11 from homeassistant.core import Event, HomeAssistant, callback
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.helpers.device_registry import DeviceEntry
14 from homeassistant.helpers.dispatcher import async_dispatcher_send
15 from homeassistant.helpers.start import async_at_started
16 
17 from .const import DOMAIN, LOGGER, format_dispatch_call
18 
19 PLATFORMS = [
20  Platform.SENSOR,
21 ]
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
25  """Set up WeatherFlow from a config entry."""
26 
27  client = WeatherFlowListener()
28 
29  @callback
30  def _async_device_discovered(device: WeatherFlowDevice) -> None:
31  LOGGER.debug("Found a device: %s", device)
32 
33  @callback
34  def _async_add_device_if_started(device: WeatherFlowDevice):
36  hass,
37  callback(
38  lambda _: async_dispatcher_send(
39  hass, format_dispatch_call(entry), device
40  )
41  ),
42  )
43 
44  entry.async_on_unload(
45  device.on(
46  EVENT_LOAD_COMPLETE,
47  lambda _: _async_add_device_if_started(device),
48  )
49  )
50 
51  entry.async_on_unload(client.on(EVENT_DEVICE_DISCOVERED, _async_device_discovered))
52 
53  try:
54  await client.start_listening()
55  except ListenerError as ex:
56  raise ConfigEntryNotReady from ex
57 
58  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = client
59  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
60 
61  async def _async_handle_ha_shutdown(event: Event) -> None:
62  """Handle HA shutdown."""
63  await client.stop_listening()
64 
65  entry.async_on_unload(
66  hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_handle_ha_shutdown)
67  )
68 
69  return True
70 
71 
72 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
73  """Unload a config entry."""
74  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
75  client: WeatherFlowListener = hass.data[DOMAIN].pop(entry.entry_id, None)
76  if client:
77  await client.stop_listening()
78 
79  return unload_ok
80 
81 
83  hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
84 ) -> bool:
85  """Remove a config entry from a device."""
86  client: WeatherFlowListener = hass.data[DOMAIN][config_entry.entry_id]
87  return not any(
88  identifier
89  for identifier in device_entry.identifiers
90  if identifier[0] == DOMAIN
91  for device in client.devices
92  if device.serial_number == identifier[1]
93  )
str format_dispatch_call(ConfigEntry config_entry)
Definition: const.py:11
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, DeviceEntry device_entry)
Definition: __init__.py:84
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:24
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:72
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193
CALLBACK_TYPE async_at_started(HomeAssistant hass, Callable[[HomeAssistant], Coroutine[Any, Any, None]|None] at_start_cb)
Definition: start.py:80