1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Adjust litterrobot tests and code to match guidelines (#47060)

* 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
This commit is contained in:
Nathan Spencer
2021-03-06 09:28:33 -07:00
committed by GitHub
parent 14f85d8731
commit e9052233a6
14 changed files with 277 additions and 142 deletions

View File

@@ -1,45 +1,59 @@
"""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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .common import CONFIG, ROBOT_DATA
from tests.common import MockConfigEntry
def create_mock_robot(hass):
def create_mock_robot(unit_status_code: Optional[str] = None):
"""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()
robot.set_night_light = AsyncMock()
robot.set_panel_lockout = AsyncMock()
return robot
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
@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
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
async def setup_hub(hass, mock_hub, platform_domain):
@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,
@@ -47,9 +61,11 @@ async def setup_hub(hass, mock_hub, platform_domain):
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.litterrobot.LitterRobotHub",
return_value=mock_hub,
with patch("pylitterbot.Account", return_value=mock_account), patch(
"homeassistant.components.litterrobot.PLATFORMS",
[platform_domain] if platform_domain else [],
):
await hass.config_entries.async_forward_entry_setup(entry, platform_domain)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry