1 """API for Zigbee Home Automation."""
3 from __future__
import annotations
5 from typing
import TYPE_CHECKING, Literal
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
13 from .const
import CONF_RADIO_TYPE, DOMAIN
14 from .helpers
import get_zha_data, get_zha_gateway
15 from .radio_manager
import ZhaRadioManager
23 """Find the singleton ZHA config entry, if one exists."""
28 if zha_data.config_entry
is not None:
29 return zha_data.config_entry
32 entries = hass.config_entries.async_entries(DOMAIN)
35 raise ValueError(f
"Invalid number of ZHA config entries: {entries!r}")
41 """Get the network settings for the currently active ZHA network."""
45 node_info=app.state.node_info,
46 network_info=app.state.network_info,
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:
57 radio_mgr = ZhaRadioManager.from_config_entry(hass, config_entry)
59 async
with radio_mgr.connect_zigpy_app()
as app:
61 settings =
max(app.backups, key=
lambda b: b.backup_time)
69 hass: HomeAssistant, config_entry: ConfigEntry |
None =
None
70 ) -> NetworkBackup |
None:
71 """Get ZHA network settings, preferring the active settings if ZHA is running."""
80 hass: HomeAssistant, config_entry: ConfigEntry |
None =
None
82 """Get ZHA radio type."""
83 if config_entry
is None:
86 return RadioType[config_entry.data[CONF_RADIO_TYPE]]
90 hass: HomeAssistant, config_entry: ConfigEntry |
None =
None
92 """Get ZHA radio path."""
93 if config_entry
is None:
96 return config_entry.data[CONF_DEVICE][CONF_DEVICE_PATH]
100 hass: HomeAssistant, new_channel: int | Literal[
"auto"]
102 """Migrate the ZHA network to a new channel."""
106 if new_channel ==
"auto":
107 channel_energy = await app.energy_scan(
108 channels=Channels.ALL_CHANNELS,
112 new_channel = pick_optimal_channel(channel_energy)
114 await app.move_network_to_channel(new_channel)
ConfigEntry _get_config_entry(HomeAssistant hass)
NetworkBackup async_get_active_network_settings(HomeAssistant hass)
NetworkBackup|None async_get_network_settings(HomeAssistant hass, ConfigEntry|None config_entry=None)
str async_get_radio_path(HomeAssistant hass, ConfigEntry|None config_entry=None)
None async_change_channel(HomeAssistant hass, int|Literal["auto"] new_channel)
RadioType async_get_radio_type(HomeAssistant hass, ConfigEntry|None config_entry=None)
NetworkBackup|None async_get_last_network_settings(HomeAssistant hass, ConfigEntry|None config_entry=None)
Gateway get_zha_gateway(HomeAssistant hass)
HAZHAData get_zha_data(HomeAssistant hass)