Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The WMS WebControl pro API integration."""
2 
3 from __future__ import annotations
4 
5 import aiohttp
6 from wmspro.webcontrol import WebControlPro
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_HOST, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ConfigEntryNotReady
12 from homeassistant.helpers import device_registry as dr
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 from homeassistant.helpers.typing import UNDEFINED
15 
16 from .const import DOMAIN, MANUFACTURER
17 
18 PLATFORMS: list[Platform] = [Platform.COVER, Platform.LIGHT, Platform.SCENE]
19 
20 type WebControlProConfigEntry = ConfigEntry[WebControlPro]
21 
22 
24  hass: HomeAssistant, entry: WebControlProConfigEntry
25 ) -> bool:
26  """Set up wmspro from a config entry."""
27  host = entry.data[CONF_HOST]
28  session = async_get_clientsession(hass)
29  hub = WebControlPro(host, session)
30 
31  try:
32  await hub.ping()
33  except aiohttp.ClientError as err:
34  raise ConfigEntryNotReady(f"Error while connecting to {host}") from err
35 
36  entry.runtime_data = hub
37 
38  device_registry = dr.async_get(hass)
39  device_registry.async_get_or_create(
40  config_entry_id=entry.entry_id,
41  connections={(dr.CONNECTION_NETWORK_MAC, entry.unique_id)}
42  if entry.unique_id
43  else UNDEFINED,
44  identifiers={(DOMAIN, entry.entry_id)},
45  manufacturer=MANUFACTURER,
46  model="WMS WebControl pro",
47  configuration_url=f"http://{hub.host}/system",
48  )
49 
50  try:
51  await hub.refresh()
52  for dest in hub.dests.values():
53  await dest.refresh()
54  except aiohttp.ClientError as err:
55  raise ConfigEntryNotReady(f"Error while refreshing from {host}") from err
56 
57  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
58 
59  return True
60 
61 
63  hass: HomeAssistant, entry: WebControlProConfigEntry
64 ) -> bool:
65  """Unload a config entry."""
66  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, WebControlProConfigEntry entry)
Definition: __init__.py:25
bool async_unload_entry(HomeAssistant hass, WebControlProConfigEntry entry)
Definition: __init__.py:64
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)