Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The iskra integration."""
2 
3 from __future__ import annotations
4 
5 from pyiskra.adapters import Modbus, RestAPI
6 from pyiskra.devices import Device
7 from pyiskra.exceptions import DeviceConnectionError, DeviceNotSupported, NotAuthorised
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import (
11  CONF_ADDRESS,
12  CONF_HOST,
13  CONF_PASSWORD,
14  CONF_PORT,
15  CONF_PROTOCOL,
16  CONF_USERNAME,
17  Platform,
18 )
19 from homeassistant.core import HomeAssistant
20 from homeassistant.exceptions import ConfigEntryNotReady
21 from homeassistant.helpers import device_registry as dr
22 
23 from .const import DOMAIN, MANUFACTURER
24 from .coordinator import IskraDataUpdateCoordinator
25 
26 PLATFORMS: list[Platform] = [Platform.SENSOR]
27 
28 
29 type IskraConfigEntry = ConfigEntry[list[IskraDataUpdateCoordinator]]
30 
31 
32 async def async_setup_entry(hass: HomeAssistant, entry: IskraConfigEntry) -> bool:
33  """Set up iskra device from a config entry."""
34  conf = entry.data
35  adapter = None
36 
37  if conf[CONF_PROTOCOL] == "modbus_tcp":
38  adapter = Modbus(
39  ip_address=conf[CONF_HOST],
40  protocol="tcp",
41  port=conf[CONF_PORT],
42  modbus_address=conf[CONF_ADDRESS],
43  )
44  elif conf[CONF_PROTOCOL] == "rest_api":
45  authentication = None
46  if (username := conf.get(CONF_USERNAME)) is not None and (
47  password := conf.get(CONF_PASSWORD)
48  ) is not None:
49  authentication = {
50  "username": username,
51  "password": password,
52  }
53  adapter = RestAPI(ip_address=conf[CONF_HOST], authentication=authentication)
54 
55  # Try connecting to the device and create pyiskra device object
56  try:
57  base_device = await Device.create_device(adapter)
58  except DeviceConnectionError as e:
59  raise ConfigEntryNotReady("Cannot connect to the device") from e
60  except NotAuthorised as e:
61  raise ConfigEntryNotReady("Not authorised to connect to the device") from e
62  except DeviceNotSupported as e:
63  raise ConfigEntryNotReady("Device not supported") from e
64 
65  # Initialize the device
66  await base_device.init()
67 
68  # if the device is a gateway, add all child devices, otherwise add the device itself.
69  if base_device.is_gateway:
70  # Add the gateway device to the device registry
71  device_registry = dr.async_get(hass)
72  device_registry.async_get_or_create(
73  config_entry_id=entry.entry_id,
74  identifiers={(DOMAIN, base_device.serial)},
75  manufacturer=MANUFACTURER,
76  name=base_device.model,
77  model=base_device.model,
78  sw_version=base_device.fw_version,
79  )
80 
81  coordinators = [
82  IskraDataUpdateCoordinator(hass, child_device)
83  for child_device in base_device.get_child_devices()
84  ]
85  else:
86  coordinators = [IskraDataUpdateCoordinator(hass, base_device)]
87 
88  for coordinator in coordinators:
89  await coordinator.async_config_entry_first_refresh()
90 
91  entry.runtime_data = coordinators
92 
93  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
94 
95  return True
96 
97 
98 async def async_unload_entry(hass: HomeAssistant, entry: IskraConfigEntry) -> bool:
99  """Unload a config entry."""
100  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, IskraConfigEntry entry)
Definition: __init__.py:98
bool async_setup_entry(HomeAssistant hass, IskraConfigEntry entry)
Definition: __init__.py:32