mirror of
https://github.com/home-assistant/core.git
synced 2026-05-25 17:56:30 +01:00
305 lines
8.7 KiB
Python
305 lines
8.7 KiB
Python
"""Test the Tractive switch platform."""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from aiotractive.exceptions import TractiveError
|
|
import pytest
|
|
from syrupy.assertion import SnapshotAssertion
|
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
|
from homeassistant.components.tractive.const import DOMAIN
|
|
from homeassistant.const import (
|
|
ATTR_ENTITY_ID,
|
|
SERVICE_TURN_OFF,
|
|
SERVICE_TURN_ON,
|
|
STATE_OFF,
|
|
STATE_ON,
|
|
STATE_UNAVAILABLE,
|
|
Platform,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
|
|
|
from . import init_integration
|
|
|
|
from tests.common import MockConfigEntry, snapshot_platform
|
|
|
|
|
|
async def test_switch(
|
|
hass: HomeAssistant,
|
|
entity_registry: er.EntityRegistry,
|
|
snapshot: SnapshotAssertion,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test states of the switch."""
|
|
with patch("homeassistant.components.tractive.PLATFORMS", [Platform.SWITCH]):
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
|
|
|
|
|
async def test_switch_on(
|
|
hass: HomeAssistant,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test the switch can be turned on."""
|
|
entity_id = "switch.tracker_device_id_123_led"
|
|
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_OFF
|
|
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
blocking=True,
|
|
)
|
|
mock_tractive_client.send_switch_event(
|
|
mock_config_entry,
|
|
{"tracker_id": "device_id_123", "led_control": {"active": True}},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_tractive_client.tracker.return_value.set_led_active.call_count == 1
|
|
assert (
|
|
mock_tractive_client.tracker.return_value.set_led_active.call_args[0][0] is True
|
|
)
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
async def test_switch_off(
|
|
hass: HomeAssistant,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test the switch can be turned off."""
|
|
entity_id = "switch.tracker_device_id_123_buzzer"
|
|
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
blocking=True,
|
|
)
|
|
mock_tractive_client.send_switch_event(
|
|
mock_config_entry,
|
|
{"tracker_id": "device_id_123", "buzzer_control": {"active": False}},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_tractive_client.tracker.return_value.set_buzzer_active.call_count == 1
|
|
assert (
|
|
mock_tractive_client.tracker.return_value.set_buzzer_active.call_args[0][0]
|
|
is False
|
|
)
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
async def test_live_tracking_switch(
|
|
hass: HomeAssistant,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test the live_tracking switch."""
|
|
entity_id = "switch.tracker_device_id_123_live_tracking"
|
|
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
blocking=True,
|
|
)
|
|
mock_tractive_client.send_switch_event(
|
|
mock_config_entry,
|
|
{"tracker_id": "device_id_123", "live_tracking": {"active": False}},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert (
|
|
mock_tractive_client.tracker.return_value.set_live_tracking_active.call_count
|
|
== 1
|
|
)
|
|
assert (
|
|
mock_tractive_client.tracker.return_value.set_live_tracking_active.call_args[0][
|
|
0
|
|
]
|
|
is False
|
|
)
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
async def test_switch_on_with_exception(
|
|
hass: HomeAssistant,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test the switch turn on with exception."""
|
|
entity_id = "switch.tracker_device_id_123_led"
|
|
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_OFF
|
|
|
|
mock_tractive_client.tracker.return_value.set_led_active.side_effect = TractiveError
|
|
|
|
with pytest.raises(
|
|
HomeAssistantError,
|
|
match=f"An error occurred while trying to turn on {entity_id}",
|
|
):
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
blocking=True,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
async def test_switch_off_with_exception(
|
|
hass: HomeAssistant,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test the switch turn off with exception."""
|
|
entity_id = "switch.tracker_device_id_123_buzzer"
|
|
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
mock_tractive_client.tracker.return_value.set_buzzer_active.side_effect = (
|
|
TractiveError
|
|
)
|
|
|
|
with pytest.raises(
|
|
HomeAssistantError,
|
|
match=f"An error occurred while trying to turn off {entity_id}",
|
|
):
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
blocking=True,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
async def test_switch_unavailable(
|
|
hass: HomeAssistant,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test the switch is navailable when the tracker is in the energy saving zone."""
|
|
entity_id = "switch.tracker_device_id_123_buzzer"
|
|
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
event = {
|
|
"tracker_id": "device_id_123",
|
|
"hardware": {"power_saving_zone_id": "zone_id_123"},
|
|
}
|
|
mock_tractive_client.send_switch_event(mock_config_entry, event)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
event["hardware"]["power_saving_zone_id"] = None
|
|
|
|
mock_tractive_client.send_switch_event(mock_config_entry, event)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
async def test_switch_device_assignment(
|
|
hass: HomeAssistant,
|
|
entity_registry: er.EntityRegistry,
|
|
device_registry: dr.DeviceRegistry,
|
|
mock_tractive_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test that switch entities are assigned to the tracker device."""
|
|
with patch("homeassistant.components.tractive.PLATFORMS", [Platform.SWITCH]):
|
|
await init_integration(hass, mock_config_entry)
|
|
|
|
tracker_device = device_registry.async_get_device(
|
|
identifiers={(DOMAIN, "device_id_123")}
|
|
)
|
|
assert tracker_device is not None
|
|
|
|
for entity_id in (
|
|
"switch.tracker_device_id_123_buzzer",
|
|
"switch.tracker_device_id_123_led",
|
|
"switch.tracker_device_id_123_live_tracking",
|
|
):
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry is not None
|
|
assert entry.device_id == tracker_device.id
|