Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Bryant Evolution integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from evolutionhttp import BryantEvolutionLocalClient
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_FILENAME, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.helpers import device_registry as dr
14 
15 from . import names
16 from .const import CONF_SYSTEM_ZONE, DOMAIN
17 
18 PLATFORMS: list[Platform] = [Platform.CLIMATE]
19 
20 type BryantEvolutionLocalClients = dict[tuple[int, int], BryantEvolutionLocalClient]
21 type BryantEvolutionConfigEntry = ConfigEntry[BryantEvolutionLocalClients]
22 _LOGGER = logging.getLogger(__name__)
23 
24 
25 async def _can_reach_device(client: BryantEvolutionLocalClient) -> bool:
26  """Return whether we can reach the device at the given filename."""
27  # Verify that we can read current temperature to check that the
28  # (filename, system, zone) is valid.
29  return await client.read_current_temperature() is not None
30 
31 
33  hass: HomeAssistant, entry: BryantEvolutionConfigEntry
34 ) -> bool:
35  """Set up Bryant Evolution from a config entry."""
36 
37  # Add a device for the SAM itself.
38  sam_uid = names.sam_device_uid(entry)
39  device_registry = dr.async_get(hass)
40  device_registry.async_get_or_create(
41  config_entry_id=entry.entry_id,
42  identifiers={(DOMAIN, sam_uid)},
43  manufacturer="Bryant",
44  name="System Access Module",
45  )
46 
47  # Add a device for each system.
48  for sys_id in (1, 2):
49  if not any(sz[0] == sys_id for sz in entry.data[CONF_SYSTEM_ZONE]):
50  _LOGGER.debug(
51  "Skipping system %s because it is not configured for this integration: %s",
52  sys_id,
53  entry.data[CONF_SYSTEM_ZONE],
54  )
55  continue
56  device_registry.async_get_or_create(
57  config_entry_id=entry.entry_id,
58  identifiers={(DOMAIN, names.system_device_uid(sam_uid, sys_id))},
59  via_device=(DOMAIN, names.sam_device_uid(entry)),
60  manufacturer="Bryant",
61  name=f"System {sys_id}",
62  )
63 
64  # Create a client for every zone.
65  entry.runtime_data = {}
66  for sz in entry.data[CONF_SYSTEM_ZONE]:
67  try:
68  client = await BryantEvolutionLocalClient.get_client(
69  sz[0], sz[1], entry.data[CONF_FILENAME]
70  )
71  if not await _can_reach_device(client):
72  raise ConfigEntryNotReady
73  entry.runtime_data[tuple(sz)] = client
74  except FileNotFoundError as f:
75  raise ConfigEntryNotReady from f
76  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
77  return True
78 
79 
81  hass: HomeAssistant, entry: BryantEvolutionConfigEntry
82 ) -> bool:
83  """Unload a config entry."""
84  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, BryantEvolutionConfigEntry entry)
Definition: __init__.py:34
bool _can_reach_device(BryantEvolutionLocalClient client)
Definition: __init__.py:25
bool async_unload_entry(HomeAssistant hass, BryantEvolutionConfigEntry entry)
Definition: __init__.py:82