Fix for rainbird unique id (#101512)

pull/101547/head
Allen Porter 2023-10-06 00:16:06 -07:00 committed by Franck Nijhof
parent d469626855
commit d14934861e
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
5 changed files with 40 additions and 7 deletions

View File

@ -61,7 +61,7 @@ class RainBirdCalendarEntity(
"""Create the Calendar event device."""
super().__init__(coordinator)
self._event: CalendarEvent | None = None
if unique_id:
if unique_id is not None:
self._attr_unique_id = unique_id
self._attr_device_info = device_info
else:

View File

@ -51,7 +51,7 @@ class RainDelayNumber(CoordinatorEntity[RainbirdUpdateCoordinator], NumberEntity
) -> None:
"""Initialize the Rain Bird sensor."""
super().__init__(coordinator)
if coordinator.unique_id:
if coordinator.unique_id is not None:
self._attr_unique_id = f"{coordinator.unique_id}-rain-delay"
self._attr_device_info = coordinator.device_info
else:

View File

@ -52,7 +52,7 @@ class RainBirdSensor(CoordinatorEntity[RainbirdUpdateCoordinator], SensorEntity)
"""Initialize the Rain Bird sensor."""
super().__init__(coordinator)
self.entity_description = description
if coordinator.unique_id:
if coordinator.unique_id is not None:
self._attr_unique_id = f"{coordinator.unique_id}-{description.key}"
self._attr_device_info = coordinator.device_info
else:

View File

@ -65,17 +65,18 @@ class RainBirdSwitch(CoordinatorEntity[RainbirdUpdateCoordinator], SwitchEntity)
"""Initialize a Rain Bird Switch Device."""
super().__init__(coordinator)
self._zone = zone
if coordinator.unique_id:
_LOGGER.debug("coordinator.unique_id=%s", coordinator.unique_id)
if coordinator.unique_id is not None:
self._attr_unique_id = f"{coordinator.unique_id}-{zone}"
device_name = f"{MANUFACTURER} Sprinkler {zone}"
if imported_name:
self._attr_name = imported_name
self._attr_has_entity_name = False
else:
self._attr_name = None if coordinator.unique_id else device_name
self._attr_name = None if coordinator.unique_id is not None else device_name
self._attr_has_entity_name = True
self._duration_minutes = duration_minutes
if coordinator.unique_id and self._attr_unique_id:
if coordinator.unique_id is not None and self._attr_unique_id is not None:
self._attr_device_info = DeviceInfo(
name=device_name,
identifiers={(DOMAIN, self._attr_unique_id)},

View File

@ -17,6 +17,7 @@ from .conftest import (
PASSWORD,
RAIN_DELAY_OFF,
RAIN_SENSOR_OFF,
SERIAL_NUMBER,
ZONE_3_ON_RESPONSE,
ZONE_5_ON_RESPONSE,
ZONE_OFF_RESPONSE,
@ -286,7 +287,7 @@ async def test_switch_error(
@pytest.mark.parametrize(
("config_entry_unique_id"),
[
None,
(None),
],
)
async def test_no_unique_id(
@ -307,3 +308,34 @@ async def test_no_unique_id(
entity_entry = entity_registry.async_get("switch.rain_bird_sprinkler_3")
assert entity_entry is None
@pytest.mark.parametrize(
("config_entry_unique_id", "entity_unique_id"),
[
(SERIAL_NUMBER, "1263613994342-3"),
# Some existing config entries may have a "0" serial number but preserve
# their unique id
(0, "0-3"),
],
)
async def test_has_unique_id(
hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse],
entity_registry: er.EntityRegistry,
entity_unique_id: str,
) -> None:
"""Test an irrigation switch with no unique id."""
assert await setup_integration()
zone = hass.states.get("switch.rain_bird_sprinkler_3")
assert zone is not None
assert zone.attributes.get("friendly_name") == "Rain Bird Sprinkler 3"
assert zone.state == "off"
entity_entry = entity_registry.async_get("switch.rain_bird_sprinkler_3")
assert entity_entry
assert entity_entry.unique_id == entity_unique_id