mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Use entity registry id in water_heater device actions (#95276)
This commit is contained in:
parent
9dc586bd98
commit
51aa2ba835
@ -38,6 +38,7 @@ ENTITY_PLATFORMS = {
|
|||||||
Platform.SELECT.value,
|
Platform.SELECT.value,
|
||||||
Platform.SWITCH.value,
|
Platform.SWITCH.value,
|
||||||
Platform.VACUUM.value,
|
Platform.VACUUM.value,
|
||||||
|
Platform.WATER_HEATER.value,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.device_automation import async_validate_entity_schema
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
@ -21,14 +22,21 @@ from . import DOMAIN
|
|||||||
|
|
||||||
ACTION_TYPES = {"turn_on", "turn_off"}
|
ACTION_TYPES = {"turn_on", "turn_off"}
|
||||||
|
|
||||||
ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
|
_ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
|
vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
|
||||||
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN),
|
vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_validate_action_config(
|
||||||
|
hass: HomeAssistant, config: ConfigType
|
||||||
|
) -> ConfigType:
|
||||||
|
"""Validate config."""
|
||||||
|
return async_validate_entity_schema(hass, config, _ACTION_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
async def async_get_actions(
|
async def async_get_actions(
|
||||||
hass: HomeAssistant, device_id: str
|
hass: HomeAssistant, device_id: str
|
||||||
) -> list[dict[str, str]]:
|
) -> list[dict[str, str]]:
|
||||||
@ -43,7 +51,7 @@ async def async_get_actions(
|
|||||||
base_action = {
|
base_action = {
|
||||||
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
actions.append({**base_action, CONF_TYPE: "turn_on"})
|
actions.append({**base_action, CONF_TYPE: "turn_on"})
|
||||||
|
@ -35,16 +35,15 @@ async def test_get_actions(
|
|||||||
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_actions = []
|
expected_actions = [
|
||||||
expected_actions += [
|
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": action,
|
"type": action,
|
||||||
"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 action in ["turn_on", "turn_off"]
|
for action in ["turn_on", "turn_off"]
|
||||||
@ -78,7 +77,7 @@ async def test_get_actions_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",
|
||||||
@ -92,7 +91,7 @@ async def test_get_actions_hidden_auxiliary(
|
|||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": action,
|
"type": action,
|
||||||
"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 action in ["turn_on", "turn_off"]
|
for action in ["turn_on", "turn_off"]
|
||||||
@ -103,8 +102,10 @@ async def test_get_actions_hidden_auxiliary(
|
|||||||
assert actions == unordered(expected_actions)
|
assert actions == unordered(expected_actions)
|
||||||
|
|
||||||
|
|
||||||
async def test_action(hass: HomeAssistant) -> None:
|
async def test_action(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None:
|
||||||
"""Test for turn_on and turn_off actions."""
|
"""Test for turn_on and turn_off actions."""
|
||||||
|
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
automation.DOMAIN,
|
automation.DOMAIN,
|
||||||
@ -118,7 +119,7 @@ async def test_action(hass: HomeAssistant) -> None:
|
|||||||
"action": {
|
"action": {
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "abcdefgh",
|
"device_id": "abcdefgh",
|
||||||
"entity_id": "water_heater.entity",
|
"entity_id": entry.id,
|
||||||
"type": "turn_off",
|
"type": "turn_off",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -130,7 +131,7 @@ async def test_action(hass: HomeAssistant) -> None:
|
|||||||
"action": {
|
"action": {
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "abcdefgh",
|
"device_id": "abcdefgh",
|
||||||
"entity_id": "water_heater.entity",
|
"entity_id": entry.id,
|
||||||
"type": "turn_on",
|
"type": "turn_on",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -150,3 +151,42 @@ async def test_action(hass: HomeAssistant) -> None:
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(turn_off_calls) == 1
|
assert len(turn_off_calls) == 1
|
||||||
assert len(turn_on_calls) == 1
|
assert len(turn_on_calls) == 1
|
||||||
|
|
||||||
|
assert turn_off_calls[-1].data == {"entity_id": entry.entity_id}
|
||||||
|
assert turn_on_calls[-1].data == {"entity_id": entry.entity_id}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_action_legacy(
|
||||||
|
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||||
|
) -> None:
|
||||||
|
"""Test for turn_on and turn_off actions."""
|
||||||
|
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: [
|
||||||
|
{
|
||||||
|
"trigger": {
|
||||||
|
"platform": "event",
|
||||||
|
"event_type": "test_event_turn_off",
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "abcdefgh",
|
||||||
|
"entity_id": entry.entity_id,
|
||||||
|
"type": "turn_off",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
turn_off_calls = async_mock_service(hass, "water_heater", "turn_off")
|
||||||
|
|
||||||
|
hass.bus.async_fire("test_event_turn_off")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(turn_off_calls) == 1
|
||||||
|
|
||||||
|
assert turn_off_calls[-1].data == {"entity_id": entry.entity_id}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user