Teach climate device trigger about entity registry ids (#94969)

This commit is contained in:
Erik Montnemery 2023-06-21 14:50:21 +02:00 committed by GitHub
parent 49ec806046
commit f3defff429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 27 deletions

View File

@ -34,7 +34,7 @@ TRIGGER_TYPES = {
HVAC_MODE_TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( HVAC_MODE_TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_ENTITY_ID): cv.entity_id, vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
vol.Required(CONF_TYPE): "hvac_mode_changed", vol.Required(CONF_TYPE): "hvac_mode_changed",
vol.Required(state_trigger.CONF_TO): vol.In(const.HVAC_MODES), vol.Required(state_trigger.CONF_TO): vol.In(const.HVAC_MODES),
} }
@ -43,7 +43,7 @@ HVAC_MODE_TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
CURRENT_TRIGGER_SCHEMA = vol.All( CURRENT_TRIGGER_SCHEMA = vol.All(
DEVICE_TRIGGER_BASE_SCHEMA.extend( DEVICE_TRIGGER_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_ENTITY_ID): cv.entity_id, vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
vol.Required(CONF_TYPE): vol.In( vol.Required(CONF_TYPE): vol.In(
["current_temperature_changed", "current_humidity_changed"] ["current_temperature_changed", "current_humidity_changed"]
), ),
@ -77,7 +77,7 @@ async def async_get_triggers(
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id, CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN, CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id, CONF_ENTITY_ID: entry.id,
} }
triggers.append( triggers.append(

View File

@ -52,13 +52,12 @@ async def test_get_triggers(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
) )
entity_registry.async_get_or_create( entity_entry = entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id DOMAIN, "test", "5678", device_id=device_entry.id
) )
entity_id = f"{DOMAIN}.test_5678"
hass.states.async_set( hass.states.async_set(
entity_id, entity_entry.entity_id,
HVACMode.COOL, const.HVAC_MODE_COOL,
{ {
const.ATTR_HVAC_ACTION: HVACAction.IDLE, const.ATTR_HVAC_ACTION: HVACAction.IDLE,
const.ATTR_CURRENT_HUMIDITY: 23, const.ATTR_CURRENT_HUMIDITY: 23,
@ -71,7 +70,7 @@ async def test_get_triggers(
"domain": DOMAIN, "domain": DOMAIN,
"type": trigger, "type": trigger,
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": entity_id, "entity_id": entity_entry.id,
"metadata": {"secondary": False}, "metadata": {"secondary": False},
} }
for trigger in [ for trigger in [
@ -109,7 +108,7 @@ async def test_get_triggers_hidden_auxiliary(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
) )
entity_registry.async_get_or_create( entity_entry = entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
"5678", "5678",
@ -117,9 +116,8 @@ async def test_get_triggers_hidden_auxiliary(
entity_category=entity_category, entity_category=entity_category,
hidden_by=hidden_by, hidden_by=hidden_by,
) )
entity_id = f"{DOMAIN}.test_5678"
hass.states.async_set( hass.states.async_set(
entity_id, entity_entry.entity_id,
HVACMode.COOL, HVACMode.COOL,
{ {
const.ATTR_HVAC_ACTION: HVACAction.IDLE, const.ATTR_HVAC_ACTION: HVACAction.IDLE,
@ -133,7 +131,7 @@ async def test_get_triggers_hidden_auxiliary(
"domain": DOMAIN, "domain": DOMAIN,
"type": trigger, "type": trigger,
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678", "entity_id": entity_entry.id,
"metadata": {"secondary": True}, "metadata": {"secondary": True},
} }
for trigger in [ for trigger in [
@ -148,10 +146,14 @@ async def test_get_triggers_hidden_auxiliary(
assert triggers == unordered(expected_triggers) assert triggers == unordered(expected_triggers)
async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: async def test_if_fires_on_state_change(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set( hass.states.async_set(
"climate.entity", entry.entity_id,
HVACMode.COOL, HVACMode.COOL,
{ {
const.ATTR_HVAC_ACTION: HVACAction.IDLE, const.ATTR_HVAC_ACTION: HVACAction.IDLE,
@ -170,7 +172,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": "climate.entity", "entity_id": entry.id,
"type": "hvac_mode_changed", "type": "hvac_mode_changed",
"to": HVACMode.AUTO, "to": HVACMode.AUTO,
}, },
@ -184,7 +186,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": "climate.entity", "entity_id": entry.id,
"type": "current_temperature_changed", "type": "current_temperature_changed",
"above": 20, "above": 20,
}, },
@ -198,7 +200,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": "climate.entity", "entity_id": entry.id,
"type": "current_humidity_changed", "type": "current_humidity_changed",
"below": 10, "below": 10,
}, },
@ -213,7 +215,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
# Fake that the HVAC mode is changing # Fake that the HVAC mode is changing
hass.states.async_set( hass.states.async_set(
"climate.entity", entry.entity_id,
HVACMode.AUTO, HVACMode.AUTO,
{ {
const.ATTR_HVAC_ACTION: HVACAction.COOLING, const.ATTR_HVAC_ACTION: HVACAction.COOLING,
@ -227,7 +229,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
# Fake that the temperature is changing # Fake that the temperature is changing
hass.states.async_set( hass.states.async_set(
"climate.entity", entry.entity_id,
HVACMode.AUTO, HVACMode.AUTO,
{ {
const.ATTR_HVAC_ACTION: HVACAction.COOLING, const.ATTR_HVAC_ACTION: HVACAction.COOLING,
@ -241,7 +243,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
# Fake that the humidity is changing # Fake that the humidity is changing
hass.states.async_set( hass.states.async_set(
"climate.entity", entry.entity_id,
HVACMode.AUTO, HVACMode.AUTO,
{ {
const.ATTR_HVAC_ACTION: HVACAction.COOLING, const.ATTR_HVAC_ACTION: HVACAction.COOLING,
@ -254,6 +256,60 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None:
assert calls[2].data["some"] == "current_humidity_changed" assert calls[2].data["some"] == "current_humidity_changed"
async def test_if_fires_on_state_change_legacy(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
) -> None:
"""Test for turn_on and turn_off triggers firing."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
hass.states.async_set(
entry.entity_id,
HVACMode.COOL,
{
const.ATTR_HVAC_ACTION: HVACAction.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": entry.entity_id,
"type": "hvac_mode_changed",
"to": HVACMode.AUTO,
},
"action": {
"service": "test.automation",
"data_template": {"some": "hvac_mode_changed"},
},
},
]
},
)
# Fake that the HVAC mode is changing
hass.states.async_set(
entry.entity_id,
HVACMode.AUTO,
{
const.ATTR_HVAC_ACTION: HVACAction.COOLING,
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"
async def test_get_trigger_capabilities_hvac_mode(hass: HomeAssistant) -> None: async def test_get_trigger_capabilities_hvac_mode(hass: HomeAssistant) -> None:
"""Test we get the expected capabilities from a climate trigger.""" """Test we get the expected capabilities from a climate trigger."""
capabilities = await device_trigger.async_get_trigger_capabilities( capabilities = await device_trigger.async_get_trigger_capabilities(
@ -262,7 +318,7 @@ async def test_get_trigger_capabilities_hvac_mode(hass: HomeAssistant) -> None:
"platform": "device", "platform": "device",
"domain": "climate", "domain": "climate",
"type": "hvac_mode_changed", "type": "hvac_mode_changed",
"entity_id": "climate.upstairs", "entity_id": "01234567890123456789012345678901",
"to": "heat", "to": "heat",
}, },
) )
@ -290,17 +346,23 @@ async def test_get_trigger_capabilities_hvac_mode(hass: HomeAssistant) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
"type", ["current_temperature_changed", "current_humidity_changed"] ("type", "suffix"),
[
("current_temperature_changed", UnitOfTemperature.CELSIUS),
("current_humidity_changed", "%"),
],
) )
async def test_get_trigger_capabilities_temp_humid(hass: HomeAssistant, type) -> None: async def test_get_trigger_capabilities_temp_humid(
hass: HomeAssistant, type, suffix
) -> None:
"""Test we get the expected capabilities from a climate trigger.""" """Test we get the expected capabilities from a climate trigger."""
capabilities = await device_trigger.async_get_trigger_capabilities( capabilities = await device_trigger.async_get_trigger_capabilities(
hass, hass,
{ {
"platform": "device", "platform": "device",
"domain": "climate", "domain": "climate",
"type": "current_temperature_changed", "type": type,
"entity_id": "climate.upstairs", "entity_id": "01234567890123456789012345678901",
"above": "23", "above": "23",
}, },
) )
@ -311,13 +373,13 @@ async def test_get_trigger_capabilities_temp_humid(hass: HomeAssistant, type) ->
capabilities["extra_fields"], custom_serializer=cv.custom_serializer capabilities["extra_fields"], custom_serializer=cv.custom_serializer
) == [ ) == [
{ {
"description": {"suffix": UnitOfTemperature.CELSIUS}, "description": {"suffix": suffix},
"name": "above", "name": "above",
"optional": True, "optional": True,
"type": "float", "type": "float",
}, },
{ {
"description": {"suffix": UnitOfTemperature.CELSIUS}, "description": {"suffix": suffix},
"name": "below", "name": "below",
"optional": True, "optional": True,
"type": "float", "type": "float",