Add switchbot cloud integration (#99607)
* Switches via API * Using external library * UT and checlist * Updating file .coveragerc * Update homeassistant/components/switchbot_via_api/switch.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/switchbot_via_api/switch.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/switchbot_via_api/switch.py Co-authored-by: J. Nick Koston <nick@koston.org> * Review fixes * Apply suggestions from code review Co-authored-by: J. Nick Koston <nick@koston.org> * This base class shouldn't know about Remote * Fixing suggestion * Sometimes, the state from the API is not updated immediately * Review changes * Some review changes * Review changes * Review change: Adding type on commands * Parameterizing some tests * Review changes * Updating .coveragerc * Fixing error handling in coordinator * Review changes * Review changes * Adding switchbot brand * Apply suggestions from code review Co-authored-by: J. Nick Koston <nick@koston.org> * Review changes * Adding strict typing * Removing log in constructor --------- Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
568974fcc4
commit
f99dedfb42
22 changed files with 623 additions and 4 deletions
100
tests/components/switchbot_cloud/test_init.py
Normal file
100
tests/components/switchbot_cloud/test_init.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
"""Tests for the SwitchBot Cloud integration init."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from switchbot_api import CannotConnect, Device, InvalidAuth, PowerState
|
||||
|
||||
from homeassistant.components.switchbot_cloud import SwitchBotAPI
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import configure_integration
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_list_devices():
|
||||
"""Mock list_devices."""
|
||||
with patch.object(SwitchBotAPI, "list_devices") as mock_list_devices:
|
||||
yield mock_list_devices
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_get_status():
|
||||
"""Mock get_status."""
|
||||
with patch.object(SwitchBotAPI, "get_status") as mock_get_status:
|
||||
yield mock_get_status
|
||||
|
||||
|
||||
async def test_setup_entry_success(
|
||||
hass: HomeAssistant, mock_list_devices, mock_get_status
|
||||
) -> None:
|
||||
"""Test successful setup of entry."""
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
deviceId="test-id",
|
||||
deviceName="test-name",
|
||||
deviceType="Plug",
|
||||
hubDeviceId="test-hub-id",
|
||||
)
|
||||
]
|
||||
mock_get_status.return_value = {"power": PowerState.ON.value}
|
||||
entry = configure_integration(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
mock_list_devices.assert_called_once()
|
||||
mock_get_status.assert_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "state"),
|
||||
[
|
||||
(InvalidAuth, ConfigEntryState.SETUP_ERROR),
|
||||
(CannotConnect, ConfigEntryState.SETUP_RETRY),
|
||||
],
|
||||
)
|
||||
async def test_setup_entry_fails_when_listing_devices(
|
||||
hass: HomeAssistant,
|
||||
error: Exception,
|
||||
state: ConfigEntryState,
|
||||
mock_list_devices,
|
||||
mock_get_status,
|
||||
) -> None:
|
||||
"""Test error handling when list_devices in setup of entry."""
|
||||
mock_list_devices.side_effect = error
|
||||
entry = configure_integration(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert entry.state == state
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
mock_list_devices.assert_called_once()
|
||||
mock_get_status.assert_not_called()
|
||||
|
||||
|
||||
async def test_setup_entry_fails_when_refreshing(
|
||||
hass: HomeAssistant, mock_list_devices, mock_get_status
|
||||
) -> None:
|
||||
"""Test error handling in get_status in setup of entry."""
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
deviceId="test-id",
|
||||
deviceName="test-name",
|
||||
deviceType="Plug",
|
||||
hubDeviceId="test-hub-id",
|
||||
)
|
||||
]
|
||||
mock_get_status.side_effect = CannotConnect
|
||||
entry = configure_integration(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
mock_list_devices.assert_called_once()
|
||||
mock_get_status.assert_called()
|
Loading…
Add table
Add a link
Reference in a new issue