Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Roth Touchline SL integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 
7 from pytouchlinesl import TouchlineSL
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import device_registry as dr
13 
14 from .const import DOMAIN
15 from .coordinator import TouchlineSLModuleCoordinator
16 
17 PLATFORMS: list[Platform] = [Platform.CLIMATE]
18 
19 type TouchlineSLConfigEntry = ConfigEntry[list[TouchlineSLModuleCoordinator]]
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: TouchlineSLConfigEntry) -> bool:
23  """Set up Roth Touchline SL from a config entry."""
24  account = TouchlineSL(
25  username=entry.data[CONF_USERNAME], password=entry.data[CONF_PASSWORD]
26  )
27 
28  coordinators: list[TouchlineSLModuleCoordinator] = [
29  TouchlineSLModuleCoordinator(hass, module) for module in await account.modules()
30  ]
31 
32  await asyncio.gather(
33  *[
34  coordinator.async_config_entry_first_refresh()
35  for coordinator in coordinators
36  ]
37  )
38 
39  device_registry = dr.async_get(hass)
40 
41  # Create a new Device for each coorodinator to represent each module
42  for c in coordinators:
43  module = c.data.module
44  device_registry.async_get_or_create(
45  config_entry_id=entry.entry_id,
46  identifiers={(DOMAIN, module.id)},
47  name=module.name,
48  manufacturer="Roth",
49  model=module.type,
50  sw_version=module.version,
51  )
52 
53  entry.runtime_data = coordinators
54  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
55 
56  return True
57 
58 
60  hass: HomeAssistant, entry: TouchlineSLConfigEntry
61 ) -> bool:
62  """Unload a config entry."""
63  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, TouchlineSLConfigEntry entry)
Definition: __init__.py:61
bool async_setup_entry(HomeAssistant hass, TouchlineSLConfigEntry entry)
Definition: __init__.py:22