Add climate device triggers (#28544)
* Add climate device triggers * Test capabilities
This commit is contained in:
1 parent
af73e54aee
commit
48660585f1
10 files changed
+470
-28
No files matched your search
@@ -0,0 +1,243 @@
|
||||
"""The tests for Climate device triggers."""
|
||||
import voluptuous_serialize
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.climate import DOMAIN, const, device_trigger
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.helpers import device_registry, config_validation as cv
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
assert_lists_same,
|
||||
async_mock_service,
|
||||
mock_device_registry,
|
||||
mock_registry,
|
||||
async_get_device_automations,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def device_reg(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
return mock_device_registry(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def entity_reg(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
return mock_registry(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock serivce."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
async def test_get_triggers(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected triggers from a climate device."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||
entity_id = f"{DOMAIN}.test_5678"
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
const.HVAC_MODE_COOL,
|
||||
{
|
||||
const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_IDLE,
|
||||
const.ATTR_CURRENT_HUMIDITY: 23,
|
||||
const.ATTR_CURRENT_TEMPERATURE: 18,
|
||||
},
|
||||
)
|
||||
expected_triggers = [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "hvac_mode_changed",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_id,
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_temperature_changed",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_id,
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_humidity_changed",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_id,
|
||||
},
|
||||
]
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert_lists_same(triggers, expected_triggers)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change(hass, calls):
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
hass.states.async_set(
|
||||
"climate.entity",
|
||||
const.HVAC_MODE_COOL,
|
||||
{
|
||||
const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_IDLE,
|
||||
const.ATTR_CURRENT_HUMIDITY: 23,
|
||||
const.ATTR_CURRENT_TEMPERATURE: 18,
|
||||
},
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "climate.entity",
|
||||
"type": "hvac_mode_changed",
|
||||
"to": const.HVAC_MODE_AUTO,
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {"some": "hvac_mode_changed"},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "climate.entity",
|
||||
"type": "current_temperature_changed",
|
||||
"above": 20,
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {"some": "current_temperature_changed"},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": "climate.entity",
|
||||
"type": "current_humidity_changed",
|
||||
"below": 10,
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {"some": "current_humidity_changed"},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Fake that the HVAC mode is changing
|
||||
hass.states.async_set(
|
||||
"climate.entity",
|
||||
const.HVAC_MODE_AUTO,
|
||||
{
|
||||
const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_COOL,
|
||||
const.ATTR_CURRENT_HUMIDITY: 23,
|
||||
const.ATTR_CURRENT_TEMPERATURE: 18,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "hvac_mode_changed"
|
||||
|
||||
# Fake that the temperature is changing
|
||||
hass.states.async_set(
|
||||
"climate.entity",
|
||||
const.HVAC_MODE_AUTO,
|
||||
{
|
||||
const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_COOL,
|
||||
const.ATTR_CURRENT_HUMIDITY: 23,
|
||||
const.ATTR_CURRENT_TEMPERATURE: 23,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == "current_temperature_changed"
|
||||
|
||||
# Fake that the humidity is changing
|
||||
hass.states.async_set(
|
||||
"climate.entity",
|
||||
const.HVAC_MODE_AUTO,
|
||||
{
|
||||
const.ATTR_HVAC_ACTION: const.CURRENT_HVAC_COOL,
|
||||
const.ATTR_CURRENT_HUMIDITY: 7,
|
||||
const.ATTR_CURRENT_TEMPERATURE: 23,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 3
|
||||
assert calls[2].data["some"] == "current_humidity_changed"
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities_hvac_mode(hass):
|
||||
"""Test we get the expected capabilities from a climate trigger."""
|
||||
capabilities = await device_trigger.async_get_trigger_capabilities(
|
||||
hass,
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": "climate",
|
||||
"type": "hvac_mode_changed",
|
||||
"entity_id": "climate.upstairs",
|
||||
"to": "heat",
|
||||
},
|
||||
)
|
||||
assert capabilities and "extra_fields" in capabilities
|
||||
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [{"name": "for", "optional": True, "type": "positive_time_period_dict"}]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"type", ["current_temperature_changed", "current_humidity_changed"]
|
||||
)
|
||||
async def test_get_trigger_capabilities_temp_humid(hass, type):
|
||||
"""Test we get the expected capabilities from a climate trigger."""
|
||||
capabilities = await device_trigger.async_get_trigger_capabilities(
|
||||
hass,
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": "climate",
|
||||
"type": "current_temperature_changed",
|
||||
"entity_id": "climate.upstairs",
|
||||
"above": "23",
|
||||
},
|
||||
)
|
||||
|
||||
assert capabilities and "extra_fields" in capabilities
|
||||
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"description": {"suffix": "°C"},
|
||||
"name": "above",
|
||||
"optional": True,
|
||||
"type": "float",
|
||||
},
|
||||
{
|
||||
"description": {"suffix": "°C"},
|
||||
"name": "below",
|
||||
"optional": True,
|
||||
"type": "float",
|
||||
},
|
||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"},
|
||||
]
|
||||
@@ -9,7 +9,7 @@ from homeassistant.components.climate.const import (
|
||||
ATTR_CURRENT_TEMPERATURE,
|
||||
ATTR_FAN_MODE,
|
||||
ATTR_HUMIDITY,
|
||||
ATTR_HVAC_ACTIONS,
|
||||
ATTR_HVAC_ACTION,
|
||||
ATTR_HVAC_MODES,
|
||||
ATTR_MAX_HUMIDITY,
|
||||
ATTR_MAX_TEMP,
|
||||
@@ -233,7 +233,7 @@ async def test_set_hvac_bad_attr_and_state(hass):
|
||||
Also check the state.
|
||||
"""
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.attributes.get(ATTR_HVAC_ACTIONS) == CURRENT_HVAC_COOL
|
||||
assert state.attributes.get(ATTR_HVAC_ACTION) == CURRENT_HVAC_COOL
|
||||
assert state.state == HVAC_MODE_COOL
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
@@ -241,7 +241,7 @@ async def test_set_hvac_bad_attr_and_state(hass):
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.attributes.get(ATTR_HVAC_ACTIONS) == CURRENT_HVAC_COOL
|
||||
assert state.attributes.get(ATTR_HVAC_ACTION) == CURRENT_HVAC_COOL
|
||||
assert state.state == HVAC_MODE_COOL
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import pytest
|
||||
|
||||
from homeassistant.components.climate.const import (
|
||||
ATTR_CURRENT_TEMPERATURE,
|
||||
ATTR_HVAC_ACTIONS,
|
||||
ATTR_HVAC_ACTION,
|
||||
ATTR_HVAC_MODE,
|
||||
ATTR_HVAC_MODES,
|
||||
ATTR_MAX_TEMP,
|
||||
@@ -92,7 +92,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
{
|
||||
ATTR_TEMPERATURE: 22.2,
|
||||
ATTR_CURRENT_TEMPERATURE: 17.8,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_HEAT,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_HEAT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -108,7 +108,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
{
|
||||
ATTR_TEMPERATURE: 22.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 23.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_IDLE,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_IDLE,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -124,7 +124,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
{
|
||||
ATTR_TEMPERATURE: 20.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 25.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_COOL,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_COOL,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -140,7 +140,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
{
|
||||
ATTR_TEMPERATURE: 20.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 19.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_IDLE,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_IDLE,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -169,7 +169,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
ATTR_HVAC_MODES: [HVAC_MODE_HEAT, HVAC_MODE_COOL],
|
||||
ATTR_TEMPERATURE: 22.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 18.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_HEAT,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_HEAT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -186,7 +186,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
ATTR_HVAC_MODES: [HVAC_MODE_HEAT, HVAC_MODE_COOL],
|
||||
ATTR_TEMPERATURE: 22.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 25.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_COOL,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_COOL,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -203,7 +203,7 @@ async def test_thermostat(hass, hk_driver, cls, events):
|
||||
ATTR_HVAC_MODES: [HVAC_MODE_HEAT, HVAC_MODE_COOL],
|
||||
ATTR_TEMPERATURE: 22.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 22.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_IDLE,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_IDLE,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -265,7 +265,7 @@ async def test_thermostat_auto(hass, hk_driver, cls, events):
|
||||
ATTR_TARGET_TEMP_HIGH: 22.0,
|
||||
ATTR_TARGET_TEMP_LOW: 20.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 18.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_HEAT,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_HEAT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -284,7 +284,7 @@ async def test_thermostat_auto(hass, hk_driver, cls, events):
|
||||
ATTR_TARGET_TEMP_HIGH: 23.0,
|
||||
ATTR_TARGET_TEMP_LOW: 19.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 24.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_COOL,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_COOL,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -303,7 +303,7 @@ async def test_thermostat_auto(hass, hk_driver, cls, events):
|
||||
ATTR_TARGET_TEMP_HIGH: 23.0,
|
||||
ATTR_TARGET_TEMP_LOW: 19.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 21.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_IDLE,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_IDLE,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -349,7 +349,7 @@ async def test_thermostat_power_state(hass, hk_driver, cls, events):
|
||||
ATTR_HVAC_MODE: HVAC_MODE_HEAT,
|
||||
ATTR_TEMPERATURE: 23.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 18.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_HEAT,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_HEAT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -367,7 +367,7 @@ async def test_thermostat_power_state(hass, hk_driver, cls, events):
|
||||
ATTR_HVAC_MODE: HVAC_MODE_HEAT,
|
||||
ATTR_TEMPERATURE: 23.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 18.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_IDLE,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_IDLE,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -381,7 +381,7 @@ async def test_thermostat_power_state(hass, hk_driver, cls, events):
|
||||
ATTR_HVAC_MODE: HVAC_MODE_OFF,
|
||||
ATTR_TEMPERATURE: 23.0,
|
||||
ATTR_CURRENT_TEMPERATURE: 18.0,
|
||||
ATTR_HVAC_ACTIONS: CURRENT_HVAC_IDLE,
|
||||
ATTR_HVAC_ACTION: CURRENT_HVAC_IDLE,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -13,7 +13,7 @@ from homeassistant.components.climate.const import (
|
||||
ATTR_CURRENT_TEMPERATURE,
|
||||
ATTR_FAN_MODE,
|
||||
ATTR_FAN_MODES,
|
||||
ATTR_HVAC_ACTIONS,
|
||||
ATTR_HVAC_ACTION,
|
||||
ATTR_HVAC_MODE,
|
||||
ATTR_HVAC_MODES,
|
||||
ATTR_TARGET_TEMP_HIGH,
|
||||
@@ -214,7 +214,7 @@ async def test_legacy_thermostat_entity_state(hass, legacy_thermostat):
|
||||
| SUPPORT_TARGET_TEMPERATURE_RANGE
|
||||
| SUPPORT_TARGET_TEMPERATURE
|
||||
)
|
||||
assert state.attributes[ATTR_HVAC_ACTIONS] == CURRENT_HVAC_IDLE
|
||||
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_IDLE
|
||||
assert sorted(state.attributes[ATTR_HVAC_MODES]) == [
|
||||
HVAC_MODE_AUTO,
|
||||
HVAC_MODE_COOL,
|
||||
@@ -238,7 +238,7 @@ async def test_basic_thermostat_entity_state(hass, basic_thermostat):
|
||||
state.attributes[ATTR_SUPPORTED_FEATURES]
|
||||
== SUPPORT_TARGET_TEMPERATURE_RANGE | SUPPORT_TARGET_TEMPERATURE
|
||||
)
|
||||
assert ATTR_HVAC_ACTIONS not in state.attributes
|
||||
assert ATTR_HVAC_ACTION not in state.attributes
|
||||
assert sorted(state.attributes[ATTR_HVAC_MODES]) == [
|
||||
HVAC_MODE_COOL,
|
||||
HVAC_MODE_HEAT,
|
||||
@@ -259,7 +259,7 @@ async def test_thermostat_entity_state(hass, thermostat):
|
||||
| SUPPORT_TARGET_TEMPERATURE_RANGE
|
||||
| SUPPORT_TARGET_TEMPERATURE
|
||||
)
|
||||
assert state.attributes[ATTR_HVAC_ACTIONS] == CURRENT_HVAC_IDLE
|
||||
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_IDLE
|
||||
assert sorted(state.attributes[ATTR_HVAC_MODES]) == [
|
||||
HVAC_MODE_AUTO,
|
||||
HVAC_MODE_COOL,
|
||||
|
||||
Reference in new issue
Block a user