1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00
Files
core/tests/components/switchbot_cloud/test_switch.py
T
2026-05-08 00:01:49 +02:00

271 lines
8.1 KiB
Python

"""Test for the switchbot_cloud relay switch & bot."""
from unittest.mock import patch
import pytest
from switchbot_api import Device
from switchbot_api.exceptions import (
SwitchBotConnectionError,
SwitchBotDeviceOfflineError,
SwitchBotError,
)
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.switchbot_cloud import SwitchBotAPI
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from . import configure_integration
async def test_relay_switch(
hass: HomeAssistant, mock_list_devices, mock_get_status
) -> None:
"""Test turn on and turn off."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="relay-switch-id-1",
deviceName="relay-switch-1",
deviceType="Relay Switch 1",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = {"switchStatus": 0}
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
entity_id = "switch.relay_switch_1"
assert hass.states.get(entity_id).state == STATE_OFF
with patch.object(SwitchBotAPI, "send_command"):
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == STATE_ON
with patch.object(SwitchBotAPI, "send_command"):
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == STATE_OFF
async def test_switchmode_bot(
hass: HomeAssistant,
mock_list_devices,
mock_get_status,
mock_setup_webhook,
mock_get_webook_configuration,
) -> None:
"""Test turn on and turn off."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="bot-id-1",
deviceName="bot-1",
deviceType="Bot",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = {"deviceMode": "switchMode", "power": "off"}
mock_setup_webhook.return_value = {
"statusCode": 100,
"body": {},
"message": "success",
}
mock_get_webook_configuration.return_value = {
"statusCode": 100,
"body": {},
"message": "success",
}
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
entity_id = "switch.bot_1"
assert hass.states.get(entity_id).state == STATE_OFF
with patch.object(SwitchBotAPI, "send_command"):
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == STATE_ON
with patch.object(SwitchBotAPI, "send_command"):
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == STATE_OFF
async def test_pressmode_bot_no_switch_entity(
hass: HomeAssistant,
mock_list_devices,
mock_get_status,
mock_setup_webhook,
mock_get_webook_configuration,
) -> None:
"""Test a pressMode bot isn't added as a switch."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="bot-id-1",
deviceName="bot-1",
deviceType="Bot",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = {"deviceMode": "pressMode"}
mock_setup_webhook.return_value = {
"statusCode": 100,
"body": {},
"message": "success",
}
mock_get_webook_configuration.return_value = {
"statusCode": 100,
"body": {},
"message": "success",
}
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
assert not hass.states.async_entity_ids(SWITCH_DOMAIN)
async def test_switch_relay_2pm_turn_on(
hass: HomeAssistant, mock_list_devices, mock_get_status
) -> None:
"""Test switch relay 2pm turn on."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="relay-switch-id-1",
deviceName="relay-switch-1",
deviceType="Relay Switch 2PM",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = {"switchStatus": 0}
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
entity_id = "switch.relay_switch_1_channel_1"
assert hass.states.get(entity_id).state == STATE_OFF
with patch.object(SwitchBotAPI, "send_command") as mock_send_command:
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
mock_send_command.assert_called_once()
async def test_switch_relay_2pm_turn_off(
hass: HomeAssistant, mock_list_devices, mock_get_status
) -> None:
"""Test switch relay 2pm turn off."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="relay-switch-id-1",
deviceName="relay-switch-1",
deviceType="Relay Switch 2PM",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = {"switchStatus": 0}
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
entity_id = "switch.relay_switch_1_channel_1"
assert hass.states.get(entity_id).state == STATE_OFF
with patch.object(SwitchBotAPI, "send_command") as mock_send_command:
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
mock_send_command.assert_called_once()
async def test_switch_relay_2pm_coordination_is_none(
hass: HomeAssistant, mock_list_devices, mock_get_status
) -> None:
"""Test switch relay 2pm coordination is none."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="relay-switch-id-1",
deviceName="relay-switch-1",
deviceType="Relay Switch 2PM",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = None
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
entity_id = "switch.relay_switch_1_channel_1"
assert hass.states.get(entity_id).state == STATE_UNKNOWN
@pytest.mark.parametrize(
("api_error", "translation_key"),
[
(SwitchBotDeviceOfflineError("offline"), "device_offline"),
(SwitchBotConnectionError("conn"), "connection_error"),
(SwitchBotError("boom"), "command_failed"),
],
)
async def test_switch_api_error_is_translated(
hass: HomeAssistant,
mock_list_devices,
mock_get_status,
api_error: Exception,
translation_key: str,
) -> None:
"""Library exceptions are translated to HomeAssistantError so script flags work."""
mock_list_devices.return_value = [
Device(
version="V1.0",
deviceId="relay-switch-id-1",
deviceName="relay-switch-1",
deviceType="Relay Switch 1",
hubDeviceId="test-hub-id",
),
]
mock_get_status.return_value = {"switchStatus": 0}
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.LOADED
with (
patch.object(SwitchBotAPI, "send_command", side_effect=api_error),
pytest.raises(HomeAssistantError) as exc_info,
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.relay_switch_1"},
blocking=True,
)
assert exc_info.value.translation_key == translation_key
assert exc_info.value.__cause__ is api_error