mirror of
https://github.com/home-assistant/core.git
synced 2026-12-05 21:29:58 -04:00
Track Overkiz executions started outside Home Assistant (#188972)
This commit is contained in:
@@ -191,8 +191,11 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
||||
return areas
|
||||
|
||||
@callback
|
||||
def async_execution_started(self) -> None:
|
||||
"""Show the in-progress state and poll fast until executions finish."""
|
||||
def async_execution_started(self, exec_id: str, actions: list[Action]) -> None:
|
||||
"""Track a new execution, show its in-progress state and poll fast."""
|
||||
# ExecutionRegisteredEvent only arrives with the next event poll, so
|
||||
# track the execution now for entities to reflect it straight away.
|
||||
self.register_execution(exec_id, actions)
|
||||
self.async_update_listeners()
|
||||
if not self.is_stateless and self.update_interval != UPDATE_INTERVAL_EXECUTION:
|
||||
self.update_interval = UPDATE_INTERVAL_EXECUTION
|
||||
@@ -303,7 +306,8 @@ async def on_execution_registered(
|
||||
coordinator: OverkizDataUpdateCoordinator, event: ExecutionRegisteredEvent
|
||||
) -> None:
|
||||
"""Handle execution registered event."""
|
||||
coordinator.executions.setdefault(event.exec_id, set())
|
||||
# Also covers executions started outside Home Assistant (e.g. the TaHoma app)
|
||||
coordinator.register_execution(event.exec_id, event.actions)
|
||||
|
||||
if not coordinator.is_stateless:
|
||||
coordinator.update_interval = UPDATE_INTERVAL_EXECUTION
|
||||
|
||||
@@ -73,10 +73,7 @@ class OverkizExecutor:
|
||||
) as exception:
|
||||
raise HomeAssistantError("Failed to connect") from exception
|
||||
|
||||
# ExecutionRegisteredEvent doesn't contain the device_url, thus we need
|
||||
# to register it here.
|
||||
self.coordinator.register_execution(exec_id, actions)
|
||||
self.coordinator.async_execution_started()
|
||||
self.coordinator.async_execution_started(exec_id, actions)
|
||||
|
||||
async def async_cancel_command(
|
||||
self, commands_to_cancel: list[OverkizCommand]
|
||||
|
||||
@@ -6,6 +6,8 @@ from typing import Any
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pyoverkiz.enums import DataType, EventName, ExecutionState
|
||||
from pyoverkiz.models import (
|
||||
Action,
|
||||
Command,
|
||||
DeviceAvailableEvent,
|
||||
DeviceCreatedEvent,
|
||||
DeviceRemovedEvent,
|
||||
@@ -13,6 +15,7 @@ from pyoverkiz.models import (
|
||||
DeviceUnavailableEvent,
|
||||
Event,
|
||||
EventState,
|
||||
ExecutionRegisteredEvent,
|
||||
ExecutionStateChangedEvent,
|
||||
GatewayAliveEvent,
|
||||
GatewayDownEvent,
|
||||
@@ -104,6 +107,24 @@ def device_created_event(device_url: str) -> DeviceCreatedEvent:
|
||||
return DeviceCreatedEvent(name=EventName.DEVICE_CREATED, device_url=device_url)
|
||||
|
||||
|
||||
def execution_registered_event(
|
||||
exec_id: str,
|
||||
device_url: str,
|
||||
command_name: str,
|
||||
*,
|
||||
label: str = "Home Assistant",
|
||||
source: str | None = None,
|
||||
) -> ExecutionRegisteredEvent:
|
||||
"""Build an EXECUTION_REGISTERED event for a single device command."""
|
||||
return ExecutionRegisteredEvent(
|
||||
name=EventName.EXECUTION_REGISTERED,
|
||||
exec_id=exec_id,
|
||||
label=label,
|
||||
source=source,
|
||||
actions=[Action(device_url=device_url, commands=[Command(name=command_name)])],
|
||||
)
|
||||
|
||||
|
||||
def execution_state_changed_event(
|
||||
exec_id: str, new_state: ExecutionState, old_state: ExecutionState
|
||||
) -> ExecutionStateChangedEvent:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Tests for the Overkiz data update coordinator."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from aiohttp import ClientConnectorError
|
||||
@@ -40,6 +41,7 @@ from .helpers import (
|
||||
device_created_event,
|
||||
device_removed_event,
|
||||
device_state_changed_event,
|
||||
execution_registered_event,
|
||||
execution_state_changed_event,
|
||||
gateway_alive_event,
|
||||
gateway_down_event,
|
||||
@@ -250,6 +252,170 @@ async def test_commands_share_the_execution_poll(
|
||||
assert mock_client.fetch_events.await_count == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"final_state",
|
||||
[ExecutionState.COMPLETED, ExecutionState.FAILED],
|
||||
ids=["completed", "failed"],
|
||||
)
|
||||
async def test_execution_polls_fast_until_it_finishes(
|
||||
hass: HomeAssistant,
|
||||
setup_overkiz_integration: SetupOverkizIntegration,
|
||||
mock_client: MockOverkizClient,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
final_state: ExecutionState,
|
||||
) -> None:
|
||||
"""Events are fetched every execution interval until the execution finishes."""
|
||||
await setup_overkiz_integration(fixture=SHUTTER.fixture)
|
||||
mock_client.fetch_events.reset_mock()
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_OPEN_COVER,
|
||||
{ATTR_ENTITY_ID: SHUTTER.entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 0
|
||||
assert hass.states.get(SHUTTER.entity_id).state == CoverState.OPENING
|
||||
|
||||
# The registered event for our own execution must not drop its commands.
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
[execution_registered_event("exec-1", SHUTTER.device_url, "open")],
|
||||
update_interval=UPDATE_INTERVAL_EXECUTION,
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 1
|
||||
assert hass.states.get(SHUTTER.entity_id).state == CoverState.OPENING
|
||||
|
||||
await async_deliver_events(
|
||||
hass, freezer, mock_client, update_interval=UPDATE_INTERVAL_EXECUTION
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 2
|
||||
assert hass.states.get(SHUTTER.entity_id).state == CoverState.OPENING
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
[
|
||||
execution_state_changed_event(
|
||||
"exec-1", final_state, ExecutionState.IN_PROGRESS
|
||||
)
|
||||
],
|
||||
update_interval=UPDATE_INTERVAL_EXECUTION,
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 3
|
||||
assert hass.states.get(SHUTTER.entity_id).state == CoverState.CLOSED
|
||||
|
||||
await async_deliver_events(
|
||||
hass, freezer, mock_client, update_interval=UPDATE_INTERVAL_EXECUTION
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 3
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
update_interval=UPDATE_INTERVAL - UPDATE_INTERVAL_EXECUTION,
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 4
|
||||
|
||||
|
||||
async def test_command_during_execution_poll_keeps_its_schedule(
|
||||
hass: HomeAssistant,
|
||||
setup_overkiz_integration: SetupOverkizIntegration,
|
||||
mock_client: MockOverkizClient,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""A command sent while the execution poll is scheduled doesn't postpone it."""
|
||||
# Refreshes are scheduled up to a second off, so the default 2 second
|
||||
# interval is too short to tell a postponed poll apart.
|
||||
execution_interval = timedelta(seconds=10)
|
||||
with patch(
|
||||
"homeassistant.components.overkiz.coordinator.UPDATE_INTERVAL_EXECUTION",
|
||||
execution_interval,
|
||||
):
|
||||
await setup_overkiz_integration(fixture=SHUTTER.fixture)
|
||||
mock_client.fetch_events.reset_mock()
|
||||
|
||||
for cover in (SHUTTER, GARAGE):
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_OPEN_COVER,
|
||||
{ATTR_ENTITY_ID: cover.entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
await async_deliver_events(
|
||||
hass, freezer, mock_client, update_interval=execution_interval / 2
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 1
|
||||
|
||||
|
||||
async def test_external_execution_polls_fast_until_it_finishes(
|
||||
hass: HomeAssistant,
|
||||
setup_overkiz_integration: SetupOverkizIntegration,
|
||||
mock_client: MockOverkizClient,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""An execution started outside Home Assistant also speeds up polling."""
|
||||
await setup_overkiz_integration(fixture=SHUTTER.fixture)
|
||||
mock_client.fetch_events.reset_mock()
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
[
|
||||
execution_registered_event(
|
||||
"app-exec",
|
||||
SHUTTER.device_url,
|
||||
"open",
|
||||
label="Shutter - TaHoma PC",
|
||||
source="mobile:pc",
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 1
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
[
|
||||
execution_state_changed_event(
|
||||
"app-exec", ExecutionState.COMPLETED, ExecutionState.IN_PROGRESS
|
||||
)
|
||||
],
|
||||
update_interval=UPDATE_INTERVAL_EXECUTION,
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 2
|
||||
|
||||
await async_deliver_events(
|
||||
hass, freezer, mock_client, update_interval=UPDATE_INTERVAL_EXECUTION
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 2
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
update_interval=UPDATE_INTERVAL - UPDATE_INTERVAL_EXECUTION,
|
||||
)
|
||||
|
||||
assert mock_client.fetch_events.await_count == 3
|
||||
|
||||
|
||||
async def test_stateless_hub_skips_the_execution_poll(
|
||||
hass: HomeAssistant,
|
||||
setup_overkiz_integration: SetupOverkizIntegration,
|
||||
|
||||
@@ -50,6 +50,7 @@ from .helpers import (
|
||||
async_deliver_events,
|
||||
device_state_changed_event,
|
||||
device_unavailable_event,
|
||||
execution_registered_event,
|
||||
execution_state_changed_event,
|
||||
)
|
||||
|
||||
@@ -611,6 +612,58 @@ async def test_merged_action_groups_keep_per_device_tracking(
|
||||
assert hass.states.get(GARAGE.entity_id).state == CoverState.CLOSED
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("command_name", "expected_state"),
|
||||
[
|
||||
pytest.param("open", CoverState.OPENING, id="open"),
|
||||
pytest.param("close", CoverState.CLOSING, id="close"),
|
||||
],
|
||||
)
|
||||
async def test_external_execution_tracks_movement(
|
||||
hass: HomeAssistant,
|
||||
setup_overkiz_integration: SetupOverkizIntegration,
|
||||
mock_client: MockOverkizClient,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
command_name: str,
|
||||
expected_state: CoverState,
|
||||
) -> None:
|
||||
"""Test that an execution started outside Home Assistant reports movement."""
|
||||
await setup_overkiz_integration(fixture=SHUTTER.fixture)
|
||||
assert hass.states.get(SHUTTER.entity_id).state == CoverState.CLOSED
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
[
|
||||
execution_registered_event(
|
||||
exec_id="app-exec",
|
||||
device_url=SHUTTER.device_url,
|
||||
command_name=command_name,
|
||||
label="Shutter - TaHoma PC",
|
||||
source="mobile:pc",
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
assert hass.states.get(SHUTTER.entity_id).state == expected_state
|
||||
|
||||
await async_deliver_events(
|
||||
hass,
|
||||
freezer,
|
||||
mock_client,
|
||||
[
|
||||
execution_state_changed_event(
|
||||
exec_id="app-exec",
|
||||
new_state=ExecutionState.COMPLETED,
|
||||
old_state=ExecutionState.IN_PROGRESS,
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
assert hass.states.get(SHUTTER.entity_id).state == CoverState.CLOSED
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user