Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Zigbee Home Automation."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING, Literal
6 
7 from zha.application.const import RadioType
8 from zigpy.backups import NetworkBackup
9 from zigpy.config import CONF_DEVICE, CONF_DEVICE_PATH
10 from zigpy.types import Channels
11 from zigpy.util import pick_optimal_channel
12 
13 from .const import CONF_RADIO_TYPE, DOMAIN
14 from .helpers import get_zha_data, get_zha_gateway
15 from .radio_manager import ZhaRadioManager
16 
17 if TYPE_CHECKING:
18  from homeassistant.config_entries import ConfigEntry
19  from homeassistant.core import HomeAssistant
20 
21 
22 def _get_config_entry(hass: HomeAssistant) -> ConfigEntry:
23  """Find the singleton ZHA config entry, if one exists."""
24 
25  # If ZHA is already running, use its config entry
26  zha_data = get_zha_data(hass)
27 
28  if zha_data.config_entry is not None:
29  return zha_data.config_entry
30 
31  # Otherwise, find an inactive one
32  entries = hass.config_entries.async_entries(DOMAIN)
33 
34  if len(entries) != 1:
35  raise ValueError(f"Invalid number of ZHA config entries: {entries!r}")
36 
37  return entries[0]
38 
39 
40 def async_get_active_network_settings(hass: HomeAssistant) -> NetworkBackup:
41  """Get the network settings for the currently active ZHA network."""
42  app = get_zha_gateway(hass).application_controller
43 
44  return NetworkBackup(
45  node_info=app.state.node_info,
46  network_info=app.state.network_info,
47  )
48 
49 
51  hass: HomeAssistant, config_entry: ConfigEntry | None = None
52 ) -> NetworkBackup | None:
53  """Get the network settings for the last-active ZHA network."""
54  if config_entry is None:
55  config_entry = _get_config_entry(hass)
56 
57  radio_mgr = ZhaRadioManager.from_config_entry(hass, config_entry)
58 
59  async with radio_mgr.connect_zigpy_app() as app:
60  try:
61  settings = max(app.backups, key=lambda b: b.backup_time)
62  except ValueError:
63  settings = None
64 
65  return settings
66 
67 
69  hass: HomeAssistant, config_entry: ConfigEntry | None = None
70 ) -> NetworkBackup | None:
71  """Get ZHA network settings, preferring the active settings if ZHA is running."""
72 
73  try:
75  except ValueError:
76  return await async_get_last_network_settings(hass, config_entry)
77 
78 
80  hass: HomeAssistant, config_entry: ConfigEntry | None = None
81 ) -> RadioType:
82  """Get ZHA radio type."""
83  if config_entry is None:
84  config_entry = _get_config_entry(hass)
85 
86  return RadioType[config_entry.data[CONF_RADIO_TYPE]]
87 
88 
90  hass: HomeAssistant, config_entry: ConfigEntry | None = None
91 ) -> str:
92  """Get ZHA radio path."""
93  if config_entry is None:
94  config_entry = _get_config_entry(hass)
95 
96  return config_entry.data[CONF_DEVICE][CONF_DEVICE_PATH]
97 
98 
100  hass: HomeAssistant, new_channel: int | Literal["auto"]
101 ) -> None:
102  """Migrate the ZHA network to a new channel."""
103 
104  app = get_zha_gateway(hass).application_controller
105 
106  if new_channel == "auto":
107  channel_energy = await app.energy_scan(
108  channels=Channels.ALL_CHANNELS,
109  duration_exp=4,
110  count=1,
111  )
112  new_channel = pick_optimal_channel(channel_energy)
113 
114  await app.move_network_to_channel(new_channel)
ConfigEntry _get_config_entry(HomeAssistant hass)
Definition: api.py:22
NetworkBackup async_get_active_network_settings(HomeAssistant hass)
Definition: api.py:40
NetworkBackup|None async_get_network_settings(HomeAssistant hass, ConfigEntry|None config_entry=None)
Definition: api.py:70
str async_get_radio_path(HomeAssistant hass, ConfigEntry|None config_entry=None)
Definition: api.py:91
None async_change_channel(HomeAssistant hass, int|Literal["auto"] new_channel)
Definition: api.py:101
RadioType async_get_radio_type(HomeAssistant hass, ConfigEntry|None config_entry=None)
Definition: api.py:81
NetworkBackup|None async_get_last_network_settings(HomeAssistant hass, ConfigEntry|None config_entry=None)
Definition: api.py:52
Gateway get_zha_gateway(HomeAssistant hass)
Definition: helpers.py:1028
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020