diff --git a/homeassistant/components/climate/device_trigger.py b/homeassistant/components/climate/device_trigger.py index 005e744b53f..470c45aee70 100644 --- a/homeassistant/components/climate/device_trigger.py +++ b/homeassistant/components/climate/device_trigger.py @@ -34,7 +34,7 @@ TRIGGER_TYPES = { 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(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( 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( ["current_temperature_changed", "current_humidity_changed"] ), @@ -77,7 +77,7 @@ async def async_get_triggers( CONF_PLATFORM: "device", CONF_DEVICE_ID: device_id, CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, + CONF_ENTITY_ID: entry.id, } triggers.append( diff --git a/tests/components/climate/test_device_trigger.py b/tests/components/climate/test_device_trigger.py index 6cc304c4955..c600e4004e8 100644 --- a/tests/components/climate/test_device_trigger.py +++ b/tests/components/climate/test_device_trigger.py @@ -52,13 +52,12 @@ async def test_get_triggers( config_entry_id=config_entry.entry_id, 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 ) - entity_id = f"{DOMAIN}.test_5678" hass.states.async_set( - entity_id, - HVACMode.COOL, + entity_entry.entity_id, + const.HVAC_MODE_COOL, { const.ATTR_HVAC_ACTION: HVACAction.IDLE, const.ATTR_CURRENT_HUMIDITY: 23, @@ -71,7 +70,7 @@ async def test_get_triggers( "domain": DOMAIN, "type": trigger, "device_id": device_entry.id, - "entity_id": entity_id, + "entity_id": entity_entry.id, "metadata": {"secondary": False}, } for trigger in [ @@ -109,7 +108,7 @@ async def test_get_triggers_hidden_auxiliary( config_entry_id=config_entry.entry_id, 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", @@ -117,9 +116,8 @@ async def test_get_triggers_hidden_auxiliary( entity_category=entity_category, hidden_by=hidden_by, ) - entity_id = f"{DOMAIN}.test_5678" hass.states.async_set( - entity_id, + entity_entry.entity_id, HVACMode.COOL, { const.ATTR_HVAC_ACTION: HVACAction.IDLE, @@ -133,7 +131,7 @@ async def test_get_triggers_hidden_auxiliary( "domain": DOMAIN, "type": trigger, "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": True}, } for trigger in [ @@ -148,10 +146,14 @@ async def test_get_triggers_hidden_auxiliary( 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.""" + entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678") + hass.states.async_set( - "climate.entity", + entry.entity_id, HVACMode.COOL, { const.ATTR_HVAC_ACTION: HVACAction.IDLE, @@ -170,7 +172,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "climate.entity", + "entity_id": entry.id, "type": "hvac_mode_changed", "to": HVACMode.AUTO, }, @@ -184,7 +186,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "climate.entity", + "entity_id": entry.id, "type": "current_temperature_changed", "above": 20, }, @@ -198,7 +200,7 @@ async def test_if_fires_on_state_change(hass: HomeAssistant, calls) -> None: "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": "climate.entity", + "entity_id": entry.id, "type": "current_humidity_changed", "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 hass.states.async_set( - "climate.entity", + entry.entity_id, HVACMode.AUTO, { 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 hass.states.async_set( - "climate.entity", + entry.entity_id, HVACMode.AUTO, { 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 hass.states.async_set( - "climate.entity", + entry.entity_id, HVACMode.AUTO, { 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" +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: """Test we get the expected capabilities from a climate trigger.""" 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", "domain": "climate", "type": "hvac_mode_changed", - "entity_id": "climate.upstairs", + "entity_id": "01234567890123456789012345678901", "to": "heat", }, ) @@ -290,17 +346,23 @@ async def test_get_trigger_capabilities_hvac_mode(hass: HomeAssistant) -> None: @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.""" capabilities = await device_trigger.async_get_trigger_capabilities( hass, { "platform": "device", "domain": "climate", - "type": "current_temperature_changed", - "entity_id": "climate.upstairs", + "type": type, + "entity_id": "01234567890123456789012345678901", "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 ) == [ { - "description": {"suffix": UnitOfTemperature.CELSIUS}, + "description": {"suffix": suffix}, "name": "above", "optional": True, "type": "float", }, { - "description": {"suffix": UnitOfTemperature.CELSIUS}, + "description": {"suffix": suffix}, "name": "below", "optional": True, "type": "float",