Update template cover to use assumed attribute framework (#182507)

This commit is contained in:
Petro31
2026-10-06 20:02:30 +02:00
committed by GitHub
parent 6f769d42a3
commit 275f402cf6
3 changed files with 150 additions and 33 deletions
+19 -30
View File
@@ -232,11 +232,13 @@ class AbstractTemplateCover(AbstractTemplateEntity, CoverEntity, RestoreEntity):
"_attr_current_cover_tilt_position",
tcv.number(self, CONF_TILT, 0, 100),
)
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._tilt_optimistic = (
config.get(CONF_TILT_OPTIMISTIC) or CONF_TILT not in self._templates
self.add_assumed_attribute(
"_attr_current_cover_tilt_position",
CONF_TILT,
TILT_ACTION,
optimistic_option=CONF_TILT_OPTIMISTIC,
)
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
# The config requires (open and close scripts) or a set position script,
# therefore the base supported features will always include them.
@@ -320,50 +322,37 @@ class AbstractTemplateCover(AbstractTemplateEntity, CoverEntity, RestoreEntity):
@override
async def async_set_cover_position(self, **kwargs: Any) -> None:
"""Set cover position."""
self._attr_current_cover_position = kwargs[ATTR_POSITION]
position = kwargs[ATTR_POSITION]
await self.async_run_script(
self._action_scripts[POSITION_ACTION],
run_variables={"position": self._attr_current_cover_position},
run_variables={"position": position},
context=self._context,
)
if self._attr_assumed_state:
self._attr_current_cover_position = position
self.async_write_ha_state()
async def _set_tilt_position(self, position: int) -> None:
if script := self._action_scripts.get(TILT_ACTION):
await self.async_run_script(
script, run_variables={"tilt": position}, context=self._context
)
self.write_assumed_attribute(CONF_TILT, position)
@override
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
"""Tilt the cover open."""
self._attr_current_cover_tilt_position = 100
await self.async_run_script(
self._action_scripts[TILT_ACTION],
run_variables={"tilt": self._attr_current_cover_tilt_position},
context=self._context,
)
if self._tilt_optimistic:
self.async_write_ha_state()
await self._set_tilt_position(100)
@override
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
"""Tilt the cover closed."""
self._attr_current_cover_tilt_position = 0
await self.async_run_script(
self._action_scripts[TILT_ACTION],
run_variables={"tilt": self._attr_current_cover_tilt_position},
context=self._context,
)
if self._tilt_optimistic:
self.async_write_ha_state()
await self._set_tilt_position(0)
@override
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
"""Move the cover tilt to a specific position."""
self._attr_current_cover_tilt_position = kwargs[ATTR_TILT_POSITION]
await self.async_run_script(
self._action_scripts[TILT_ACTION],
run_variables={"tilt": self._attr_current_cover_tilt_position},
context=self._context,
)
if self._tilt_optimistic:
self.async_write_ha_state()
await self._set_tilt_position(kwargs[ATTR_TILT_POSITION])
@property
@override
+19 -2
View File
@@ -202,9 +202,26 @@ class AbstractTemplateEntity(Entity):
domain,
)
def add_assumed_attribute(self, attr: str, option: str, action_option: str):
def add_assumed_attribute(
self,
attr: str,
option: str,
action_option: str,
*,
optimistic_option: str | None = None,
):
"""Add an optimistic option."""
if option not in self._config and action_option in self._config:
if action_option not in self._config:
return
if optimistic_option is None:
if option not in self._config:
self._assumed_attributes[option] = attr
return
if (optimistic_override := self._config.get(optimistic_option)) or (
not optimistic_override and option not in self._config
):
self._assumed_attributes[option] = attr
def update_assumed_attribute(self, option: str, value: Any) -> bool:
+112 -1
View File
@@ -773,7 +773,10 @@ async def test_non_optimistic_template_with_optimistic_state(
@pytest.mark.parametrize(
("count", "position_template", "config"),
[(1, "{{ 100 }}", SET_COVER_TILT_POSITION)],
[
(1, "{{ 100 }}", SET_COVER_TILT_POSITION),
(1, "{{ 100 }}", {"tilt_optimistic": False, **SET_COVER_TILT_POSITION}),
],
)
@pytest.mark.parametrize(
"style",
@@ -811,6 +814,114 @@ async def test_set_tilt_position_optimistic(
assert state.attributes.get("current_tilt_position") == pos
@pytest.mark.parametrize(
("count", "position_template", "config"),
[
(
1,
"{{ 100 }}",
{
"tilt": "{{ states('sensor.test_state') | float }}",
"tilt_optimistic": True,
**SET_COVER_TILT_POSITION,
},
),
],
)
@pytest.mark.parametrize(
"style",
[ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER],
)
@pytest.mark.usefixtures("setup_position_cover")
async def test_set_tilt_position_optimistic_with_tilt_optimistic_true(
hass: HomeAssistant, calls: list[ServiceCall]
) -> None:
"""Test the optimistic tilt_position mode with tilt_optimistic true."""
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") is None
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_SET_COVER_TILT_POSITION,
{ATTR_ENTITY_ID: TEST_COVER.entity_id, ATTR_TILT_POSITION: 42},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") == 42.0
for service, pos in (
(SERVICE_CLOSE_COVER_TILT, 0.0),
(SERVICE_OPEN_COVER_TILT, 100.0),
(SERVICE_TOGGLE_COVER_TILT, 0.0),
(SERVICE_TOGGLE_COVER_TILT, 100.0),
):
await hass.services.async_call(
COVER_DOMAIN, service, {ATTR_ENTITY_ID: TEST_COVER.entity_id}, blocking=True
)
await hass.async_block_till_done()
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") == pos
await async_trigger(hass, TEST_STATE_ENTITY_ID, 45)
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") == 45.0
@pytest.mark.parametrize(
("count", "position_template", "config"),
[
(
1,
"{{ 100 }}",
{
"tilt": "{{ states('sensor.test_state') | float }}",
"tilt_optimistic": False,
**SET_COVER_TILT_POSITION,
},
)
],
)
@pytest.mark.parametrize(
"style",
[ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER],
)
@pytest.mark.usefixtures("setup_position_cover")
async def test_set_tilt_position_optimistic_with_tilt_optimistic_false(
hass: HomeAssistant, calls: list[ServiceCall]
) -> None:
"""Test the optimistic tilt_position mode with tilt_optimistic false."""
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") is None
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_SET_COVER_TILT_POSITION,
{ATTR_ENTITY_ID: TEST_COVER.entity_id, ATTR_TILT_POSITION: 42},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") is None
for service in (
SERVICE_CLOSE_COVER_TILT,
SERVICE_OPEN_COVER_TILT,
SERVICE_TOGGLE_COVER_TILT,
SERVICE_TOGGLE_COVER_TILT,
):
await hass.services.async_call(
COVER_DOMAIN, service, {ATTR_ENTITY_ID: TEST_COVER.entity_id}, blocking=True
)
await hass.async_block_till_done()
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") is None
await async_trigger(hass, TEST_STATE_ENTITY_ID, 45)
state = hass.states.get(TEST_COVER.entity_id)
assert state.attributes.get("current_tilt_position") == 45.0
@pytest.mark.parametrize(
("count", "state_template", "attribute_template"),
[