8000 Check if Multi-PAN addon is using the HA Yellow's radio (#82853) · home-assistant/core@892be99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 892be99

Browse files
authored
Check if Multi-PAN addon is using the HA Yellow's radio (#82853)
1 parent 58b3a00 commit 892be99

File tree

3 files changed

+80
-14
lines changed

3 files changed

+80
-14
lines changed

homeassistant/components/homeassistant_yellow/__init__.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,13 @@
1818
from homeassistant.core import HomeAssistant
1919
from homeassistant.exceptions import ConfigEntryNotReady
2020

21-
from .const import ZHA_HW_DISCOVERY_DATA
21+
from .const import RADIO_DEVICE, ZHA_HW_DISCOVERY_DATA
2222

2323
_LOGGER = logging.getLogger(__name__)
2424

2525

26-
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
27-
"""Set up a Home Assistant Yellow config entry."""
28-
if (os_info := get_os_info(hass)) is None:
29-
# The hassio integration has not yet fetched data from the supervisor
30-
raise ConfigEntryNotReady
31-
32-
board: str | None
33-
if (board := os_info.get("board")) is None or not board == "yellow":
34-
# Not running on a Home Assistant Yellow, Home Assistant may have been migrated
35-
hass.async_create_task(hass.config_entries.async_remove(entry.entry_id))
36-
return False
37-
26+
async def _multi_pan_addon_info(hass, entry: ConfigEntry) -> AddonInfo | None:
27+
"""Return AddonInfo if the multi-PAN addon is enabled for the Yellow's radio."""
3828
addon_manager: AddonManager = get_addon_manager(hass)
3929
try:
4030
addon_info: AddonInfo = await addon_manager.async_get_addon_info()
@@ -54,6 +44,29 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5444
raise ConfigEntryNotReady
5545

5646
if addon_info.state == AddonState.NOT_INSTALLED:
47+
return None
48+
49+
if addon_info.options["device"] != RADIO_DEVICE:
50+
return None
51+
52+
return addon_info
53+
54+
55+
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
56+
"""Set up a Home Assistant Yellow config entry."""
57+
if (os_info := get_os_info(hass)) is None:
58+
# The hassio integration has not yet fetched data from the supervisor
59+
raise ConfigEntryNotReady
60+
61+
board: str | None
62+
if (board := os_info.get("board")) is None or not board == "yellow":
63+
# Not running on a Home Assistant Yellow, Home Assistant may have been migrated
64+
hass.async_create_task(hass.config_entries.async_remove(entry.entry_id))
65+
return False
66+
67+
addon_info = await _multi_pan_addon_info(hass, entry)
68+
69+
if not addon_info:
5770
hw_discovery_data = ZHA_HW_DISCOVERY_DATA
5871
else:
5972
hw_discovery_data = {

homeassistant/components/homeassistant_yellow/const.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
DOMAIN = "homeassistant_yellow"
44

5+
RADIO_DEVICE = "/dev/ttyAMA1"
56
ZHA_HW_DISCOVERY_DATA = {
67
"name": "Yellow",
78
"port": {
8-
"path": "/dev/ttyAMA1",
9+
"path": RADIO_DEVICE,
910
"baudrate": 115200,
1011
"flow_control": "hardware",
1112
},

tests/components/homeassistant_yellow/test_init.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ async def test_setup_zha_multipan(
110110
"""Test zha gets the right config."""
111111
mock_integration(hass, MockModule("hassio"))
112112

113+
addon_info.return_value["options"]["device"] = "/dev/ttyAMA1"
114+
113115
# Setup the config entry
114116
config_entry = MockConfigEntry(
115117
data={},
@@ -152,6 +154,56 @@ async def test_setup_zha_multipan(
152154
assert config_entry.title == "Yellow Multi-PAN"
153155

154156

157+
async def test_setup_zha_multipan_other_device(
158+
hass: HomeAssistant, addon_info, addon_running
159+
) -> None:
160+
"""Test zha gets the right config."""
161+
mock_integration(hass, MockModule("hassio"))
162+
163+
addon_info.return_value["options"]["device"] = "/dev/not_yellow_radio"
164+
165+
# Setup the config entry
166+
config_entry = MockConfigEntry(
167+
data={},
168+
domain=DOMAIN,
169+
options={},
170+
title="Home Assistant Yellow",
171+
)
172+
config_entry.add_to_hass(hass)
173+
with patch(
174+
"homeassistant.components.homeassistant_yellow.get_os_info",
175+
return_value={"board": "yellow"},
176+
) as mock_get_os_info, patch(
177+
"homeassistant.components.onboarding.async_is_onboarded", return_value=False
178+
):
179+
assert await hass.config_entries.async_setup(config_entry.entry_id)
180+
await hass.async_block_till_done()
181+
assert len(mock_get_os_info.mock_calls) == 1
182+
183+
# Finish setting up ZHA
184+
zha_flows = hass.config_entries.flow.async_progress_by_handler("zha")
185+
assert len(zha_flows) == 1
186+
assert zha_flows[0]["step_id"] == "choose_formation_strategy"
187+
188+
await hass.config_entries.flow.async_configure(
189+
zha_flows[0]["flow_id"],
190+
user_input={"next_step_id": zha.config_flow.FORMATION_REUSE_SETTINGS},
191+
)
192+
await hass.async_block_till_done()
193+
194+
config_entry = hass.config_entries.async_entries("zha")[0]
195+
assert config_entry.data == {
196+
"device": {
197+
"baudrate": 115200,
198+
"flow_control": "hardware",
199+
"path": "/dev/ttyAMA1",
200+
},
201+
"radio_type": "ezsp",
202+
}
203+
assert config_entry.options == {}
204+
assert config_entry.title == "Yellow"
205+
206+
155207
async def test_setup_entry_wrong_board(hass: HomeAssistant) -> None:
156208
"""Test setup of a config entry with wrong board type."""
157209
mock_integration(hass, MockModule("hassio"))

0 commit comments

Comments
 (0)
0