mirror of
https://github.com/home-assistant/core.git
synced 2026-11-30 01:33:12 -04:00
Add litterrobot integration (#45886)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Tests for the Litter-Robot Component."""
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Common utils for Litter-Robot tests."""
|
||||
from homeassistant.components.litterrobot import DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
|
||||
BASE_PATH = "homeassistant.components.litterrobot"
|
||||
CONFIG = {DOMAIN: {CONF_USERNAME: "user@example.com", CONF_PASSWORD: "password"}}
|
||||
|
||||
ROBOT_NAME = "Test"
|
||||
ROBOT_SERIAL = "LR3C012345"
|
||||
ROBOT_DATA = {
|
||||
"powerStatus": "AC",
|
||||
"lastSeen": "2021-02-01T15:30:00.000000",
|
||||
"cleanCycleWaitTimeMinutes": "7",
|
||||
"unitStatus": "RDY",
|
||||
"litterRobotNickname": ROBOT_NAME,
|
||||
"cycleCount": "15",
|
||||
"panelLockActive": "0",
|
||||
"cyclesAfterDrawerFull": "0",
|
||||
"litterRobotSerial": ROBOT_SERIAL,
|
||||
"cycleCapacity": "30",
|
||||
"litterRobotId": "a0123b4567cd8e",
|
||||
"nightLightActive": "1",
|
||||
"sleepModeActive": "112:50:19",
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Configure pytest for Litter-Robot tests."""
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from pylitterbot import Robot
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import litterrobot
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .common import ROBOT_DATA
|
||||
|
||||
|
||||
def create_mock_robot(hass):
|
||||
"""Create a mock Litter-Robot device."""
|
||||
robot = Robot(data=ROBOT_DATA)
|
||||
robot.start_cleaning = AsyncMock()
|
||||
robot.set_power_status = AsyncMock()
|
||||
robot.reset_waste_drawer = AsyncMock()
|
||||
robot.set_sleep_mode = AsyncMock()
|
||||
return robot
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_hub(hass):
|
||||
"""Mock a Litter-Robot hub."""
|
||||
hub = MagicMock(
|
||||
hass=hass,
|
||||
account=MagicMock(),
|
||||
logged_in=True,
|
||||
coordinator=MagicMock(spec=DataUpdateCoordinator),
|
||||
spec=litterrobot.LitterRobotHub,
|
||||
)
|
||||
hub.coordinator.last_update_success = True
|
||||
hub.account.robots = [create_mock_robot(hass)]
|
||||
return hub
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Test the Litter-Robot config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
|
||||
from .common import CONF_USERNAME, CONFIG, DOMAIN
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.litterrobot.config_flow.LitterRobotHub.login",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.litterrobot.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.litterrobot.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], CONFIG[DOMAIN]
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == CONFIG[DOMAIN][CONF_USERNAME]
|
||||
assert result2["data"] == CONFIG[DOMAIN]
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.litterrobot.config_flow.LitterRobotHub.login",
|
||||
side_effect=LitterRobotLoginException,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], CONFIG[DOMAIN]
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.litterrobot.config_flow.LitterRobotHub.login",
|
||||
side_effect=LitterRobotException,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], CONFIG[DOMAIN]
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass):
|
||||
"""Test we handle unknown error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.litterrobot.config_flow.LitterRobotHub.login",
|
||||
side_effect=Exception,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], CONFIG[DOMAIN]
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Test Litter-Robot setup process."""
|
||||
from homeassistant.components import litterrobot
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .common import CONFIG
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test being able to unload an entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain=litterrobot.DOMAIN,
|
||||
data=CONFIG[litterrobot.DOMAIN],
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert await async_setup_component(hass, litterrobot.DOMAIN, {}) is True
|
||||
assert await litterrobot.async_unload_entry(hass, entry)
|
||||
assert hass.data[litterrobot.DOMAIN] == {}
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Test the Litter-Robot vacuum entity."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import litterrobot
|
||||
from homeassistant.components.litterrobot.hub import REFRESH_WAIT_TIME
|
||||
from homeassistant.components.vacuum import (
|
||||
ATTR_PARAMS,
|
||||
DOMAIN as PLATFORM_DOMAIN,
|
||||
SERVICE_SEND_COMMAND,
|
||||
SERVICE_START,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_DOCKED,
|
||||
)
|
||||
from homeassistant.const import ATTR_COMMAND, ATTR_ENTITY_ID
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .common import CONFIG
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
ENTITY_ID = "vacuum.test_litter_box"
|
||||
|
||||
|
||||
async def setup_hub(hass, mock_hub):
|
||||
"""Load the Litter-Robot vacuum platform with the provided hub."""
|
||||
hass.config.components.add(litterrobot.DOMAIN)
|
||||
entry = MockConfigEntry(
|
||||
domain=litterrobot.DOMAIN,
|
||||
data=CONFIG[litterrobot.DOMAIN],
|
||||
)
|
||||
|
||||
with patch.dict(hass.data, {litterrobot.DOMAIN: {entry.entry_id: mock_hub}}):
|
||||
await hass.config_entries.async_forward_entry_setup(entry, PLATFORM_DOMAIN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_vacuum(hass, mock_hub):
|
||||
"""Tests the vacuum entity was set up."""
|
||||
await setup_hub(hass, mock_hub)
|
||||
|
||||
vacuum = hass.states.get(ENTITY_ID)
|
||||
assert vacuum is not None
|
||||
assert vacuum.state == STATE_DOCKED
|
||||
assert vacuum.attributes["is_sleeping"] is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"service,command,extra",
|
||||
[
|
||||
(SERVICE_START, "start_cleaning", None),
|
||||
(SERVICE_TURN_OFF, "set_power_status", None),
|
||||
(SERVICE_TURN_ON, "set_power_status", None),
|
||||
(
|
||||
SERVICE_SEND_COMMAND,
|
||||
"reset_waste_drawer",
|
||||
{ATTR_COMMAND: "reset_waste_drawer"},
|
||||
),
|
||||
(
|
||||
SERVICE_SEND_COMMAND,
|
||||
"set_sleep_mode",
|
||||
{
|
||||
ATTR_COMMAND: "set_sleep_mode",
|
||||
ATTR_PARAMS: {"enabled": True, "sleep_time": "22:30"},
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_commands(hass, mock_hub, service, command, extra):
|
||||
"""Test sending commands to the vacuum."""
|
||||
await setup_hub(hass, mock_hub)
|
||||
|
||||
vacuum = hass.states.get(ENTITY_ID)
|
||||
assert vacuum is not None
|
||||
assert vacuum.state == STATE_DOCKED
|
||||
|
||||
data = {ATTR_ENTITY_ID: ENTITY_ID}
|
||||
if extra:
|
||||
data.update(extra)
|
||||
|
||||
await hass.services.async_call(
|
||||
PLATFORM_DOMAIN,
|
||||
service,
|
||||
data,
|
||||
blocking=True,
|
||||
)
|
||||
future = utcnow() + timedelta(seconds=REFRESH_WAIT_TIME)
|
||||
async_fire_time_changed(hass, future)
|
||||
getattr(mock_hub.account.robots[0], command).assert_called_once()
|
||||
Reference in New Issue
Block a user