From ea046f32beb53806b544f59b0cef1a05ccbce677 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Thu, 15 May 2025 04:43:56 -0400 Subject: [PATCH] Add modern style template lock (#144756) * Add modern style lock * add tests * Add tests and address comments * Update homeassistant/components/template/lock.py --------- Co-authored-by: Erik Montnemery --- homeassistant/components/template/config.py | 7 +- homeassistant/components/template/lock.py | 94 ++- tests/components/template/test_lock.py | 889 +++++++++++++------- 3 files changed, 652 insertions(+), 338 deletions(-) diff --git a/homeassistant/components/template/config.py b/homeassistant/components/template/config.py index 5e7425f13d7..1dc20d07c0e 100644 --- a/homeassistant/components/template/config.py +++ b/homeassistant/components/template/config.py @@ -17,6 +17,7 @@ from homeassistant.components.cover import DOMAIN as COVER_DOMAIN from homeassistant.components.fan import DOMAIN as FAN_DOMAIN from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN +from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN from homeassistant.components.select import DOMAIN as SELECT_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN @@ -50,6 +51,7 @@ from . import ( fan as fan_platform, image as image_platform, light as light_platform, + lock as lock_platform, number as number_platform, select as select_platform, sensor as sensor_platform, @@ -124,6 +126,9 @@ CONFIG_SECTION_SCHEMA = vol.All( vol.Optional(LIGHT_DOMAIN): vol.All( cv.ensure_list, [light_platform.LIGHT_SCHEMA] ), + vol.Optional(LOCK_DOMAIN): vol.All( + cv.ensure_list, [lock_platform.LOCK_SCHEMA] + ), vol.Optional(WEATHER_DOMAIN): vol.All( cv.ensure_list, [weather_platform.WEATHER_SCHEMA] ), @@ -139,7 +144,7 @@ CONFIG_SECTION_SCHEMA = vol.All( }, ), ensure_domains_do_not_have_trigger_or_action( - BUTTON_DOMAIN, COVER_DOMAIN, FAN_DOMAIN + BUTTON_DOMAIN, COVER_DOMAIN, FAN_DOMAIN, LOCK_DOMAIN ), ) diff --git a/homeassistant/components/template/lock.py b/homeassistant/components/template/lock.py index 12a3e66cb5e..c858325e0ea 100644 --- a/homeassistant/components/template/lock.py +++ b/homeassistant/components/template/lock.py @@ -16,6 +16,7 @@ from homeassistant.const import ( ATTR_CODE, CONF_NAME, CONF_OPTIMISTIC, + CONF_STATE, CONF_UNIQUE_ID, CONF_VALUE_TEMPLATE, ) @@ -25,14 +26,18 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from .const import DOMAIN +from .const import CONF_PICTURE, DOMAIN from .template_entity import ( + LEGACY_FIELDS as TEMPLATE_ENTITY_LEGACY_FIELDS, + TEMPLATE_ENTITY_AVAILABILITY_SCHEMA, TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY, + TEMPLATE_ENTITY_ICON_SCHEMA, TemplateEntity, rewrite_common_legacy_to_modern_conf, ) CONF_CODE_FORMAT_TEMPLATE = "code_format_template" +CONF_CODE_FORMAT = "code_format" CONF_LOCK = "lock" CONF_UNLOCK = "unlock" CONF_OPEN = "open" @@ -40,26 +45,69 @@ CONF_OPEN = "open" DEFAULT_NAME = "Template Lock" DEFAULT_OPTIMISTIC = False +LEGACY_FIELDS = TEMPLATE_ENTITY_LEGACY_FIELDS | { + CONF_CODE_FORMAT_TEMPLATE: CONF_CODE_FORMAT, + CONF_VALUE_TEMPLATE: CONF_STATE, +} + +LOCK_SCHEMA = vol.All( + vol.Schema( + { + vol.Optional(CONF_CODE_FORMAT): cv.template, + vol.Required(CONF_LOCK): cv.SCRIPT_SCHEMA, + vol.Optional(CONF_NAME): cv.template, + vol.Optional(CONF_OPEN): cv.SCRIPT_SCHEMA, + vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean, + vol.Optional(CONF_PICTURE): cv.template, + vol.Required(CONF_STATE): cv.template, + vol.Optional(CONF_UNIQUE_ID): cv.string, + vol.Required(CONF_UNLOCK): cv.SCRIPT_SCHEMA, + } + ) + .extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema) + .extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema), +) + + PLATFORM_SCHEMA = LOCK_PLATFORM_SCHEMA.extend( { - vol.Optional(CONF_NAME): cv.string, - vol.Required(CONF_LOCK): cv.SCRIPT_SCHEMA, - vol.Required(CONF_UNLOCK): cv.SCRIPT_SCHEMA, - vol.Optional(CONF_OPEN): cv.SCRIPT_SCHEMA, - vol.Required(CONF_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_CODE_FORMAT_TEMPLATE): cv.template, + vol.Required(CONF_LOCK): cv.SCRIPT_SCHEMA, + vol.Optional(CONF_NAME): cv.string, + vol.Optional(CONF_OPEN): cv.SCRIPT_SCHEMA, vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean, vol.Optional(CONF_UNIQUE_ID): cv.string, + vol.Required(CONF_UNLOCK): cv.SCRIPT_SCHEMA, + vol.Required(CONF_VALUE_TEMPLATE): cv.template, } ).extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY.schema) -async def _async_create_entities( - hass: HomeAssistant, config: dict[str, Any] -) -> list[TemplateLock]: - """Create the Template lock.""" - config = rewrite_common_legacy_to_modern_conf(hass, config) - return [TemplateLock(hass, config, config.get(CONF_UNIQUE_ID))] +@callback +def _async_create_template_tracking_entities( + async_add_entities: AddEntitiesCallback, + hass: HomeAssistant, + definitions: list[dict], + unique_id_prefix: str | None, +) -> None: + """Create the template fans.""" + fans = [] + + for entity_conf in definitions: + unique_id = entity_conf.get(CONF_UNIQUE_ID) + + if unique_id and unique_id_prefix: + unique_id = f"{unique_id_prefix}-{unique_id}" + + fans.append( + TemplateLock( + hass, + entity_conf, + unique_id, + ) + ) + + async_add_entities(fans) async def async_setup_platform( @@ -68,8 +116,22 @@ async def async_setup_platform( async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: - """Set up the template lock.""" - async_add_entities(await _async_create_entities(hass, config)) + """Set up the template fans.""" + if discovery_info is None: + _async_create_template_tracking_entities( + async_add_entities, + hass, + [rewrite_common_legacy_to_modern_conf(hass, config, LEGACY_FIELDS)], + None, + ) + return + + _async_create_template_tracking_entities( + async_add_entities, + hass, + discovery_info["entities"], + discovery_info["unique_id"], + ) class TemplateLock(TemplateEntity, LockEntity): @@ -92,7 +154,7 @@ class TemplateLock(TemplateEntity, LockEntity): if TYPE_CHECKING: assert name is not None - self._state_template = config.get(CONF_VALUE_TEMPLATE) + self._state_template = config.get(CONF_STATE) for action_id, supported_feature in ( (CONF_LOCK, 0), (CONF_UNLOCK, 0), @@ -102,7 +164,7 @@ class TemplateLock(TemplateEntity, LockEntity): if (action_config := config.get(action_id)) is not None: self.add_script(action_id, action_config, name, DOMAIN) self._attr_supported_features |= supported_feature - self._code_format_template = config.get(CONF_CODE_FORMAT_TEMPLATE) + self._code_format_template = config.get(CONF_CODE_FORMAT) self._code_format: str | None = None self._code_format_template_error: TemplateError | None = None self._optimistic = config.get(CONF_OPTIMISTIC) diff --git a/tests/components/template/test_lock.py b/tests/components/template/test_lock.py index 50baa11b2d0..4435e4a2404 100644 --- a/tests/components/template/test_lock.py +++ b/tests/components/template/test_lock.py @@ -1,9 +1,11 @@ """The tests for the Template lock platform.""" +from typing import Any + import pytest from homeassistant import setup -from homeassistant.components import lock +from homeassistant.components import lock, template from homeassistant.components.lock import LockEntityFeature, LockState from homeassistant.const import ( ATTR_CODE, @@ -14,25 +16,38 @@ from homeassistant.const import ( STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.helpers import entity_registry as er +from homeassistant.setup import async_setup_component + +from .conftest import ConfigurationStyle from tests.common import assert_setup_component -OPTIMISTIC_LOCK_CONFIG = { - "platform": "template", +TEST_OBJECT_ID = "test_template_lock" +TEST_ENTITY_ID = f"lock.{TEST_OBJECT_ID}" +TEST_STATE_ENTITY_ID = "switch.test_state" + +LOCK_ACTION = { "lock": { "service": "test.automation", "data_template": { "action": "lock", "caller": "{{ this.entity_id }}", + "code": "{{ code if code is defined else None }}", }, }, +} +UNLOCK_ACTION = { "unlock": { "service": "test.automation", "data_template": { "action": "unlock", "caller": "{{ this.entity_id }}", + "code": "{{ code if code is defined else None }}", }, }, +} +OPEN_ACTION = { "open": { "service": "test.automation", "data_template": { @@ -42,424 +57,565 @@ OPTIMISTIC_LOCK_CONFIG = { }, } -OPTIMISTIC_CODED_LOCK_CONFIG = { - "platform": "template", - "lock": { - "service": "test.automation", - "data_template": { - "action": "lock", - "caller": "{{ this.entity_id }}", - "code": "{{ code }}", - }, - }, - "unlock": { - "service": "test.automation", - "data_template": { - "action": "unlock", - "caller": "{{ this.entity_id }}", - "code": "{{ code }}", - }, - }, + +OPTIMISTIC_LOCK = { + **LOCK_ACTION, + **UNLOCK_ACTION, } -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) +OPTIMISTIC_LOCK_CONFIG = { + "platform": "template", + **LOCK_ACTION, + **UNLOCK_ACTION, + **OPEN_ACTION, +} + +OPTIMISTIC_CODED_LOCK_CONFIG = { + "platform": "template", + **LOCK_ACTION, + **UNLOCK_ACTION, +} + + +async def async_setup_legacy_format( + hass: HomeAssistant, count: int, lock_config: dict[str, Any] +) -> None: + """Do setup of lock integration via legacy format.""" + config = {"lock": {"platform": "template", "name": TEST_OBJECT_ID, **lock_config}} + + with assert_setup_component(count, lock.DOMAIN): + assert await async_setup_component( + hass, + lock.DOMAIN, + config, + ) + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + +async def async_setup_modern_format( + hass: HomeAssistant, count: int, lock_config: dict[str, Any] +) -> None: + """Do setup of lock integration via modern format.""" + config = {"template": {"lock": {"name": TEST_OBJECT_ID, **lock_config}}} + + with assert_setup_component(count, template.DOMAIN): + assert await async_setup_component( + hass, + template.DOMAIN, + config, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + +@pytest.fixture +async def setup_lock( + hass: HomeAssistant, + count: int, + style: ConfigurationStyle, + lock_config: dict[str, Any], +) -> None: + """Do setup of lock integration.""" + if style == ConfigurationStyle.LEGACY: + await async_setup_legacy_format(hass, count, lock_config) + elif style == ConfigurationStyle.MODERN: + await async_setup_modern_format(hass, count, lock_config) + + +@pytest.fixture +async def setup_base_lock( + hass: HomeAssistant, + count: int, + style: ConfigurationStyle, + state_template: str, + extra_config: dict, +): + """Do setup of cover integration using a state template.""" + if style == ConfigurationStyle.LEGACY: + await async_setup_legacy_format( + hass, + count, + {"value_template": state_template, **extra_config}, + ) + elif style == ConfigurationStyle.MODERN: + await async_setup_modern_format( + hass, + count, + {"state": state_template, **extra_config}, + ) + + +@pytest.fixture +async def setup_state_lock( + hass: HomeAssistant, + count: int, + style: ConfigurationStyle, + state_template: str, +): + """Do setup of cover integration using a state template.""" + if style == ConfigurationStyle.LEGACY: + await async_setup_legacy_format( + hass, + count, + { + **OPTIMISTIC_LOCK, + "value_template": state_template, + }, + ) + elif style == ConfigurationStyle.MODERN: + await async_setup_modern_format( + hass, + count, + { + **OPTIMISTIC_LOCK, + "state": state_template, + }, + ) + + +@pytest.fixture +async def setup_state_lock_with_extra_config( + hass: HomeAssistant, + count: int, + style: ConfigurationStyle, + state_template: str, + extra_config: dict, +): + """Do setup of cover integration using a state template.""" + if style == ConfigurationStyle.LEGACY: + await async_setup_legacy_format( + hass, + count, + {**OPTIMISTIC_LOCK, "value_template": state_template, **extra_config}, + ) + elif style == ConfigurationStyle.MODERN: + await async_setup_modern_format( + hass, + count, + {**OPTIMISTIC_LOCK, "state": state_template, **extra_config}, + ) + + +@pytest.fixture +async def setup_state_lock_with_attribute( + hass: HomeAssistant, + count: int, + style: ConfigurationStyle, + state_template: str, + attribute: str, + attribute_template: str, +): + """Do setup of cover integration using a state template.""" + extra = {attribute: attribute_template} if attribute else {} + if style == ConfigurationStyle.LEGACY: + await async_setup_legacy_format( + hass, + count, + { + **OPTIMISTIC_LOCK, + "value_template": state_template, + **extra, + }, + ) + elif style == ConfigurationStyle.MODERN: + await async_setup_modern_format( + hass, + count, + {**OPTIMISTIC_LOCK, "state": state_template, **extra}, + ) + + @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "name": "Test template lock", - "value_template": "{{ states.switch.test_state.state }}", - } - }, - ], + ("count", "state_template"), [(1, "{{ states.switch.test_state.state }}")] ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.usefixtures("setup_state_lock") async def test_template_state(hass: HomeAssistant) -> None: """Test template.""" - hass.states.async_set("switch.test_state", STATE_ON) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) await hass.async_block_till_done() state = hass.states.get("lock.test_template_lock") assert state.state == LockState.LOCKED - hass.states.async_set("switch.test_state", STATE_OFF) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) await hass.async_block_till_done() state = hass.states.get("lock.test_template_lock") assert state.state == LockState.UNLOCKED - hass.states.async_set("switch.test_state", STATE_OPEN) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OPEN) await hass.async_block_till_done() state = hass.states.get("lock.test_template_lock") assert state.state == LockState.OPEN -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "name": "Test lock", - "optimistic": True, - "value_template": "{{ states.switch.test_state.state }}", - } - }, - ], + ("count", "state_template", "extra_config"), + [(1, "{{ states.switch.test_state.state }}", {"optimistic": True, **OPEN_ACTION})], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.usefixtures("setup_state_lock_with_extra_config") async def test_open_lock_optimistic( hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test optimistic open.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_ON) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) await hass.async_block_till_done() - state = hass.states.get("lock.test_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.LOCKED await hass.services.async_call( lock.DOMAIN, lock.SERVICE_OPEN, - {ATTR_ENTITY_ID: "lock.test_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == "open" - assert calls[0].data["caller"] == "lock.test_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID - state = hass.states.get("lock.test_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.OPEN -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) +@pytest.mark.parametrize(("count", "state_template"), [(1, "{{ 1 == 1 }}")]) @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 == 1 }}", - } - }, - ], + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.usefixtures("setup_state_lock") async def test_template_state_boolean_on(hass: HomeAssistant) -> None: """Test the setting of the state with boolean on.""" - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.LOCKED -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) +@pytest.mark.parametrize(("count", "state_template"), [(1, "{{ 1 == 2 }}")]) @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 == 2 }}", - } - }, - ], + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.usefixtures("setup_state_lock") async def test_template_state_boolean_off(hass: HomeAssistant) -> None: """Test the setting of the state with off.""" - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.UNLOCKED -@pytest.mark.parametrize(("count", "domain"), [(0, lock.DOMAIN)]) +@pytest.mark.parametrize("count", [0]) @pytest.mark.parametrize( - "config", + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.parametrize( + ("state_template", "extra_config"), [ - { - lock.DOMAIN: { - "platform": "template", - "value_template": "{% if rubbish %}", - "lock": { - "service": "switch.turn_on", - "entity_id": "switch.test_state", - }, - "unlock": { - "service": "switch.turn_off", - "entity_id": "switch.test_state", - }, - } - }, - { - "switch": { - "platform": "lock", - "name": "{{%}", - "value_template": "{{ rubbish }", - "lock": { - "service": "switch.turn_on", - "entity_id": "switch.test_state", - }, - "unlock": { - "service": "switch.turn_off", - "entity_id": "switch.test_state", - }, - }, - }, - {lock.DOMAIN: {"platform": "template", "value_template": "Invalid"}}, - { - lock.DOMAIN: { - "platform": "template", + ("{% if rubbish %}", OPTIMISTIC_LOCK), + ("{{ rubbish }", OPTIMISTIC_LOCK), + ("Invalid", {}), + ( + "{{ 1==1 }}", + { "not_value_template": "{{ states.switch.test_state.state }}", - "lock": { - "service": "switch.turn_on", - "entity_id": "switch.test_state", - }, - "unlock": { - "service": "switch.turn_off", - "entity_id": "switch.test_state", - }, - } - }, - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 == 1 }}", - "code_format_template": "{{ rubbish }", - } - }, - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 == 1 }}", - "code_format_template": "{% if rubbish %}", - } - }, + **OPTIMISTIC_LOCK, + }, + ), ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.usefixtures("setup_base_lock") async def test_template_syntax_error(hass: HomeAssistant) -> None: """Test templating syntax errors don't create entities.""" assert hass.states.async_all("lock") == [] -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) +@pytest.mark.parametrize(("count", "state_template"), [(0, "{{ 1==1 }}")]) +@pytest.mark.parametrize("attribute_template", ["{{ rubbish }", "{% if rubbish %}"]) @pytest.mark.parametrize( - "config", + ("style", "attribute"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 + 1 }}", - } - }, + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.usefixtures("setup_state_lock_with_attribute") +async def test_template_code_template_syntax_error(hass: HomeAssistant) -> None: + """Test templating code_format syntax errors don't create entities.""" + assert hass.states.async_all("lock") == [] + + +@pytest.mark.parametrize(("count", "state_template"), [(1, "{{ 1 + 1 }}")]) +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.usefixtures("setup_state_lock") async def test_template_static(hass: HomeAssistant) -> None: """Test that we allow static templates.""" - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.UNLOCKED - hass.states.async_set("lock.template_lock", LockState.LOCKED) + hass.states.async_set(TEST_ENTITY_ID, LockState.LOCKED) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.LOCKED -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.parametrize( + ("state_template", "expected"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - } - }, + ("{{ True }}", LockState.LOCKED), + ("{{ False }}", LockState.UNLOCKED), + ("{{ x - 12 }}", STATE_UNAVAILABLE), ], ) -@pytest.mark.usefixtures("start_ha") -async def test_lock_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None: - """Test lock action.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_OFF) +@pytest.mark.usefixtures("setup_state_lock") +async def test_state_template(hass: HomeAssistant, expected: str) -> None: + """Test state and value_template template.""" + state = hass.states.get(TEST_ENTITY_ID) + assert state.state == expected + + +@pytest.mark.parametrize(("count", "state_template"), [(1, "{{ 1==1 }}")]) +@pytest.mark.parametrize( + "attribute_template", + ["{% if states.switch.test_state.state %}/local/switch.png{% endif %}"], +) +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.MODERN, "picture"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") +async def test_picture_template(hass: HomeAssistant) -> None: + """Test entity_picture template.""" + state = hass.states.get(TEST_ENTITY_ID) + assert state.attributes.get("entity_picture") in ("", None) + + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) + assert state.attributes["entity_picture"] == "/local/switch.png" + + +@pytest.mark.parametrize(("count", "state_template"), [(1, "{{ 1==1 }}")]) +@pytest.mark.parametrize( + "attribute_template", + ["{% if states.switch.test_state.state %}mdi:eye{% endif %}"], +) +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.MODERN, "icon"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") +async def test_icon_template(hass: HomeAssistant) -> None: + """Test entity_picture template.""" + state = hass.states.get(TEST_ENTITY_ID) + assert state.attributes.get("icon") in ("", None) + + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) + await hass.async_block_till_done() + + state = hass.states.get(TEST_ENTITY_ID) + assert state.attributes["icon"] == "mdi:eye" + + +@pytest.mark.parametrize( + ("count", "state_template"), [(1, "{{ states.switch.test_state.state }}")] +) +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.usefixtures("setup_state_lock") +async def test_lock_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None: + """Test lock action.""" + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) + await hass.async_block_till_done() + + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.UNLOCKED await hass.services.async_call( lock.DOMAIN, lock.SERVICE_LOCK, - {ATTR_ENTITY_ID: "lock.template_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == "lock" - assert calls[0].data["caller"] == "lock.template_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - } - }, - ], + ("count", "state_template"), [(1, "{{ states.switch.test_state.state }}")] ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.usefixtures("setup_state_lock") async def test_unlock_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None: """Test unlock action.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_ON) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.LOCKED await hass.services.async_call( lock.DOMAIN, lock.SERVICE_UNLOCK, - {ATTR_ENTITY_ID: "lock.template_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == "unlock" - assert calls[0].data["caller"] == "lock.template_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - } - }, - ], + ("count", "state_template", "extra_config"), + [(1, "{{ states.switch.test_state.state }}", OPEN_ACTION)], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] +) +@pytest.mark.usefixtures("setup_state_lock_with_extra_config") async def test_open_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None: """Test open action.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_ON) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.LOCKED await hass.services.async_call( lock.DOMAIN, lock.SERVICE_OPEN, - {ATTR_ENTITY_ID: "lock.template_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == "open" - assert calls[0].data["caller"] == "lock.template_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_CODED_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - "code_format_template": "{{ '.+' }}", - } - }, + ( + 1, + "{{ states.switch.test_state.state }}", + "{{ '.+' }}", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_lock_action_with_code( hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test lock action with defined code format and supplied lock code.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_OFF) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.UNLOCKED await hass.services.async_call( lock.DOMAIN, lock.SERVICE_LOCK, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "LOCK_CODE"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "LOCK_CODE"}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == "lock" - assert calls[0].data["caller"] == "lock.template_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID assert calls[0].data["code"] == "LOCK_CODE" -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_CODED_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - "code_format_template": "{{ '.+' }}", - } - }, + ( + 1, + "{{ states.switch.test_state.state }}", + "{{ '.+' }}", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_unlock_action_with_code( hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test unlock action with code format and supplied unlock code.""" await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_ON) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.LOCKED await hass.services.async_call( lock.DOMAIN, lock.SERVICE_UNLOCK, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "UNLOCK_CODE"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "UNLOCK_CODE"}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == "unlock" - assert calls[0].data["caller"] == "lock.template_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID assert calls[0].data["code"] == "UNLOCK_CODE" -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 == 1 }}", - "code_format_template": "{{ '\\\\d+' }}", - } - }, + ( + 1, + "{{ 1 == 1 }}", + "{{ '\\\\d+' }}", + ) + ], +) +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), ], ) @pytest.mark.parametrize( @@ -469,7 +625,7 @@ async def test_unlock_action_with_code( lock.SERVICE_UNLOCK, ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_lock_actions_fail_with_invalid_code( hass: HomeAssistant, calls: list[ServiceCall], test_action ) -> None: @@ -477,32 +633,36 @@ async def test_lock_actions_fail_with_invalid_code( await hass.services.async_call( lock.DOMAIN, test_action, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "non-number-value"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "non-number-value"}, ) await hass.services.async_call( lock.DOMAIN, test_action, - {ATTR_ENTITY_ID: "lock.template_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.async_block_till_done() assert len(calls) == 0 -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 == 1 }}", - "code_format_template": "{{ 1/0 }}", - } - }, + ( + 1, + "{{ 1 == 1 }}", + "{{ 1/0 }}", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_lock_actions_dont_execute_with_code_template_rendering_error( hass: HomeAssistant, calls: list[ServiceCall] ) -> None: @@ -510,142 +670,146 @@ async def test_lock_actions_dont_execute_with_code_template_rendering_error( await hass.services.async_call( lock.DOMAIN, lock.SERVICE_LOCK, - {ATTR_ENTITY_ID: "lock.template_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.services.async_call( lock.DOMAIN, lock.SERVICE_UNLOCK, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "any-value"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "any-value"}, ) await hass.async_block_till_done() assert len(calls) == 0 -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) -@pytest.mark.parametrize("action", [lock.SERVICE_LOCK, lock.SERVICE_UNLOCK]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_CODED_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - "code_format_template": "{{ None }}", - } - }, + ( + 1, + "{{ states.switch.test_state.state }}", + "{{ None }}", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), + ], +) +@pytest.mark.parametrize("action", [lock.SERVICE_LOCK, lock.SERVICE_UNLOCK]) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_actions_with_none_as_codeformat_ignores_code( hass: HomeAssistant, action, calls: list[ServiceCall] ) -> None: """Test lock actions with supplied lock code.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_OFF) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.UNLOCKED await hass.services.async_call( lock.DOMAIN, action, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "any code"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "any code"}, ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["action"] == action - assert calls[0].data["caller"] == "lock.template_lock" + assert calls[0].data["caller"] == TEST_ENTITY_ID assert calls[0].data["code"] == "any code" -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) -@pytest.mark.parametrize("action", [lock.SERVICE_LOCK, lock.SERVICE_UNLOCK]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ states.switch.test_state.state }}", - "code_format_template": "[12]{1", - } - }, + ( + 1, + "{{ states.switch.test_state.state }}", + "[12]{1", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "code_format_template"), + (ConfigurationStyle.MODERN, "code_format"), + ], +) +@pytest.mark.parametrize("action", [lock.SERVICE_LOCK, lock.SERVICE_UNLOCK]) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_actions_with_invalid_regexp_as_codeformat_never_execute( hass: HomeAssistant, action, calls: list[ServiceCall] ) -> None: """Test lock actions don't execute with invalid regexp.""" - await setup.async_setup_component(hass, "switch", {}) - hass.states.async_set("switch.test_state", STATE_OFF) + hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == LockState.UNLOCKED await hass.services.async_call( lock.DOMAIN, action, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "1"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "1"}, ) await hass.services.async_call( lock.DOMAIN, action, - {ATTR_ENTITY_ID: "lock.template_lock", ATTR_CODE: "x"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID, ATTR_CODE: "x"}, ) await hass.services.async_call( lock.DOMAIN, action, - {ATTR_ENTITY_ID: "lock.template_lock"}, + {ATTR_ENTITY_ID: TEST_ENTITY_ID}, ) await hass.async_block_till_done() assert len(calls) == 0 -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", - [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ states.input_select.test_state.state }}", - } - }, - ], + ("count", "state_template"), [(1, "{{ states.input_select.test_state.state }}")] +) +@pytest.mark.parametrize( + "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN] ) @pytest.mark.parametrize( "test_state", [LockState.UNLOCKING, LockState.LOCKING, LockState.JAMMED] ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.usefixtures("setup_state_lock") async def test_lock_state(hass: HomeAssistant, test_state) -> None: """Test value template.""" hass.states.async_set("input_select.test_state", test_state) await hass.async_block_till_done() - state = hass.states.get("lock.template_lock") + state = hass.states.get(TEST_ENTITY_ID) assert state.state == test_state -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ states('switch.test_state') }}", - "availability_template": "{{ is_state('availability_state.state', 'on') }}", - } - }, + ( + 1, + "{{ states('switch.test_state') }}", + "{{ is_state('availability_state.state', 'on') }}", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "availability_template"), + (ConfigurationStyle.MODERN, "availability"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_available_template_with_entities(hass: HomeAssistant) -> None: """Test availability templates with values from other entities.""" # When template returns true.. @@ -653,35 +817,39 @@ async def test_available_template_with_entities(hass: HomeAssistant) -> None: await hass.async_block_till_done() # Device State should not be unavailable - assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE + assert hass.states.get(TEST_ENTITY_ID).state != STATE_UNAVAILABLE # When Availability template returns false hass.states.async_set("availability_state.state", STATE_OFF) await hass.async_block_till_done() # device state should be unavailable - assert hass.states.get("lock.template_lock").state == STATE_UNAVAILABLE + assert hass.states.get(TEST_ENTITY_ID).state == STATE_UNAVAILABLE -@pytest.mark.parametrize(("count", "domain"), [(1, lock.DOMAIN)]) @pytest.mark.parametrize( - "config", + ("count", "state_template", "attribute_template"), [ - { - lock.DOMAIN: { - **OPTIMISTIC_LOCK_CONFIG, - "value_template": "{{ 1 + 1 }}", - "availability_template": "{{ x - 12 }}", - } - }, + ( + 1, + "{{ 1 + 1 }}", + "{{ x - 12 }}", + ) ], ) -@pytest.mark.usefixtures("start_ha") +@pytest.mark.parametrize( + ("style", "attribute"), + [ + (ConfigurationStyle.LEGACY, "availability_template"), + (ConfigurationStyle.MODERN, "availability"), + ], +) +@pytest.mark.usefixtures("setup_state_lock_with_attribute") async def test_invalid_availability_template_keeps_component_available( hass: HomeAssistant, caplog_setup_text ) -> None: """Test that an invalid availability keeps the device available.""" - assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE + assert hass.states.get(TEST_ENTITY_ID).state != STATE_UNAVAILABLE assert ("UndefinedError: 'x' is undefined") in caplog_setup_text @@ -700,7 +868,7 @@ async def test_invalid_availability_template_keeps_component_available( ], ) @pytest.mark.usefixtures("start_ha") -async def test_unique_id(hass: HomeAssistant) -> None: +async def test_legacy_unique_id(hass: HomeAssistant) -> None: """Test unique_id option only creates one lock per id.""" await setup.async_setup_component( hass, @@ -722,6 +890,85 @@ async def test_unique_id(hass: HomeAssistant) -> None: assert len(hass.states.async_all("lock")) == 1 +async def test_modern_unique_id(hass: HomeAssistant) -> None: + """Test unique_id option only creates one cover per id.""" + config = { + "template": { + "lock": [ + { + "name": "test_template_lock_01", + "unique_id": "not-so-unique-anymore", + "state": "{{ false }}", + **OPTIMISTIC_LOCK, + }, + { + "name": "test_template_lock_02", + "unique_id": "not-so-unique-anymore", + "state": "{{ false }}", + **OPTIMISTIC_LOCK, + }, + ] + } + } + + with assert_setup_component(1, template.DOMAIN): + assert await async_setup_component( + hass, + template.DOMAIN, + config, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1 + + +async def test_nested_unique_id( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test a template unique_id propagates to lock unique_ids.""" + with assert_setup_component(1, template.DOMAIN): + assert await async_setup_component( + hass, + template.DOMAIN, + { + "template": { + "unique_id": "x", + "lock": [ + { + **OPTIMISTIC_LOCK, + "name": "test_a", + "unique_id": "a", + "state": "{{ true }}", + }, + { + **OPTIMISTIC_LOCK, + "name": "test_b", + "unique_id": "b", + "state": "{{ true }}", + }, + ], + }, + }, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + assert len(hass.states.async_all("lock")) == 2 + + entry = entity_registry.async_get("lock.test_a") + assert entry + assert entry.unique_id == "x-a" + + entry = entity_registry.async_get("lock.test_b") + assert entry + assert entry.unique_id == "x-b" + + async def test_emtpy_action_config(hass: HomeAssistant) -> None: """Test configuration with empty script.""" with assert_setup_component(1, lock.DOMAIN):