Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for the Dynalite networks."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant, ServiceCall
9 from homeassistant.exceptions import ConfigEntryNotReady
10 from homeassistant.helpers import config_validation as cv
11 from homeassistant.helpers.typing import ConfigType
12 
13 from .bridge import DynaliteBridge
14 from .const import (
15  ATTR_AREA,
16  ATTR_CHANNEL,
17  ATTR_HOST,
18  DOMAIN,
19  LOGGER,
20  PLATFORMS,
21  SERVICE_REQUEST_AREA_PRESET,
22  SERVICE_REQUEST_CHANNEL_LEVEL,
23 )
24 from .convert_config import convert_config
25 from .panel import async_register_dynalite_frontend
26 
27 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
28 
29 
30 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
31  """Set up the Dynalite platform."""
32  hass.data[DOMAIN] = {}
33 
34  async def dynalite_service(service_call: ServiceCall) -> None:
35  data = service_call.data
36  host = data.get(ATTR_HOST, "")
37  bridges = [
38  bridge
39  for bridge in hass.data[DOMAIN].values()
40  if not host or bridge.host == host
41  ]
42  LOGGER.debug("Selected bridged for service call: %s", bridges)
43  if service_call.service == SERVICE_REQUEST_AREA_PRESET:
44  bridge_attr = "request_area_preset"
45  elif service_call.service == SERVICE_REQUEST_CHANNEL_LEVEL:
46  bridge_attr = "request_channel_level"
47  for bridge in bridges:
48  getattr(bridge.dynalite_devices, bridge_attr)(
49  data[ATTR_AREA], data.get(ATTR_CHANNEL)
50  )
51 
52  hass.services.async_register(
53  DOMAIN,
54  SERVICE_REQUEST_AREA_PRESET,
55  dynalite_service,
56  vol.Schema(
57  {
58  vol.Optional(ATTR_HOST): cv.string,
59  vol.Required(ATTR_AREA): int,
60  vol.Optional(ATTR_CHANNEL): int,
61  }
62  ),
63  )
64 
65  hass.services.async_register(
66  DOMAIN,
67  SERVICE_REQUEST_CHANNEL_LEVEL,
68  dynalite_service,
69  vol.Schema(
70  {
71  vol.Optional(ATTR_HOST): cv.string,
72  vol.Required(ATTR_AREA): int,
73  vol.Required(ATTR_CHANNEL): int,
74  }
75  ),
76  )
77 
79 
80  return True
81 
82 
83 async def async_entry_changed(hass: HomeAssistant, entry: ConfigEntry) -> None:
84  """Reload entry since the data has changed."""
85  LOGGER.debug("Reconfiguring entry %s", entry.data)
86  bridge = hass.data[DOMAIN][entry.entry_id]
87  bridge.reload_config(entry.data)
88  LOGGER.debug("Reconfiguring entry finished %s", entry.data)
89 
90 
91 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
92  """Set up a bridge from a config entry."""
93  LOGGER.debug("Setting up entry %s", entry.data)
94  bridge = DynaliteBridge(hass, convert_config(entry.data))
95  # need to do it before the listener
96  hass.data[DOMAIN][entry.entry_id] = bridge
97  entry.async_on_unload(entry.add_update_listener(async_entry_changed))
98 
99  if not await bridge.async_setup():
100  LOGGER.error("Could not set up bridge for entry %s", entry.data)
101  hass.data[DOMAIN][entry.entry_id] = None
102  raise ConfigEntryNotReady
103 
104  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
105  return True
106 
107 
108 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
109  """Unload a config entry."""
110  LOGGER.debug("Unloading entry %s", entry.data)
111  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
112  if unload_ok:
113  hass.data[DOMAIN].pop(entry.entry_id)
114  return unload_ok
dict[str, Any] convert_config(dict[str, Any]|MappingProxyType[str, Any] config)
def async_register_dynalite_frontend(HomeAssistant hass)
Definition: panel.py:96
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:30
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:91
None async_entry_changed(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:83
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:108