Add generic_thermostat unique ID parameter (#46399)

* Add generic_thermostat unique ID parameter

* Add tests for unique id

* Fix flake8
This commit is contained in:
Czapla 2021-02-11 20:46:58 +01:00 committed by GitHub
parent 26e7916367
commit 14a64ea970
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -23,6 +23,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_TEMPERATURE,
CONF_NAME,
CONF_UNIQUE_ID,
EVENT_HOMEASSISTANT_START,
PRECISION_HALVES,
PRECISION_TENTHS,
@ -85,6 +86,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Optional(CONF_PRECISION): vol.In(
[PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE]
),
vol.Optional(CONF_UNIQUE_ID): cv.string,
}
)
@ -109,6 +111,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
away_temp = config.get(CONF_AWAY_TEMP)
precision = config.get(CONF_PRECISION)
unit = hass.config.units.temperature_unit
unique_id = config.get(CONF_UNIQUE_ID)
async_add_entities(
[
@ -128,6 +131,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
away_temp,
precision,
unit,
unique_id,
)
]
)
@ -153,6 +157,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
away_temp,
precision,
unit,
unique_id,
):
"""Initialize the thermostat."""
self._name = name
@ -177,6 +182,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
self._max_temp = max_temp
self._target_temp = target_temp
self._unit = unit
self._unique_id = unique_id
self._support_flags = SUPPORT_FLAGS
if away_temp:
self._support_flags = SUPPORT_FLAGS | SUPPORT_PRESET_MODE
@ -269,6 +275,11 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
"""Return the name of the thermostat."""
return self._name
@property
def unique_id(self):
"""Return the unique id of this thermostat."""
return self._unique_id
@property
def precision(self):
"""Return the precision of the system."""

View file

@ -159,6 +159,33 @@ async def test_heater_switch(hass, setup_comp_1):
assert STATE_ON == hass.states.get(heater_switch).state
async def test_unique_id(hass, setup_comp_1):
"""Test heater switching input_boolean."""
unique_id = "some_unique_id"
_setup_sensor(hass, 18)
_setup_switch(hass, True)
assert await async_setup_component(
hass,
DOMAIN,
{
"climate": {
"platform": "generic_thermostat",
"name": "test",
"heater": ENT_SWITCH,
"target_sensor": ENT_SENSOR,
"unique_id": unique_id,
}
},
)
await hass.async_block_till_done()
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entry = entity_registry.async_get(ENTITY)
assert entry
assert entry.unique_id == unique_id
def _setup_sensor(hass, temp):
"""Set up the test sensor."""
hass.states.async_set(ENT_SENSOR, temp)