1 """Get data from Smart Weather station via UDP."""
3 from __future__
import annotations
5 from pyweatherflowudp.client
import EVENT_DEVICE_DISCOVERED, WeatherFlowListener
6 from pyweatherflowudp.device
import EVENT_LOAD_COMPLETE, WeatherFlowDevice
7 from pyweatherflowudp.errors
import ListenerError
17 from .const
import DOMAIN, LOGGER, format_dispatch_call
25 """Set up WeatherFlow from a config entry."""
27 client = WeatherFlowListener()
30 def _async_device_discovered(device: WeatherFlowDevice) ->
None:
31 LOGGER.debug(
"Found a device: %s", device)
34 def _async_add_device_if_started(device: WeatherFlowDevice):
44 entry.async_on_unload(
47 lambda _: _async_add_device_if_started(device),
51 entry.async_on_unload(client.on(EVENT_DEVICE_DISCOVERED, _async_device_discovered))
54 await client.start_listening()
55 except ListenerError
as ex:
56 raise ConfigEntryNotReady
from ex
58 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = client
59 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
61 async
def _async_handle_ha_shutdown(event: Event) ->
None:
62 """Handle HA shutdown."""
63 await client.stop_listening()
65 entry.async_on_unload(
66 hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_handle_ha_shutdown)
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)
77 await client.stop_listening()
83 hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
85 """Remove a config entry from a device."""
86 client: WeatherFlowListener = hass.data[DOMAIN][config_entry.entry_id]
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]
str format_dispatch_call(ConfigEntry config_entry)
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, DeviceEntry device_entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
CALLBACK_TYPE async_at_started(HomeAssistant hass, Callable[[HomeAssistant], Coroutine[Any, Any, None]|None] at_start_cb)