mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Use entity registry id in fan device conditions (#95255)
This commit is contained in:
parent
16ec9b1e9f
commit
c4589ad4e5
@ -28,7 +28,7 @@ CONDITION_TYPES = {"is_on", "is_off"}
|
|||||||
|
|
||||||
CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend(
|
CONDITION_SCHEMA = DEVICE_CONDITION_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(CONDITION_TYPES),
|
vol.Required(CONF_TYPE): vol.In(CONDITION_TYPES),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -50,7 +50,7 @@ async def async_get_conditions(
|
|||||||
CONF_CONDITION: "device",
|
CONF_CONDITION: "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,
|
||||||
}
|
}
|
||||||
|
|
||||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
@ -63,6 +63,9 @@ def async_condition_from_config(
|
|||||||
hass: HomeAssistant, config: ConfigType
|
hass: HomeAssistant, config: ConfigType
|
||||||
) -> condition.ConditionCheckerType:
|
) -> condition.ConditionCheckerType:
|
||||||
"""Create a function to test a device condition."""
|
"""Create a function to test a device condition."""
|
||||||
|
registry = er.async_get(hass)
|
||||||
|
entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID])
|
||||||
|
|
||||||
if config[CONF_TYPE] == "is_on":
|
if config[CONF_TYPE] == "is_on":
|
||||||
state = STATE_ON
|
state = STATE_ON
|
||||||
else:
|
else:
|
||||||
@ -71,6 +74,6 @@ def async_condition_from_config(
|
|||||||
@callback
|
@callback
|
||||||
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
|
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
|
||||||
"""Test if an entity is a certain state."""
|
"""Test if an entity is a certain state."""
|
||||||
return condition.state(hass, config[ATTR_ENTITY_ID], state)
|
return condition.state(hass, entity_id, state)
|
||||||
|
|
||||||
return test_is_state
|
return test_is_state
|
||||||
|
@ -41,7 +41,7 @@ async def test_get_conditions(
|
|||||||
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
|
||||||
)
|
)
|
||||||
expected_conditions = [
|
expected_conditions = [
|
||||||
@ -50,7 +50,7 @@ async def test_get_conditions(
|
|||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": condition,
|
"type": condition,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
"entity_id": entity_entry.id,
|
||||||
"metadata": {"secondary": False},
|
"metadata": {"secondary": False},
|
||||||
}
|
}
|
||||||
for condition in ["is_off", "is_on"]
|
for condition in ["is_off", "is_on"]
|
||||||
@ -84,7 +84,7 @@ async def test_get_conditions_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",
|
||||||
@ -98,7 +98,7 @@ async def test_get_conditions_hidden_auxiliary(
|
|||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": condition,
|
"type": condition,
|
||||||
"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 condition in ["is_off", "is_on"]
|
for condition in ["is_off", "is_on"]
|
||||||
@ -109,9 +109,13 @@ async def test_get_conditions_hidden_auxiliary(
|
|||||||
assert conditions == unordered(expected_conditions)
|
assert conditions == unordered(expected_conditions)
|
||||||
|
|
||||||
|
|
||||||
async def test_if_state(hass: HomeAssistant, calls) -> None:
|
async def test_if_state(
|
||||||
|
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
|
||||||
|
) -> None:
|
||||||
"""Test for turn_on and turn_off conditions."""
|
"""Test for turn_on and turn_off conditions."""
|
||||||
hass.states.async_set("fan.entity", STATE_ON)
|
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||||
|
|
||||||
|
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
@ -125,7 +129,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None:
|
|||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "fan.entity",
|
"entity_id": entry.id,
|
||||||
"type": "is_on",
|
"type": "is_on",
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -147,7 +151,7 @@ async def test_if_state(hass: HomeAssistant, calls) -> None:
|
|||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"entity_id": "fan.entity",
|
"entity_id": entry.id,
|
||||||
"type": "is_off",
|
"type": "is_off",
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -171,9 +175,53 @@ async def test_if_state(hass: HomeAssistant, calls) -> None:
|
|||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].data["some"] == "is_on - event - test_event1"
|
assert calls[0].data["some"] == "is_on - event - test_event1"
|
||||||
|
|
||||||
hass.states.async_set("fan.entity", STATE_OFF)
|
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||||
hass.bus.async_fire("test_event1")
|
hass.bus.async_fire("test_event1")
|
||||||
hass.bus.async_fire("test_event2")
|
hass.bus.async_fire("test_event2")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
assert calls[1].data["some"] == "is_off - event - test_event2"
|
assert calls[1].data["some"] == "is_off - event - test_event2"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_state_legacy(
|
||||||
|
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
|
||||||
|
) -> None:
|
||||||
|
"""Test for turn_on and turn_off conditions."""
|
||||||
|
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||||
|
|
||||||
|
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: [
|
||||||
|
{
|
||||||
|
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||||
|
"condition": [
|
||||||
|
{
|
||||||
|
"condition": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "",
|
||||||
|
"entity_id": entry.entity_id,
|
||||||
|
"type": "is_on",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"action": {
|
||||||
|
"service": "test.automation",
|
||||||
|
"data_template": {
|
||||||
|
"some": (
|
||||||
|
"is_on "
|
||||||
|
"- {{ trigger.platform }} "
|
||||||
|
"- {{ trigger.event.event_type }}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
hass.bus.async_fire("test_event1")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 1
|
||||||
|
assert calls[0].data["some"] == "is_on - event - test_event1"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user