Add unique ID for generic hygrostat (#101503)

pull/101549/head
Justin Grover 2023-10-06 12:15:40 -06:00 committed by GitHub
parent 9ac5bdc832
commit 475cb7719b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 2 deletions

View File

@ -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,
}
)

View File

@ -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."""

View File

@ -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)

View File

@ -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)