2019-08-06 12:39:07 +00:00
|
|
|
"""The tests for the Ring switch platform."""
|
2024-03-22 23:27:57 +00:00
|
|
|
|
2024-03-11 19:23:49 +00:00
|
|
|
import pytest
|
|
|
|
import ring_doorbell
|
2023-02-17 17:45:48 +00:00
|
|
|
|
2024-03-11 19:23:49 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH
|
2024-09-11 14:27:07 +00:00
|
|
|
from homeassistant.const import Platform
|
2023-02-17 17:45:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-03-11 19:23:49 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-03-09 13:28:32 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2019-12-09 10:58:40 +00:00
|
|
|
|
2019-08-06 12:39:07 +00:00
|
|
|
from .common import setup_platform
|
|
|
|
|
|
|
|
|
2023-02-17 17:45:48 +00:00
|
|
|
async def test_entity_registry(
|
2024-05-28 12:23:31 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
2024-06-06 18:13:19 +00:00
|
|
|
mock_ring_client,
|
2023-02-17 17:45:48 +00:00
|
|
|
) -> None:
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Tests that the devices are registered in the entity registry."""
|
2021-12-22 05:01:01 +00:00
|
|
|
await setup_platform(hass, Platform.SWITCH)
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
entry = entity_registry.async_get("switch.front_siren")
|
2020-01-14 20:54:45 +00:00
|
|
|
assert entry.unique_id == "765432-siren"
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
entry = entity_registry.async_get("switch.internal_siren")
|
2020-01-14 20:54:45 +00:00
|
|
|
assert entry.unique_id == "345678-siren"
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
|
2023-02-17 17:45:48 +00:00
|
|
|
async def test_siren_off_reports_correctly(
|
2024-06-06 18:13:19 +00:00
|
|
|
hass: HomeAssistant, mock_ring_client
|
2023-02-17 17:45:48 +00:00
|
|
|
) -> None:
|
2019-08-06 12:39:07 +00:00
|
|
|
"""Tests that the initial state of a device that should be off is correct."""
|
2021-12-22 05:01:01 +00:00
|
|
|
await setup_platform(hass, Platform.SWITCH)
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
state = hass.states.get("switch.front_siren")
|
|
|
|
assert state.state == "off"
|
2023-07-09 17:55:10 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "Front Siren"
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
|
2023-02-17 17:45:48 +00:00
|
|
|
async def test_siren_on_reports_correctly(
|
2024-06-06 18:13:19 +00:00
|
|
|
hass: HomeAssistant, mock_ring_client
|
2023-02-17 17:45:48 +00:00
|
|
|
) -> None:
|
2019-08-06 12:39:07 +00:00
|
|
|
"""Tests that the initial state of a device that should be on is correct."""
|
2021-12-22 05:01:01 +00:00
|
|
|
await setup_platform(hass, Platform.SWITCH)
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
state = hass.states.get("switch.internal_siren")
|
|
|
|
assert state.state == "on"
|
2023-07-09 17:55:10 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "Internal Siren"
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
async def test_siren_can_be_turned_on(hass: HomeAssistant, mock_ring_client) -> None:
|
2019-08-06 12:39:07 +00:00
|
|
|
"""Tests the siren turns on correctly."""
|
2021-12-22 05:01:01 +00:00
|
|
|
await setup_platform(hass, Platform.SWITCH)
|
2019-08-06 12:39:07 +00:00
|
|
|
|
|
|
|
state = hass.states.get("switch.front_siren")
|
|
|
|
assert state.state == "off"
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"switch", "turn_on", {"entity_id": "switch.front_siren"}, blocking=True
|
|
|
|
)
|
|
|
|
|
2019-08-06 21:55:54 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-08-06 12:39:07 +00:00
|
|
|
state = hass.states.get("switch.front_siren")
|
|
|
|
assert state.state == "on"
|
|
|
|
|
|
|
|
|
2024-03-11 19:23:49 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("exception_type", "reauth_expected"),
|
|
|
|
[
|
|
|
|
(ring_doorbell.AuthenticationError, True),
|
|
|
|
(ring_doorbell.RingTimeout, False),
|
|
|
|
(ring_doorbell.RingError, False),
|
|
|
|
],
|
|
|
|
ids=["Authentication", "Timeout", "Other"],
|
|
|
|
)
|
|
|
|
async def test_switch_errors_when_turned_on(
|
|
|
|
hass: HomeAssistant,
|
2024-06-06 18:13:19 +00:00
|
|
|
mock_ring_client,
|
|
|
|
mock_ring_devices,
|
2024-03-11 19:23:49 +00:00
|
|
|
exception_type,
|
|
|
|
reauth_expected,
|
|
|
|
) -> None:
|
|
|
|
"""Tests the switch turns on correctly."""
|
|
|
|
await setup_platform(hass, Platform.SWITCH)
|
|
|
|
config_entry = hass.config_entries.async_entries("ring")[0]
|
|
|
|
|
|
|
|
assert not any(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH}))
|
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
front_siren_mock = mock_ring_devices.get_device(765432)
|
2024-08-24 06:23:31 +00:00
|
|
|
front_siren_mock.async_set_siren.side_effect = exception_type
|
2024-06-06 18:13:19 +00:00
|
|
|
|
|
|
|
with pytest.raises(HomeAssistantError):
|
|
|
|
await hass.services.async_call(
|
|
|
|
"switch", "turn_on", {"entity_id": "switch.front_siren"}, blocking=True
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-08-24 06:23:31 +00:00
|
|
|
front_siren_mock.async_set_siren.assert_called_once()
|
2024-03-11 19:23:49 +00:00
|
|
|
assert (
|
|
|
|
any(
|
|
|
|
flow
|
|
|
|
for flow in config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})
|
|
|
|
if flow["handler"] == "ring"
|
|
|
|
)
|
|
|
|
== reauth_expected
|
|
|
|
)
|