Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The kmtronic integration."""
2 
3 import asyncio
4 from datetime import timedelta
5 import logging
6 
7 import aiohttp
8 from pykmtronic.auth import Auth
9 from pykmtronic.hub import KMTronicHubAPI
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import aiohttp_client
15 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
16 
17 from .const import DATA_COORDINATOR, DATA_HUB, DOMAIN, MANUFACTURER, UPDATE_LISTENER
18 
19 PLATFORMS = [Platform.SWITCH]
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
25  """Set up kmtronic from a config entry."""
26  session = aiohttp_client.async_get_clientsession(hass)
27  auth = Auth(
28  session,
29  f"http://{entry.data[CONF_HOST]}",
30  entry.data[CONF_USERNAME],
31  entry.data[CONF_PASSWORD],
32  )
33  hub = KMTronicHubAPI(auth)
34 
35  async def async_update_data():
36  try:
37  async with asyncio.timeout(10):
38  await hub.async_update_relays()
39  except aiohttp.client_exceptions.ClientResponseError as err:
40  raise UpdateFailed(f"Wrong credentials: {err}") from err
41  except aiohttp.client_exceptions.ClientConnectorError as err:
42  raise UpdateFailed(f"Error communicating with API: {err}") from err
43 
44  coordinator = DataUpdateCoordinator(
45  hass,
46  _LOGGER,
47  config_entry=entry,
48  name=f"{MANUFACTURER} {hub.name}",
49  update_method=async_update_data,
50  update_interval=timedelta(seconds=30),
51  )
52  await coordinator.async_config_entry_first_refresh()
53 
54  hass.data.setdefault(DOMAIN, {})
55  hass.data[DOMAIN][entry.entry_id] = {
56  DATA_HUB: hub,
57  DATA_COORDINATOR: coordinator,
58  }
59 
60  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
61 
62  update_listener = entry.add_update_listener(async_update_options)
63  hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] = update_listener
64 
65  return True
66 
67 
68 async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
69  """Update options."""
70  await hass.config_entries.async_reload(config_entry.entry_id)
71 
72 
73 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
74  """Unload a config entry."""
75  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
76  if unload_ok:
77  update_listener = hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER]
79  hass.data[DOMAIN].pop(entry.entry_id)
80 
81  return unload_ok
None async_update_options(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:68
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:24
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:73
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30