mirror of
https://github.com/home-assistant/core.git
synced 2025-04-29 11:47:50 +00:00
Fix enigma2 integration for devices not reporting MAC address (#133226)
This commit is contained in:
parent
f8da2c3e5c
commit
412aa60e8f
@ -133,7 +133,8 @@ class Enigma2ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
except Exception: # noqa: BLE001
|
except Exception: # noqa: BLE001
|
||||||
errors = {"base": "unknown"}
|
errors = {"base": "unknown"}
|
||||||
else:
|
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()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
return errors
|
return errors
|
||||||
|
@ -35,6 +35,7 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
|
|||||||
"""The Enigma2 data update coordinator."""
|
"""The Enigma2 data update coordinator."""
|
||||||
|
|
||||||
device: OpenWebIfDevice
|
device: OpenWebIfDevice
|
||||||
|
unique_id: str | None
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||||
"""Initialize the Enigma2 data update coordinator."""
|
"""Initialize the Enigma2 data update coordinator."""
|
||||||
@ -64,6 +65,10 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
|
|||||||
name=config_entry.data[CONF_HOST],
|
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:
|
async def _async_setup(self) -> None:
|
||||||
"""Provide needed data to the device info."""
|
"""Provide needed data to the device info."""
|
||||||
|
|
||||||
@ -71,6 +76,7 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
|
|||||||
self.device.mac_address = about["info"]["ifaces"][0]["mac"]
|
self.device.mac_address = about["info"]["ifaces"][0]["mac"]
|
||||||
self.device_info["model"] = about["info"]["model"]
|
self.device_info["model"] = about["info"]["model"]
|
||||||
self.device_info["manufacturer"] = about["info"]["brand"]
|
self.device_info["manufacturer"] = about["info"]["brand"]
|
||||||
|
if self.device.mac_address is not None:
|
||||||
self.device_info[ATTR_IDENTIFIERS] = {
|
self.device_info[ATTR_IDENTIFIERS] = {
|
||||||
(DOMAIN, format_mac(iface["mac"]))
|
(DOMAIN, format_mac(iface["mac"]))
|
||||||
for iface in about["info"]["ifaces"]
|
for iface in about["info"]["ifaces"]
|
||||||
@ -81,6 +87,9 @@ class Enigma2UpdateCoordinator(DataUpdateCoordinator[OpenWebIfStatus]):
|
|||||||
for iface in about["info"]["ifaces"]
|
for iface in about["info"]["ifaces"]
|
||||||
if "mac" in iface and iface["mac"] is not None
|
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:
|
async def _async_update_data(self) -> OpenWebIfStatus:
|
||||||
await self.device.update()
|
await self.device.update()
|
||||||
|
@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from typing import cast
|
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ServerDisconnectedError
|
from aiohttp.client_exceptions import ServerDisconnectedError
|
||||||
from openwebif.enums import PowerState, RemoteControlCodes, SetVolumeOption
|
from openwebif.enums import PowerState, RemoteControlCodes, SetVolumeOption
|
||||||
@ -15,7 +14,6 @@ from homeassistant.components.media_player import (
|
|||||||
MediaPlayerState,
|
MediaPlayerState,
|
||||||
MediaType,
|
MediaType,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
@ -65,10 +63,7 @@ class Enigma2Device(CoordinatorEntity[Enigma2UpdateCoordinator], MediaPlayerEnti
|
|||||||
|
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = coordinator.unique_id
|
||||||
coordinator.device.mac_address
|
|
||||||
or cast(ConfigEntry, coordinator.config_entry).entry_id
|
|
||||||
)
|
|
||||||
|
|
||||||
self._attr_device_info = coordinator.device_info
|
self._attr_device_info = coordinator.device_info
|
||||||
|
|
||||||
|
@ -5,23 +5,37 @@ from unittest.mock import patch
|
|||||||
from homeassistant.components.enigma2.const import DOMAIN
|
from homeassistant.components.enigma2.const import DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
from .conftest import TEST_REQUIRED, MockDevice
|
from .conftest import TEST_REQUIRED, MockDevice
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
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:
|
async def test_unload_entry(hass: HomeAssistant) -> None:
|
||||||
"""Test successful unload of entry."""
|
"""Test successful unload of entry."""
|
||||||
with (
|
with patch(
|
||||||
patch(
|
|
||||||
"homeassistant.components.enigma2.coordinator.OpenWebIfDevice.__new__",
|
"homeassistant.components.enigma2.coordinator.OpenWebIfDevice.__new__",
|
||||||
return_value=MockDevice(),
|
return_value=MockDevice(),
|
||||||
),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.enigma2.media_player.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
),
|
|
||||||
):
|
):
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=TEST_REQUIRED, title="name")
|
entry = MockConfigEntry(domain=DOMAIN, data=TEST_REQUIRED, title="name")
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user