Fix enigma2 integration for devices not reporting MAC address (#133226)

This commit is contained in:
Sid 2024-12-15 11:05:17 +01:00 committed by GitHub
parent f8da2c3e5c
commit 412aa60e8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 26 deletions

View File

@ -133,7 +133,8 @@ class Enigma2ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
except Exception: # noqa: BLE001
errors = {"base": "unknown"}
else:
await self.async_set_unique_id(about["info"]["ifaces"][0]["mac"])
unique_id = about["info"]["ifaces"][0]["mac"] or self.unique_id
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
return errors

View File

@ -35,6 +35,7 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
"""The Enigma2 data update coordinator."""
device: OpenWebIfDevice
unique_id: str | None
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Initialize the Enigma2 data update coordinator."""
@ -64,6 +65,10 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
name=config_entry.data[CONF_HOST],
)
# set the unique ID for the entities to the config entry unique ID
# for devices that don't report a MAC address
self.unique_id = config_entry.unique_id
async def _async_setup(self) -> None:
"""Provide needed data to the device info."""
@ -71,16 +76,20 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
self.device.mac_address = about["info"]["ifaces"][0]["mac"]
self.device_info["model"] = about["info"]["model"]
self.device_info["manufacturer"] = about["info"]["brand"]
self.device_info[ATTR_IDENTIFIERS] = {
(DOMAIN, format_mac(iface["mac"]))
for iface in about["info"]["ifaces"]
if "mac" in iface and iface["mac"] is not None
}
self.device_info[ATTR_CONNECTIONS] = {
(CONNECTION_NETWORK_MAC, format_mac(iface["mac"]))
for iface in about["info"]["ifaces"]
if "mac" in iface and iface["mac"] is not None
}
if self.device.mac_address is not None:
self.device_info[ATTR_IDENTIFIERS] = {
(DOMAIN, format_mac(iface["mac"]))
for iface in about["info"]["ifaces"]
if "mac" in iface and iface["mac"] is not None
}
self.device_info[ATTR_CONNECTIONS] = {
(CONNECTION_NETWORK_MAC, format_mac(iface["mac"]))
for iface in about["info"]["ifaces"]
if "mac" in iface and iface["mac"] is not None
}
self.unique_id = self.device.mac_address
elif self.unique_id is not None:
self.device_info[ATTR_IDENTIFIERS] = {(DOMAIN, self.unique_id)}
async def _async_update_data(self) -> OpenWebIfStatus:
await self.device.update()

View File

@ -4,7 +4,6 @@ from __future__ import annotations
import contextlib
from logging import getLogger
from typing import cast
from aiohttp.client_exceptions import ServerDisconnectedError
from openwebif.enums import PowerState, RemoteControlCodes, SetVolumeOption
@ -15,7 +14,6 @@ from homeassistant.components.media_player import (
MediaPlayerState,
MediaType,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -65,10 +63,7 @@ class Enigma2Device(CoordinatorEntity[Enigma2UpdateCoordinator], MediaPlayerEnti
super().__init__(coordinator)
self._attr_unique_id = (
coordinator.device.mac_address
or cast(ConfigEntry, coordinator.config_entry).entry_id
)
self._attr_unique_id = coordinator.unique_id
self._attr_device_info = coordinator.device_info

View File

@ -5,23 +5,37 @@ from unittest.mock import patch
from homeassistant.components.enigma2.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from .conftest import TEST_REQUIRED, MockDevice
from tests.common import MockConfigEntry
async def test_device_without_mac_address(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that a device gets successfully registered when the device doesn't report a MAC address."""
mock_device = MockDevice()
mock_device.mac_address = None
with patch(
"homeassistant.components.enigma2.coordinator.OpenWebIfDevice.__new__",
return_value=mock_device,
):
entry = MockConfigEntry(
domain=DOMAIN, data=TEST_REQUIRED, title="name", unique_id="123456"
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert device_registry.async_get_device({(DOMAIN, entry.unique_id)}) is not None
async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test successful unload of entry."""
with (
patch(
"homeassistant.components.enigma2.coordinator.OpenWebIfDevice.__new__",
return_value=MockDevice(),
),
patch(
"homeassistant.components.enigma2.media_player.async_setup_entry",
return_value=True,
),
with patch(
"homeassistant.components.enigma2.coordinator.OpenWebIfDevice.__new__",
return_value=MockDevice(),
):
entry = MockConfigEntry(domain=DOMAIN, data=TEST_REQUIRED, title="name")
entry.add_to_hass(hass)