Home Assistant Unofficial Reference 2024.12.1
silabs_multiprotocol.py
Go to the documentation of this file.
1 """Silicon Labs Multiprotocol support."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import contextlib
7 
9  is_multiprotocol_url,
10 )
11 from homeassistant.core import HomeAssistant
12 
13 from . import api
14 
15 # The approximate time it takes ZHA to change channels on SiLabs coordinators
16 ZHA_CHANNEL_CHANGE_TIME_S = 10.27
17 
18 
19 def _get_zha_url(hass: HomeAssistant) -> str | None:
20  """Return the ZHA radio path, or None if there's no ZHA config entry."""
21  with contextlib.suppress(ValueError):
22  return api.async_get_radio_path(hass)
23  return None
24 
25 
26 async def _get_zha_channel(hass: HomeAssistant) -> int | None:
27  """Get ZHA channel, or None if there's no ZHA config entry."""
28  zha_network_settings: api.NetworkBackup | None
29  with contextlib.suppress(ValueError):
30  zha_network_settings = await api.async_get_network_settings(hass)
31  if not zha_network_settings:
32  return None
33  channel: int = zha_network_settings.network_info.channel
34  # ZHA uses channel 0 when no channel is set
35  return channel or None
36 
37 
39  hass: HomeAssistant, channel: int, delay: float = 0
40 ) -> asyncio.Task | None:
41  """Set the channel to be used.
42 
43  Does nothing if not configured.
44  """
45  zha_url = _get_zha_url(hass)
46  if not zha_url:
47  # ZHA is not configured
48  return None
49 
50  async def finish_migration() -> None:
51  """Finish the channel migration."""
52  await asyncio.sleep(max(0, delay - ZHA_CHANNEL_CHANGE_TIME_S))
53  return await api.async_change_channel(hass, channel)
54 
55  return hass.async_create_task(finish_migration())
56 
57 
58 async def async_get_channel(hass: HomeAssistant) -> int | None:
59  """Return the channel.
60 
61  Returns None if not configured.
62  """
63  zha_url = _get_zha_url(hass)
64  if not zha_url:
65  # ZHA is not configured
66  return None
67 
68  return await _get_zha_channel(hass)
69 
70 
71 async def async_using_multipan(hass: HomeAssistant) -> bool:
72  """Return if the multiprotocol device is used.
73 
74  Returns False if not configured.
75  """
76  zha_url = _get_zha_url(hass)
77  if not zha_url:
78  # ZHA is not configured
79  return False
80 
81  return is_multiprotocol_url(zha_url)
asyncio.Task|None async_change_channel(HomeAssistant hass, int channel, float delay=0)