mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Use entry ID when IPP printer offers no identifier (#34316)
This commit is contained in:
parent
f3c6f665af
commit
b4083dc14f
@ -128,24 +128,20 @@ class IPPEntity(Entity):
|
|||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
|
device_id: str,
|
||||||
coordinator: IPPDataUpdateCoordinator,
|
coordinator: IPPDataUpdateCoordinator,
|
||||||
name: str,
|
name: str,
|
||||||
icon: str,
|
icon: str,
|
||||||
enabled_default: bool = True,
|
enabled_default: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the IPP entity."""
|
"""Initialize the IPP entity."""
|
||||||
self._device_id = None
|
self._device_id = device_id
|
||||||
self._enabled_default = enabled_default
|
self._enabled_default = enabled_default
|
||||||
self._entry_id = entry_id
|
self._entry_id = entry_id
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._name = name
|
self._name = name
|
||||||
self.coordinator = coordinator
|
self.coordinator = coordinator
|
||||||
|
|
||||||
if coordinator.data.info.uuid is not None:
|
|
||||||
self._device_id = coordinator.data.info.uuid
|
|
||||||
elif coordinator.data.info.serial is not None:
|
|
||||||
self._device_id = coordinator.data.info.serial
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the entity."""
|
"""Return the name of the entity."""
|
||||||
|
@ -32,13 +32,21 @@ async def async_setup_entry(
|
|||||||
"""Set up IPP sensor based on a config entry."""
|
"""Set up IPP sensor based on a config entry."""
|
||||||
coordinator: IPPDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: IPPDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
|
# config flow sets this to either UUID, serial number or None
|
||||||
|
unique_id = entry.unique_id
|
||||||
|
|
||||||
|
if unique_id is None:
|
||||||
|
unique_id = entry.entry_id
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
|
|
||||||
sensors.append(IPPPrinterSensor(entry.entry_id, coordinator))
|
sensors.append(IPPPrinterSensor(entry.entry_id, unique_id, coordinator))
|
||||||
sensors.append(IPPUptimeSensor(entry.entry_id, coordinator))
|
sensors.append(IPPUptimeSensor(entry.entry_id, unique_id, coordinator))
|
||||||
|
|
||||||
for marker_index in range(len(coordinator.data.markers)):
|
for marker_index in range(len(coordinator.data.markers)):
|
||||||
sensors.append(IPPMarkerSensor(entry.entry_id, coordinator, marker_index))
|
sensors.append(
|
||||||
|
IPPMarkerSensor(entry.entry_id, unique_id, coordinator, marker_index)
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
async_add_entities(sensors, True)
|
||||||
|
|
||||||
@ -52,6 +60,7 @@ class IPPSensor(IPPEntity):
|
|||||||
coordinator: IPPDataUpdateCoordinator,
|
coordinator: IPPDataUpdateCoordinator,
|
||||||
enabled_default: bool = True,
|
enabled_default: bool = True,
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
|
unique_id: str,
|
||||||
icon: str,
|
icon: str,
|
||||||
key: str,
|
key: str,
|
||||||
name: str,
|
name: str,
|
||||||
@ -62,13 +71,12 @@ class IPPSensor(IPPEntity):
|
|||||||
self._key = key
|
self._key = key
|
||||||
self._unique_id = None
|
self._unique_id = None
|
||||||
|
|
||||||
if coordinator.data.info.uuid is not None:
|
if unique_id is not None:
|
||||||
self._unique_id = f"{coordinator.data.info.uuid}_{key}"
|
self._unique_id = f"{unique_id}_{key}"
|
||||||
elif coordinator.data.info.serial is not None:
|
|
||||||
self._unique_id = f"{coordinator.data.info.serial}_{key}"
|
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
entry_id=entry_id,
|
entry_id=entry_id,
|
||||||
|
device_id=unique_id,
|
||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
name=name,
|
name=name,
|
||||||
icon=icon,
|
icon=icon,
|
||||||
@ -90,7 +98,11 @@ class IPPMarkerSensor(IPPSensor):
|
|||||||
"""Defines an IPP marker sensor."""
|
"""Defines an IPP marker sensor."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, entry_id: str, coordinator: IPPDataUpdateCoordinator, marker_index: int
|
self,
|
||||||
|
entry_id: str,
|
||||||
|
unique_id: str,
|
||||||
|
coordinator: IPPDataUpdateCoordinator,
|
||||||
|
marker_index: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize IPP marker sensor."""
|
"""Initialize IPP marker sensor."""
|
||||||
self.marker_index = marker_index
|
self.marker_index = marker_index
|
||||||
@ -98,6 +110,7 @@ class IPPMarkerSensor(IPPSensor):
|
|||||||
super().__init__(
|
super().__init__(
|
||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
entry_id=entry_id,
|
entry_id=entry_id,
|
||||||
|
unique_id=unique_id,
|
||||||
icon="mdi:water",
|
icon="mdi:water",
|
||||||
key=f"marker_{marker_index}",
|
key=f"marker_{marker_index}",
|
||||||
name=f"{coordinator.data.info.name} {coordinator.data.markers[marker_index].name}",
|
name=f"{coordinator.data.info.name} {coordinator.data.markers[marker_index].name}",
|
||||||
@ -133,11 +146,14 @@ class IPPMarkerSensor(IPPSensor):
|
|||||||
class IPPPrinterSensor(IPPSensor):
|
class IPPPrinterSensor(IPPSensor):
|
||||||
"""Defines an IPP printer sensor."""
|
"""Defines an IPP printer sensor."""
|
||||||
|
|
||||||
def __init__(self, entry_id: str, coordinator: IPPDataUpdateCoordinator) -> None:
|
def __init__(
|
||||||
|
self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator
|
||||||
|
) -> None:
|
||||||
"""Initialize IPP printer sensor."""
|
"""Initialize IPP printer sensor."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
entry_id=entry_id,
|
entry_id=entry_id,
|
||||||
|
unique_id=unique_id,
|
||||||
icon="mdi:printer",
|
icon="mdi:printer",
|
||||||
key="printer",
|
key="printer",
|
||||||
name=coordinator.data.info.name,
|
name=coordinator.data.info.name,
|
||||||
@ -166,12 +182,15 @@ class IPPPrinterSensor(IPPSensor):
|
|||||||
class IPPUptimeSensor(IPPSensor):
|
class IPPUptimeSensor(IPPSensor):
|
||||||
"""Defines a IPP uptime sensor."""
|
"""Defines a IPP uptime sensor."""
|
||||||
|
|
||||||
def __init__(self, entry_id: str, coordinator: IPPDataUpdateCoordinator) -> None:
|
def __init__(
|
||||||
|
self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator
|
||||||
|
) -> None:
|
||||||
"""Initialize IPP uptime sensor."""
|
"""Initialize IPP uptime sensor."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
enabled_default=False,
|
enabled_default=False,
|
||||||
entry_id=entry_id,
|
entry_id=entry_id,
|
||||||
|
unique_id=unique_id,
|
||||||
icon="mdi:clock-outline",
|
icon="mdi:clock-outline",
|
||||||
key="uptime",
|
key="uptime",
|
||||||
name=f"{coordinator.data.info.name} Uptime",
|
name=f"{coordinator.data.info.name} Uptime",
|
||||||
|
@ -62,7 +62,11 @@ def load_fixture_binary(filename):
|
|||||||
|
|
||||||
|
|
||||||
async def init_integration(
|
async def init_integration(
|
||||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, skip_setup: bool = False,
|
hass: HomeAssistant,
|
||||||
|
aioclient_mock: AiohttpClientMocker,
|
||||||
|
skip_setup: bool = False,
|
||||||
|
uuid: str = "cfe92100-67c4-11d4-a45f-f8d027761251",
|
||||||
|
unique_id: str = "cfe92100-67c4-11d4-a45f-f8d027761251",
|
||||||
) -> MockConfigEntry:
|
) -> MockConfigEntry:
|
||||||
"""Set up the IPP integration in Home Assistant."""
|
"""Set up the IPP integration in Home Assistant."""
|
||||||
fixture = "ipp/get-printer-attributes.bin"
|
fixture = "ipp/get-printer-attributes.bin"
|
||||||
@ -74,14 +78,14 @@ async def init_integration(
|
|||||||
|
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
unique_id="cfe92100-67c4-11d4-a45f-f8d027761251",
|
unique_id=unique_id,
|
||||||
data={
|
data={
|
||||||
CONF_HOST: "192.168.1.31",
|
CONF_HOST: "192.168.1.31",
|
||||||
CONF_PORT: 631,
|
CONF_PORT: 631,
|
||||||
CONF_SSL: False,
|
CONF_SSL: False,
|
||||||
CONF_VERIFY_SSL: True,
|
CONF_VERIFY_SSL: True,
|
||||||
CONF_BASE_PATH: "/ipp/print",
|
CONF_BASE_PATH: "/ipp/print",
|
||||||
CONF_UUID: "cfe92100-67c4-11d4-a45f-f8d027761251",
|
CONF_UUID: uuid,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -94,3 +94,15 @@ async def test_disabled_by_default_sensors(
|
|||||||
assert entry
|
assert entry
|
||||||
assert entry.disabled
|
assert entry.disabled
|
||||||
assert entry.disabled_by == "integration"
|
assert entry.disabled_by == "integration"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_missing_entry_unique_id(
|
||||||
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
|
) -> None:
|
||||||
|
"""Test the unique_id of IPP sensor when printer is missing identifiers."""
|
||||||
|
entry = await init_integration(hass, aioclient_mock, uuid=None, unique_id=None)
|
||||||
|
registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
|
|
||||||
|
entity = registry.async_get("sensor.epson_xp_6000_series")
|
||||||
|
assert entity
|
||||||
|
assert entity.unique_id == f"{entry.entry_id}_printer"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user