mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Reflect unavailable state when litter robot hasn't been seen recently (#70810)
This commit is contained in:
parent
5e3740d5ed
commit
1ede67e51f
@ -1,6 +1,7 @@
|
|||||||
"""Support for Litter-Robot "Vacuum"."""
|
"""Support for Litter-Robot "Vacuum"."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ from homeassistant.components.vacuum import (
|
|||||||
VacuumEntityFeature,
|
VacuumEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import STATE_OFF
|
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -34,6 +35,19 @@ SERVICE_RESET_WASTE_DRAWER = "reset_waste_drawer"
|
|||||||
SERVICE_SET_SLEEP_MODE = "set_sleep_mode"
|
SERVICE_SET_SLEEP_MODE = "set_sleep_mode"
|
||||||
SERVICE_SET_WAIT_TIME = "set_wait_time"
|
SERVICE_SET_WAIT_TIME = "set_wait_time"
|
||||||
|
|
||||||
|
LITTER_BOX_STATUS_STATE_MAP = {
|
||||||
|
LitterBoxStatus.CLEAN_CYCLE: STATE_CLEANING,
|
||||||
|
LitterBoxStatus.EMPTY_CYCLE: STATE_CLEANING,
|
||||||
|
LitterBoxStatus.CLEAN_CYCLE_COMPLETE: STATE_DOCKED,
|
||||||
|
LitterBoxStatus.CAT_SENSOR_TIMING: STATE_DOCKED,
|
||||||
|
LitterBoxStatus.DRAWER_FULL_1: STATE_DOCKED,
|
||||||
|
LitterBoxStatus.DRAWER_FULL_2: STATE_DOCKED,
|
||||||
|
LitterBoxStatus.READY: STATE_DOCKED,
|
||||||
|
LitterBoxStatus.CAT_SENSOR_INTERRUPTED: STATE_PAUSED,
|
||||||
|
LitterBoxStatus.OFF: STATE_OFF,
|
||||||
|
}
|
||||||
|
UNAVAILABLE_AFTER = timedelta(minutes=30)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -85,19 +99,10 @@ class LitterRobotCleaner(LitterRobotControlEntity, StateVacuumEntity):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> str:
|
||||||
"""Return the state of the cleaner."""
|
"""Return the state of the cleaner."""
|
||||||
switcher = {
|
if self.robot.last_seen < datetime.now(timezone.utc) - UNAVAILABLE_AFTER:
|
||||||
LitterBoxStatus.CLEAN_CYCLE: STATE_CLEANING,
|
return STATE_UNAVAILABLE
|
||||||
LitterBoxStatus.EMPTY_CYCLE: STATE_CLEANING,
|
|
||||||
LitterBoxStatus.CLEAN_CYCLE_COMPLETE: STATE_DOCKED,
|
|
||||||
LitterBoxStatus.CAT_SENSOR_TIMING: STATE_DOCKED,
|
|
||||||
LitterBoxStatus.DRAWER_FULL_1: STATE_DOCKED,
|
|
||||||
LitterBoxStatus.DRAWER_FULL_2: STATE_DOCKED,
|
|
||||||
LitterBoxStatus.READY: STATE_DOCKED,
|
|
||||||
LitterBoxStatus.CAT_SENSOR_INTERRUPTED: STATE_PAUSED,
|
|
||||||
LitterBoxStatus.OFF: STATE_OFF,
|
|
||||||
}
|
|
||||||
|
|
||||||
return switcher.get(self.robot.status, STATE_ERROR)
|
return LITTER_BOX_STATUS_STATE_MAP.get(self.robot.status, STATE_ERROR)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def status(self) -> str:
|
def status(self) -> str:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Configure pytest for Litter-Robot tests."""
|
"""Configure pytest for Litter-Robot tests."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
@ -9,6 +10,7 @@ from pylitterbot.exceptions import InvalidCommandException
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import litterrobot
|
from homeassistant.components import litterrobot
|
||||||
|
from homeassistant.components.litterrobot.vacuum import UNAVAILABLE_AFTER
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .common import CONFIG, ROBOT_DATA
|
from .common import CONFIG, ROBOT_DATA
|
||||||
@ -65,6 +67,14 @@ def mock_account_with_sleeping_robot() -> MagicMock:
|
|||||||
return create_mock_account({"sleepModeActive": "102:00:00"})
|
return create_mock_account({"sleepModeActive": "102:00:00"})
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_account_with_robot_not_recently_seen() -> MagicMock:
|
||||||
|
"""Mock a Litter-Robot account with a sleeping robot."""
|
||||||
|
return create_mock_account(
|
||||||
|
{"lastSeen": (datetime.now() - UNAVAILABLE_AFTER).isoformat()}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_account_with_error() -> MagicMock:
|
def mock_account_with_error() -> MagicMock:
|
||||||
"""Mock a Litter-Robot account with error."""
|
"""Mock a Litter-Robot account with error."""
|
||||||
|
@ -24,7 +24,7 @@ from homeassistant.components.vacuum import (
|
|||||||
STATE_DOCKED,
|
STATE_DOCKED,
|
||||||
STATE_ERROR,
|
STATE_ERROR,
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
@ -62,6 +62,19 @@ async def test_vacuum_status_when_sleeping(
|
|||||||
assert vacuum.attributes.get(ATTR_STATUS) == "Ready (Sleeping)"
|
assert vacuum.attributes.get(ATTR_STATUS) == "Ready (Sleeping)"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_vacuum_state_when_not_recently_seen(
|
||||||
|
hass: HomeAssistant, mock_account_with_robot_not_recently_seen: MagicMock
|
||||||
|
) -> None:
|
||||||
|
"""Tests the vacuum state when not seen recently."""
|
||||||
|
await setup_integration(
|
||||||
|
hass, mock_account_with_robot_not_recently_seen, PLATFORM_DOMAIN
|
||||||
|
)
|
||||||
|
|
||||||
|
vacuum = hass.states.get(VACUUM_ENTITY_ID)
|
||||||
|
assert vacuum
|
||||||
|
assert vacuum.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
async def test_no_robots(
|
async def test_no_robots(
|
||||||
hass: HomeAssistant, mock_account_with_no_robots: MagicMock
|
hass: HomeAssistant, mock_account_with_no_robots: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user