Skip to content

Commit 962ad99

Browse files
mib1185frenck
authored andcommitted
Add workaround for sub units without main device in AVM Fritz!SmartHome (home-assistant#148507)
1 parent 9c9836d commit 962ad99

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

homeassistant/components/fritzbox/coordinator.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,19 @@ async def _async_update_data(self) -> FritzboxCoordinatorData:
171171

172172
for device in new_data.devices.values():
173173
# create device registry entry for new main devices
174-
if (
175-
device.ain not in self.data.devices
176-
and device.device_and_unit_id[1] is None
174+
if device.ain not in self.data.devices and (
175+
device.device_and_unit_id[1] is None
176+
or (
177+
# workaround for sub units without a main device, e.g. Energy 250
178+
# https://github.com/home-assistant/core/issues/145204
179+
device.device_and_unit_id[1] == "1"
180+
and device.device_and_unit_id[0] not in new_data.devices
181+
)
177182
):
178183
dr.async_get(self.hass).async_get_or_create(
179184
config_entry_id=self.config_entry.entry_id,
180185
name=device.name,
181-
identifiers={(DOMAIN, device.ain)},
186+
identifiers={(DOMAIN, device.device_and_unit_id[0])},
182187
manufacturer=device.manufacturer,
183188
model=device.productname,
184189
sw_version=device.fw_version,

tests/components/fritzbox/test_coordinator.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
from homeassistant.helpers import device_registry as dr, entity_registry as er
1616
from homeassistant.util.dt import utcnow
1717

18-
from . import FritzDeviceCoverMock, FritzDeviceSwitchMock, FritzEntityBaseMock
18+
from . import (
19+
FritzDeviceCoverMock,
20+
FritzDeviceSensorMock,
21+
FritzDeviceSwitchMock,
22+
FritzEntityBaseMock,
23+
)
1924
from .const import MOCK_CONFIG
2025

2126
from tests.common import MockConfigEntry, async_fire_time_changed
@@ -140,3 +145,42 @@ async def test_coordinator_automatic_registry_cleanup(
140145

141146
assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 12
142147
assert len(dr.async_entries_for_config_entry(device_registry, entry.entry_id)) == 1
148+
149+
150+
async def test_coordinator_workaround_sub_units_without_main_device(
151+
hass: HomeAssistant,
152+
fritz: Mock,
153+
device_registry: dr.DeviceRegistry,
154+
) -> None:
155+
"""Test the workaround for sub units without main device."""
156+
fritz().get_devices.return_value = [
157+
FritzDeviceSensorMock(
158+
ain="bad_device-1",
159+
device_and_unit_id=("bad_device", "1"),
160+
name="bad_sensor_sub",
161+
),
162+
FritzDeviceSensorMock(
163+
ain="good_device",
164+
device_and_unit_id=("good_device", None),
165+
name="good_sensor",
166+
),
167+
FritzDeviceSensorMock(
168+
ain="good_device-1",
169+
device_and_unit_id=("good_device", "1"),
170+
name="good_sensor_sub",
171+
),
172+
]
173+
174+
entry = MockConfigEntry(
175+
domain=DOMAIN,
176+
data=MOCK_CONFIG[DOMAIN][CONF_DEVICES][0],
177+
unique_id="any",
178+
)
179+
entry.add_to_hass(hass)
180+
await hass.config_entries.async_setup(entry.entry_id)
181+
await hass.async_block_till_done(wait_background_tasks=True)
182+
183+
device_entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
184+
assert len(device_entries) == 2
185+
assert device_entries[0].identifiers == {(DOMAIN, "good_device")}
186+
assert device_entries[1].identifiers == {(DOMAIN, "bad_device")}

0 commit comments

Comments
 (0)