* Use SwitchEntity instead of ToggleEntity and adjust test patches as recommended * Move async_create_entry out of try block in config_flow * Patch pypi package instead of HA code * Bump pylitterbot to 2021.2.6, fix tests, and implement other code review suggestions * Bump pylitterbot to 2021.2.8, remove sleep mode start/end time from vacuum, adjust and add sensors for sleep mode start/end time * Move icon helper back to Litter-Robot component and isoformat times on time sensors
71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
"""Configure pytest for Litter-Robot tests."""
|
|
from typing import Optional
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pylitterbot
|
|
from pylitterbot import Robot
|
|
import pytest
|
|
|
|
from homeassistant.components import litterrobot
|
|
|
|
from .common import CONFIG, ROBOT_DATA
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
def create_mock_robot(unit_status_code: Optional[str] = None):
|
|
"""Create a mock Litter-Robot device."""
|
|
if not (
|
|
unit_status_code
|
|
and Robot.UnitStatus(unit_status_code) != Robot.UnitStatus.UNKNOWN
|
|
):
|
|
unit_status_code = ROBOT_DATA["unitStatus"]
|
|
|
|
with patch.dict(ROBOT_DATA, {"unitStatus": unit_status_code}):
|
|
robot = Robot(data=ROBOT_DATA)
|
|
robot.start_cleaning = AsyncMock()
|
|
robot.set_power_status = AsyncMock()
|
|
robot.reset_waste_drawer = AsyncMock()
|
|
robot.set_sleep_mode = AsyncMock()
|
|
robot.set_night_light = AsyncMock()
|
|
robot.set_panel_lockout = AsyncMock()
|
|
return robot
|
|
|
|
|
|
def create_mock_account(unit_status_code: Optional[str] = None):
|
|
"""Create a mock Litter-Robot account."""
|
|
account = MagicMock(spec=pylitterbot.Account)
|
|
account.connect = AsyncMock()
|
|
account.refresh_robots = AsyncMock()
|
|
account.robots = [create_mock_robot(unit_status_code)]
|
|
return account
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_account():
|
|
"""Mock a Litter-Robot account."""
|
|
return create_mock_account()
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_account_with_error():
|
|
"""Mock a Litter-Robot account with error."""
|
|
return create_mock_account("BR")
|
|
|
|
|
|
async def setup_integration(hass, mock_account, platform_domain=None):
|
|
"""Load a Litter-Robot platform with the provided hub."""
|
|
entry = MockConfigEntry(
|
|
domain=litterrobot.DOMAIN,
|
|
data=CONFIG[litterrobot.DOMAIN],
|
|
)
|
|
entry.add_to_hass(hass)
|
|
|
|
with patch("pylitterbot.Account", return_value=mock_account), patch(
|
|
"homeassistant.components.litterrobot.PLATFORMS",
|
|
[platform_domain] if platform_domain else [],
|
|
):
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
return entry
|