Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Ecowitt Weather Station Component."""
2 
3 from __future__ import annotations
4 
5 from aioecowitt import EcoWittListener
6 from aiohttp import web
7 
8 from homeassistant.components import webhook
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_WEBHOOK_ID, EVENT_HOMEASSISTANT_STOP, Platform
11 from homeassistant.core import Event, HomeAssistant, callback
12 
13 from .const import DOMAIN
14 
15 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
16 
17 type EcowittConfigEntry = ConfigEntry[EcoWittListener]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: EcowittConfigEntry) -> bool:
21  """Set up the Ecowitt component from UI."""
22  ecowitt = entry.runtime_data = EcoWittListener()
23 
24  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
25 
26  async def handle_webhook(
27  hass: HomeAssistant, webhook_id: str, request: web.Request
28  ) -> web.Response:
29  """Handle webhook callback."""
30  return await ecowitt.handler(request)
31 
32  webhook.async_register(
33  hass, DOMAIN, entry.title, entry.data[CONF_WEBHOOK_ID], handle_webhook
34  )
35 
36  @callback
37  def _stop_ecowitt(_: Event) -> None:
38  """Stop the Ecowitt listener."""
39  webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID])
40 
41  entry.async_on_unload(
42  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop_ecowitt)
43  )
44 
45  return True
46 
47 
48 async def async_unload_entry(hass: HomeAssistant, entry: EcowittConfigEntry) -> bool:
49  """Unload a config entry."""
50  webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID])
51 
52  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, EcowittConfigEntry entry)
Definition: __init__.py:48
bool async_setup_entry(HomeAssistant hass, EcowittConfigEntry entry)
Definition: __init__.py:20
web.Response handle_webhook(HomeAssistant hass, str webhook_id, web.Request request)
Definition: __init__.py:51