From 475cb7719b773dbf28097e5247946947ab9c66ea Mon Sep 17 00:00:00 2001 From: Justin Grover Date: Fri, 6 Oct 2023 12:15:40 -0600 Subject: [PATCH] Add unique ID for generic hygrostat (#101503) --- .../components/generic_hygrostat/__init__.py | 4 ++- .../generic_hygrostat/humidifier.py | 5 ++++ .../generic_hygrostat/test_humidifier.py | 28 +++++++++++++++++++ .../generic_thermostat/test_climate.py | 2 +- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/generic_hygrostat/__init__.py b/homeassistant/components/generic_hygrostat/__init__.py index 0821e8798c7..585d0aa1fe3 100644 --- a/homeassistant/components/generic_hygrostat/__init__.py +++ b/homeassistant/components/generic_hygrostat/__init__.py @@ -2,7 +2,7 @@ import voluptuous as vol from homeassistant.components.humidifier import HumidifierDeviceClass -from homeassistant.const import CONF_NAME, Platform +from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.typing import ConfigType @@ -24,6 +24,7 @@ CONF_AWAY_HUMIDITY = "away_humidity" CONF_AWAY_FIXED = "away_fixed" CONF_STALE_DURATION = "sensor_stale_duration" + DEFAULT_TOLERANCE = 3 DEFAULT_NAME = "Generic Hygrostat" @@ -48,6 +49,7 @@ HYGROSTAT_SCHEMA = vol.Schema( vol.Optional(CONF_STALE_DURATION): vol.All( cv.time_period, cv.positive_timedelta ), + vol.Optional(CONF_UNIQUE_ID): cv.string, } ) diff --git a/homeassistant/components/generic_hygrostat/humidifier.py b/homeassistant/components/generic_hygrostat/humidifier.py index 959b0a8e8df..3bdecbfa997 100644 --- a/homeassistant/components/generic_hygrostat/humidifier.py +++ b/homeassistant/components/generic_hygrostat/humidifier.py @@ -18,6 +18,7 @@ from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_MODE, CONF_NAME, + CONF_UNIQUE_ID, EVENT_HOMEASSISTANT_START, SERVICE_TURN_OFF, SERVICE_TURN_ON, @@ -86,6 +87,7 @@ async def async_setup_platform( initial_state = config.get(CONF_INITIAL_STATE) away_humidity = config.get(CONF_AWAY_HUMIDITY) away_fixed = config.get(CONF_AWAY_FIXED) + unique_id = config.get(CONF_UNIQUE_ID) async_add_entities( [ @@ -105,6 +107,7 @@ async def async_setup_platform( away_humidity, away_fixed, sensor_stale_duration, + unique_id, ) ] ) @@ -132,6 +135,7 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity): away_humidity, away_fixed, sensor_stale_duration, + unique_id, ): """Initialize the hygrostat.""" self._name = name @@ -160,6 +164,7 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity): if not self._device_class: self._device_class = HumidifierDeviceClass.HUMIDIFIER self._attr_action = HumidifierAction.IDLE + self._attr_unique_id = unique_id async def async_added_to_hass(self): """Run when entity about to be added.""" diff --git a/tests/components/generic_hygrostat/test_humidifier.py b/tests/components/generic_hygrostat/test_humidifier.py index e3fb26ffe22..bd97a683989 100644 --- a/tests/components/generic_hygrostat/test_humidifier.py +++ b/tests/components/generic_hygrostat/test_humidifier.py @@ -31,6 +31,7 @@ from homeassistant.core import ( State, callback, ) +from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -169,6 +170,33 @@ async def test_humidifier_switch( assert hass.states.get(ENTITY).attributes.get("action") == "humidifying" +async def test_unique_id(hass: HomeAssistant, setup_comp_1) -> None: + """Test setting a unique ID.""" + unique_id = "some_unique_id" + _setup_sensor(hass, 18) + await _setup_switch(hass, True) + assert await async_setup_component( + hass, + DOMAIN, + { + "humidifier": { + "platform": "generic_hygrostat", + "name": "test", + "humidifier": ENT_SWITCH, + "target_sensor": ENT_SENSOR, + "unique_id": unique_id, + } + }, + ) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + + entry = entity_registry.async_get(ENTITY) + assert entry + assert entry.unique_id == unique_id + + def _setup_sensor(hass, humidity): """Set up the test sensor.""" hass.states.async_set(ENT_SENSOR, humidity) diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index 4eb2e3ce711..2a406ddbd79 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -174,7 +174,7 @@ async def test_heater_switch( async def test_unique_id(hass: HomeAssistant, setup_comp_1) -> None: - """Test heater switching input_boolean.""" + """Test setting a unique ID.""" unique_id = "some_unique_id" _setup_sensor(hass, 18) _setup_switch(hass, True)