mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 12:17:23 +00:00
[cover] Fold ControlAction/CoverPublishAction fields into stateless lambdas (#16046)
This commit is contained in:
111
tests/integration/fixtures/cover_control_action.yaml
Normal file
111
tests/integration/fixtures/cover_control_action.yaml
Normal file
@@ -0,0 +1,111 @@
|
||||
esphome:
|
||||
name: cover-control-action-test
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
globals:
|
||||
- id: test_position
|
||||
type: float
|
||||
initial_value: "0.42"
|
||||
|
||||
cover:
|
||||
- platform: template
|
||||
name: "Test Cover"
|
||||
id: test_cover
|
||||
has_position: true
|
||||
optimistic: true
|
||||
assumed_state: true
|
||||
open_action:
|
||||
- cover.template.publish:
|
||||
id: test_cover
|
||||
position: 1.0
|
||||
close_action:
|
||||
- cover.template.publish:
|
||||
id: test_cover
|
||||
position: 0.0
|
||||
stop_action:
|
||||
- cover.template.publish:
|
||||
id: test_cover
|
||||
current_operation: IDLE
|
||||
tilt_action:
|
||||
- lambda: |-
|
||||
// Manually set tilt and publish
|
||||
id(test_cover).tilt = tilt;
|
||||
id(test_cover).publish_state();
|
||||
|
||||
button:
|
||||
# cover.control: position only
|
||||
- platform: template
|
||||
id: btn_position
|
||||
name: "Set Position"
|
||||
on_press:
|
||||
- cover.control:
|
||||
id: test_cover
|
||||
position: 50%
|
||||
|
||||
# cover.control: tilt only
|
||||
- platform: template
|
||||
id: btn_tilt
|
||||
name: "Set Tilt"
|
||||
on_press:
|
||||
- cover.control:
|
||||
id: test_cover
|
||||
tilt: 75%
|
||||
|
||||
# cover.control: position + tilt
|
||||
- platform: template
|
||||
id: btn_pos_tilt
|
||||
name: "Set Pos Tilt"
|
||||
on_press:
|
||||
- cover.control:
|
||||
id: test_cover
|
||||
position: 25%
|
||||
tilt: 30%
|
||||
|
||||
# cover.control: state alias for position
|
||||
- platform: template
|
||||
id: btn_open_state
|
||||
name: "Open State"
|
||||
on_press:
|
||||
- cover.control:
|
||||
id: test_cover
|
||||
state: OPEN
|
||||
|
||||
# cover.control: lambda position (exercises lambda path)
|
||||
- platform: template
|
||||
id: btn_lambda_position
|
||||
name: "Lambda Position"
|
||||
on_press:
|
||||
- cover.control:
|
||||
id: test_cover
|
||||
position: !lambda "return id(test_position);"
|
||||
|
||||
# cover.template.publish: position only
|
||||
- platform: template
|
||||
id: btn_publish_pos
|
||||
name: "Publish Pos"
|
||||
on_press:
|
||||
- cover.template.publish:
|
||||
id: test_cover
|
||||
position: 0.6
|
||||
|
||||
# cover.template.publish: current_operation only
|
||||
- platform: template
|
||||
id: btn_publish_op
|
||||
name: "Publish Op"
|
||||
on_press:
|
||||
- cover.template.publish:
|
||||
id: test_cover
|
||||
current_operation: OPENING
|
||||
|
||||
# cover.control: stop only — runs after Publish Op so the test can
|
||||
# verify current_operation transitions OPENING -> IDLE.
|
||||
- platform: template
|
||||
id: btn_stop
|
||||
name: "Stop Cover"
|
||||
on_press:
|
||||
- cover.control:
|
||||
id: test_cover
|
||||
stop: true
|
||||
92
tests/integration/test_cover_control_action.py
Normal file
92
tests/integration/test_cover_control_action.py
Normal file
@@ -0,0 +1,92 @@
|
||||
"""Integration test for cover ControlAction and CoverPublishAction.
|
||||
|
||||
Tests that cover.control and cover.template.publish automation actions
|
||||
work correctly with the single stateless apply lambda/function pointer
|
||||
implementation. Exercises multiple field combinations and the lambda path.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from aioesphomeapi import ButtonInfo, CoverInfo, CoverState, EntityState
|
||||
import pytest
|
||||
|
||||
from .state_utils import InitialStateHelper, require_entity
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cover_control_action(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""Test cover ControlAction/CoverPublishAction with constants and lambdas."""
|
||||
loop = asyncio.get_running_loop()
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
cover_state_future: asyncio.Future[CoverState] | None = None
|
||||
|
||||
def on_state(state: EntityState) -> None:
|
||||
if (
|
||||
isinstance(state, CoverState)
|
||||
and cover_state_future is not None
|
||||
and not cover_state_future.done()
|
||||
):
|
||||
cover_state_future.set_result(state)
|
||||
|
||||
async def wait_for_cover_state(timeout: float = 5.0) -> CoverState:
|
||||
nonlocal cover_state_future
|
||||
cover_state_future = loop.create_future()
|
||||
try:
|
||||
return await asyncio.wait_for(cover_state_future, timeout)
|
||||
finally:
|
||||
cover_state_future = None
|
||||
|
||||
entities, _ = await client.list_entities_services()
|
||||
initial_state_helper = InitialStateHelper(entities)
|
||||
client.subscribe_states(initial_state_helper.on_state_wrapper(on_state))
|
||||
await initial_state_helper.wait_for_initial_states()
|
||||
|
||||
require_entity(entities, "test_cover", CoverInfo)
|
||||
|
||||
async def press_and_wait(name: str) -> CoverState:
|
||||
btn = require_entity(entities, name.lower().replace(" ", "_"), ButtonInfo)
|
||||
client.button_command(btn.key)
|
||||
return await wait_for_cover_state()
|
||||
|
||||
# cover.control: position only
|
||||
state = await press_and_wait("Set Position")
|
||||
assert state.position == pytest.approx(0.5, abs=0.01)
|
||||
|
||||
# cover.control: tilt only
|
||||
state = await press_and_wait("Set Tilt")
|
||||
assert state.tilt == pytest.approx(0.75, abs=0.01)
|
||||
|
||||
# cover.control: position + tilt
|
||||
state = await press_and_wait("Set Pos Tilt")
|
||||
assert state.position == pytest.approx(0.25, abs=0.01)
|
||||
assert state.tilt == pytest.approx(0.30, abs=0.01)
|
||||
|
||||
# cover.control: state alias for position 1.0
|
||||
state = await press_and_wait("Open State")
|
||||
assert state.position == pytest.approx(1.0, abs=0.01)
|
||||
|
||||
# cover.control: lambda position (test_position global = 0.42)
|
||||
state = await press_and_wait("Lambda Position")
|
||||
assert state.position == pytest.approx(0.42, abs=0.01)
|
||||
|
||||
# cover.template.publish: position only
|
||||
state = await press_and_wait("Publish Pos")
|
||||
assert state.position == pytest.approx(0.6, abs=0.01)
|
||||
|
||||
# cover.template.publish: current_operation only
|
||||
state = await press_and_wait("Publish Op")
|
||||
# CoverOperation.OPENING == 1
|
||||
assert state.current_operation == 1
|
||||
|
||||
# cover.control: stop only — template cover's stop_action publishes
|
||||
# current_operation: IDLE.
|
||||
state = await press_and_wait("Stop Cover")
|
||||
# CoverOperation.IDLE == 0
|
||||
assert state.current_operation == 0
|
||||
Reference in New Issue
Block a user