diff --git a/homeassistant/components/rainbird/binary_sensor.py b/homeassistant/components/rainbird/binary_sensor.py index 3333d8bc4cb..142e8ecc4b8 100644 --- a/homeassistant/components/rainbird/binary_sensor.py +++ b/homeassistant/components/rainbird/binary_sensor.py @@ -48,7 +48,7 @@ class RainBirdSensor(CoordinatorEntity[RainbirdUpdateCoordinator], BinarySensorE """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: diff --git a/homeassistant/components/rainbird/coordinator.py b/homeassistant/components/rainbird/coordinator.py index 763e50fe5d9..9f1ea95b333 100644 --- a/homeassistant/components/rainbird/coordinator.py +++ b/homeassistant/components/rainbird/coordinator.py @@ -84,7 +84,7 @@ class RainbirdUpdateCoordinator(DataUpdateCoordinator[RainbirdDeviceState]): @property def device_info(self) -> DeviceInfo | None: """Return information about the device.""" - if not self._unique_id: + if self._unique_id is None: return None return DeviceInfo( name=self.device_name, diff --git a/tests/components/rainbird/test_binary_sensor.py b/tests/components/rainbird/test_binary_sensor.py index e372a10ae23..24cd1750ed4 100644 --- a/tests/components/rainbird/test_binary_sensor.py +++ b/tests/components/rainbird/test_binary_sensor.py @@ -7,7 +7,7 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from .conftest import RAIN_SENSOR_OFF, RAIN_SENSOR_ON, ComponentSetup +from .conftest import RAIN_SENSOR_OFF, RAIN_SENSOR_ON, SERIAL_NUMBER, ComponentSetup from tests.test_util.aiohttp import AiohttpClientMockResponse @@ -41,11 +41,38 @@ async def test_rainsensor( "icon": "mdi:water", } + +@pytest.mark.parametrize( + ("config_entry_unique_id", "entity_unique_id"), + [ + (SERIAL_NUMBER, "1263613994342-rainsensor"), + # Some existing config entries may have a "0" serial number but preserve + # their unique id + (0, "0-rainsensor"), + ], +) +async def test_unique_id( + hass: HomeAssistant, + setup_integration: ComponentSetup, + entity_registry: er.EntityRegistry, + entity_unique_id: str, +) -> None: + """Test rainsensor binary sensor.""" + + assert await setup_integration() + + rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor") + assert rainsensor is not None + assert rainsensor.attributes == { + "friendly_name": "Rain Bird Controller Rainsensor", + "icon": "mdi:water", + } + entity_entry = entity_registry.async_get( "binary_sensor.rain_bird_controller_rainsensor" ) assert entity_entry - assert entity_entry.unique_id == "1263613994342-rainsensor" + assert entity_entry.unique_id == entity_unique_id @pytest.mark.parametrize( diff --git a/tests/components/rainbird/test_number.py b/tests/components/rainbird/test_number.py index 5d208f08a25..b3cfd56832d 100644 --- a/tests/components/rainbird/test_number.py +++ b/tests/components/rainbird/test_number.py @@ -63,6 +63,36 @@ async def test_number_values( assert entity_entry.unique_id == "1263613994342-rain-delay" +@pytest.mark.parametrize( + ("config_entry_unique_id", "entity_unique_id"), + [ + (SERIAL_NUMBER, "1263613994342-rain-delay"), + # Some existing config entries may have a "0" serial number but preserve + # their unique id + (0, "0-rain-delay"), + ], +) +async def test_unique_id( + hass: HomeAssistant, + setup_integration: ComponentSetup, + entity_registry: er.EntityRegistry, + entity_unique_id: str, +) -> None: + """Test number platform.""" + + assert await setup_integration() + + raindelay = hass.states.get("number.rain_bird_controller_rain_delay") + assert raindelay is not None + assert ( + raindelay.attributes.get("friendly_name") == "Rain Bird Controller Rain delay" + ) + + entity_entry = entity_registry.async_get("number.rain_bird_controller_rain_delay") + assert entity_entry + assert entity_entry.unique_id == entity_unique_id + + async def test_set_value( hass: HomeAssistant, setup_integration: ComponentSetup,