Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for device connected via Lightwave WiFi-link hub."""
2 
3 import logging
4 
5 from lightwave.lightwave import LWLink
6 import voluptuous as vol
7 
8 from homeassistant.const import (
9  CONF_HOST,
10  CONF_LIGHTS,
11  CONF_NAME,
12  CONF_SWITCHES,
13  Platform,
14 )
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.discovery import async_load_platform
18 from homeassistant.helpers.typing import ConfigType
19 
20 CONF_SERIAL = "serial"
21 CONF_PROXY_IP = "proxy_ip"
22 CONF_PROXY_PORT = "proxy_port"
23 CONF_TRV = "trv"
24 CONF_TRVS = "trvs"
25 DEFAULT_PROXY_PORT = 7878
26 DOMAIN = "lightwave"
27 LIGHTWAVE_LINK = f"{DOMAIN}_link"
28 LIGHTWAVE_TRV_PROXY = f"{DOMAIN}_proxy"
29 LIGHTWAVE_TRV_PROXY_PORT = f"{DOMAIN}_proxy_port"
30 
31 _LOGGER = logging.getLogger(__name__)
32 
33 
34 CONFIG_SCHEMA = vol.Schema(
35  {
36  DOMAIN: vol.Schema(
37  vol.All(
38  cv.has_at_least_one_key(CONF_LIGHTS, CONF_SWITCHES, CONF_TRV),
39  {
40  vol.Required(CONF_HOST): cv.string,
41  vol.Optional(CONF_LIGHTS, default={}): {
42  cv.string: vol.Schema({vol.Required(CONF_NAME): cv.string})
43  },
44  vol.Optional(CONF_SWITCHES, default={}): {
45  cv.string: vol.Schema({vol.Required(CONF_NAME): cv.string})
46  },
47  vol.Optional(CONF_TRV, default={}): {
48  vol.Optional(
49  CONF_PROXY_PORT, default=DEFAULT_PROXY_PORT
50  ): cv.port,
51  vol.Optional(CONF_PROXY_IP): cv.string,
52  vol.Required(CONF_TRVS, default={}): {
53  cv.string: vol.Schema(
54  {
55  vol.Required(CONF_NAME): cv.string,
56  vol.Required(CONF_SERIAL): cv.string,
57  }
58  )
59  },
60  },
61  },
62  )
63  )
64  },
65  extra=vol.ALLOW_EXTRA,
66 )
67 
68 PLATFORMS = (Platform.CLIMATE, Platform.SENSOR)
69 
70 
71 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
72  """Try to start embedded Lightwave broker."""
73  host = config[DOMAIN][CONF_HOST]
74  lwlink = LWLink(host)
75  hass.data[LIGHTWAVE_LINK] = lwlink
76 
77  if lights := config[DOMAIN][CONF_LIGHTS]:
78  hass.async_create_task(
79  async_load_platform(hass, Platform.LIGHT, DOMAIN, lights, config)
80  )
81 
82  if switches := config[DOMAIN][CONF_SWITCHES]:
83  hass.async_create_task(
84  async_load_platform(hass, Platform.SWITCH, DOMAIN, switches, config)
85  )
86 
87  if trv := config[DOMAIN][CONF_TRV]:
88  trvs = trv[CONF_TRVS]
89  proxy_ip = trv.get(CONF_PROXY_IP)
90  proxy_port = trv[CONF_PROXY_PORT]
91  if proxy_ip is None:
92  await lwlink.LW_listen()
93  else:
94  lwlink.set_trv_proxy(proxy_ip, proxy_port)
95  _LOGGER.warning(
96  "Proxy no longer required, remove `proxy_ip` from config to use builtin listener"
97  )
98 
99  for platform in PLATFORMS:
100  hass.async_create_task(
101  async_load_platform(hass, platform, DOMAIN, trvs, config)
102  )
103 
104  return True
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:71
None async_load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)
Definition: discovery.py:152