mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Fix OpenGarage cover showing unknown while the door moves (#192081)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0ff4cf7427
commit
fdcc919bb0
@@ -1,5 +1,6 @@
|
||||
"""Platform for the opengarage.io cover component."""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import Any, cast, override
|
||||
|
||||
@@ -11,13 +12,30 @@ from homeassistant.components.cover import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .coordinator import OpenGarageConfigEntry, OpenGarageDataUpdateCoordinator
|
||||
from .entity import OpenGarageEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
STATES_MAP = {0: CoverState.CLOSED, 1: CoverState.OPEN}
|
||||
# A door stopped mid-travel is partially open: it is not closed, but it
|
||||
# has no known position and can still be opened.
|
||||
STATE_STOPPED = "stopped"
|
||||
|
||||
# Firmware 1.2.x reports door: 0 closed, 1 open, 2 stopped, 3 closing,
|
||||
# 4 opening, 5 unknown.
|
||||
STATES_MAP: dict[int, str] = {
|
||||
0: CoverState.CLOSED,
|
||||
1: CoverState.OPEN,
|
||||
2: STATE_STOPPED,
|
||||
3: CoverState.CLOSING,
|
||||
4: CoverState.OPENING,
|
||||
}
|
||||
|
||||
# After a command, the device keeps reporting the old state until the door
|
||||
# starts moving. Stop waiting if it still has not moved after this long.
|
||||
MOVEMENT_TIMEOUT = timedelta(seconds=60)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@@ -44,6 +62,7 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
||||
"""Initialize the cover."""
|
||||
self._state: str | None = None
|
||||
self._state_before_move: str | None = None
|
||||
self._movement_deadline: datetime | None = None
|
||||
|
||||
super().__init__(coordinator, device_id)
|
||||
|
||||
@@ -77,6 +96,7 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
||||
if self._state in [CoverState.CLOSED, CoverState.CLOSING]:
|
||||
return
|
||||
self._state_before_move = self._state
|
||||
self._movement_deadline = dt_util.utcnow() + MOVEMENT_TIMEOUT
|
||||
self._state = CoverState.CLOSING
|
||||
self.async_write_ha_state()
|
||||
await self._push_button()
|
||||
@@ -87,6 +107,7 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
||||
if self._state in [CoverState.OPEN, CoverState.OPENING]:
|
||||
return
|
||||
self._state_before_move = self._state
|
||||
self._movement_deadline = dt_util.utcnow() + MOVEMENT_TIMEOUT
|
||||
self._state = CoverState.OPENING
|
||||
self.async_write_ha_state()
|
||||
await self._push_button()
|
||||
@@ -98,12 +119,15 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
||||
status = self.coordinator.data
|
||||
|
||||
state = STATES_MAP.get(status.get("door")) # type: ignore[arg-type]
|
||||
if self._state_before_move is not None:
|
||||
if self._state_before_move != state:
|
||||
self._state = state
|
||||
self._state_before_move = None
|
||||
else:
|
||||
self._state = state
|
||||
if (
|
||||
self._movement_deadline is not None
|
||||
and dt_util.utcnow() < self._movement_deadline
|
||||
and state == self._state_before_move
|
||||
):
|
||||
return
|
||||
self._state = state
|
||||
self._state_before_move = None
|
||||
self._movement_deadline = None
|
||||
|
||||
@property
|
||||
@override
|
||||
@@ -130,4 +154,5 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
||||
|
||||
self._state = self._state_before_move
|
||||
self._state_before_move = None
|
||||
self._movement_deadline = None
|
||||
self.async_write_ha_state()
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from datetime import timedelta
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
@@ -14,7 +15,15 @@ from homeassistant.components.cover import (
|
||||
CoverService,
|
||||
CoverState,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_CLOSED, STATE_OPEN, TargetKey
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
STATE_CLOSED,
|
||||
STATE_CLOSING,
|
||||
STATE_OPEN,
|
||||
STATE_OPENING,
|
||||
STATE_UNKNOWN,
|
||||
TargetKey,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
@@ -55,6 +64,161 @@ async def test_cover_position(
|
||||
assert state.attributes[ATTR_CURRENT_POSITION] == expected_position
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("door_state", "expected_state", "expected_position"),
|
||||
[
|
||||
(2, STATE_OPEN, None),
|
||||
(3, STATE_CLOSING, None),
|
||||
(4, STATE_OPENING, None),
|
||||
(5, STATE_UNKNOWN, None),
|
||||
],
|
||||
ids=["stopped", "closing", "opening", "unknown"],
|
||||
)
|
||||
async def test_cover_extended_door_states(
|
||||
hass: HomeAssistant,
|
||||
mock_opengarage: MagicMock,
|
||||
init_integration: MockConfigEntry,
|
||||
door_state: int,
|
||||
expected_state: str,
|
||||
expected_position: int | None,
|
||||
) -> None:
|
||||
"""Test door states reported by firmware 1.2.x beyond open and closed."""
|
||||
await _simulate_door_state(hass, mock_opengarage, door_state)
|
||||
|
||||
state = hass.states.get("cover.garage_abcdef")
|
||||
assert state.state == expected_state
|
||||
assert state.attributes.get(ATTR_CURRENT_POSITION) == expected_position
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("door_states", "expected_states"),
|
||||
[
|
||||
([0, 4, 1], [STATE_CLOSED, STATE_OPENING, STATE_OPEN]),
|
||||
([1, 3, 0], [STATE_OPEN, STATE_CLOSING, STATE_CLOSED]),
|
||||
([1, 3, 2], [STATE_OPEN, STATE_CLOSING, STATE_OPEN]),
|
||||
],
|
||||
ids=["opening", "closing", "stopped_while_closing"],
|
||||
)
|
||||
async def test_cover_external_movement(
|
||||
hass: HomeAssistant,
|
||||
mock_opengarage: MagicMock,
|
||||
init_integration: MockConfigEntry,
|
||||
door_states: list[int],
|
||||
expected_states: list[str],
|
||||
) -> None:
|
||||
"""Test a door moved outside HA never passes through unknown."""
|
||||
for door_state, expected_state in zip(door_states, expected_states, strict=True):
|
||||
await _simulate_door_state(hass, mock_opengarage, door_state)
|
||||
assert hass.states.get("cover.garage_abcdef").state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service", "moving_door_state", "moving_state", "final_door_state", "final_state"),
|
||||
[
|
||||
(CoverService.OPEN, 4, STATE_OPENING, 1, STATE_OPEN),
|
||||
(CoverService.CLOSE, 3, STATE_CLOSING, 0, STATE_CLOSED),
|
||||
],
|
||||
ids=["open", "close"],
|
||||
)
|
||||
async def test_cover_command_from_stopped(
|
||||
hass: HomeAssistant,
|
||||
mock_opengarage: MagicMock,
|
||||
init_integration: MockConfigEntry,
|
||||
service: str,
|
||||
moving_door_state: int,
|
||||
moving_state: str,
|
||||
final_door_state: int,
|
||||
final_state: str,
|
||||
) -> None:
|
||||
"""Test a door stopped mid-travel can be opened or closed."""
|
||||
mock_opengarage.push_button.return_value = 1
|
||||
await _simulate_door_state(hass, mock_opengarage, 2)
|
||||
assert hass.states.get("cover.garage_abcdef").state == STATE_OPEN
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
service,
|
||||
{TargetKey.ENTITY_ID: "cover.garage_abcdef"},
|
||||
blocking=True,
|
||||
)
|
||||
assert mock_opengarage.push_button.call_count == 1
|
||||
assert hass.states.get("cover.garage_abcdef").state == moving_state
|
||||
|
||||
await _simulate_door_state(hass, mock_opengarage, moving_door_state)
|
||||
assert hass.states.get("cover.garage_abcdef").state == moving_state
|
||||
|
||||
await _simulate_door_state(hass, mock_opengarage, final_door_state)
|
||||
assert hass.states.get("cover.garage_abcdef").state == final_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("door_state", "service", "moving_state", "door_state_name"),
|
||||
[
|
||||
(2, CoverService.OPEN, STATE_OPENING, STATE_OPEN),
|
||||
(2, CoverService.CLOSE, STATE_CLOSING, STATE_OPEN),
|
||||
(0, CoverService.OPEN, STATE_OPENING, STATE_CLOSED),
|
||||
(1, CoverService.CLOSE, STATE_CLOSING, STATE_OPEN),
|
||||
],
|
||||
ids=["open_from_stopped", "close_from_stopped", "open", "close"],
|
||||
)
|
||||
async def test_cover_command_without_movement(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
mock_opengarage: MagicMock,
|
||||
init_integration: MockConfigEntry,
|
||||
door_state: int,
|
||||
service: str,
|
||||
moving_state: str,
|
||||
door_state_name: str,
|
||||
) -> None:
|
||||
"""Test a command that never moves the door does not leave it moving."""
|
||||
mock_opengarage.push_button.return_value = 1
|
||||
await _simulate_door_state(hass, mock_opengarage, door_state)
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
service,
|
||||
{TargetKey.ENTITY_ID: "cover.garage_abcdef"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("cover.garage_abcdef").state == moving_state
|
||||
|
||||
# The door may not start moving straight away.
|
||||
freezer.tick(timedelta(seconds=50))
|
||||
await _simulate_door_state(hass, mock_opengarage, door_state)
|
||||
assert hass.states.get("cover.garage_abcdef").state == moving_state
|
||||
|
||||
freezer.tick(timedelta(seconds=11))
|
||||
await _simulate_door_state(hass, mock_opengarage, door_state)
|
||||
assert hass.states.get("cover.garage_abcdef").state == door_state_name
|
||||
|
||||
|
||||
async def test_cover_command_with_firmware_transition(
|
||||
hass: HomeAssistant,
|
||||
mock_opengarage: MagicMock,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test a close command while the firmware reports the closing state."""
|
||||
mock_opengarage.push_button.return_value = 1
|
||||
await _simulate_door_state(hass, mock_opengarage, 1)
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
CoverService.CLOSE,
|
||||
{TargetKey.ENTITY_ID: "cover.garage_abcdef"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("cover.garage_abcdef").state == STATE_CLOSING
|
||||
|
||||
await _simulate_door_state(hass, mock_opengarage, 3)
|
||||
assert hass.states.get("cover.garage_abcdef").state == STATE_CLOSING
|
||||
|
||||
await _simulate_door_state(hass, mock_opengarage, 0)
|
||||
state = hass.states.get("cover.garage_abcdef")
|
||||
assert state.state == STATE_CLOSED
|
||||
assert state.attributes[ATTR_CURRENT_POSITION] == 0
|
||||
|
||||
|
||||
async def test_cover_position_during_transition(
|
||||
hass: HomeAssistant,
|
||||
mock_opengarage: MagicMock,
|
||||
|
||||
Reference in New Issue
Block a user