2022-07-11 14:00:13 +00:00
|
|
|
"""Test switch of NextDNS integration."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2022-07-11 14:00:13 +00:00
|
|
|
from datetime import timedelta
|
2022-08-22 05:08:57 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2022-07-11 14:00:13 +00:00
|
|
|
|
2022-08-22 05:08:57 +00:00
|
|
|
from aiohttp import ClientError
|
|
|
|
from aiohttp.client_exceptions import ClientConnectorError
|
2022-07-11 14:00:13 +00:00
|
|
|
from nextdns import ApiError
|
2022-08-22 05:08:57 +00:00
|
|
|
import pytest
|
2024-04-20 10:31:20 +00:00
|
|
|
from syrupy import SnapshotAssertion
|
2024-06-27 21:12:20 +00:00
|
|
|
from tenacity import RetryError
|
2022-07-11 14:00:13 +00:00
|
|
|
|
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
2024-04-20 10:31:20 +00:00
|
|
|
Platform,
|
2022-07-11 14:00:13 +00:00
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-22 05:08:57 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2022-07-11 14:00:13 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
2024-04-01 16:53:49 +00:00
|
|
|
from . import init_integration, mock_nextdns
|
2022-07-11 14:00:13 +00:00
|
|
|
|
2024-04-20 16:43:40 +00:00
|
|
|
from tests.common import async_fire_time_changed, snapshot_platform
|
2022-07-11 14:00:13 +00:00
|
|
|
|
|
|
|
|
2024-06-04 14:18:42 +00:00
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
2023-04-22 16:09:54 +00:00
|
|
|
async def test_switch(
|
2024-04-20 10:31:20 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
snapshot: SnapshotAssertion,
|
2023-04-22 16:09:54 +00:00
|
|
|
) -> None:
|
2022-07-11 14:00:13 +00:00
|
|
|
"""Test states of the switches."""
|
2024-04-20 10:31:20 +00:00
|
|
|
with patch("homeassistant.components.nextdns.PLATFORMS", [Platform.SWITCH]):
|
|
|
|
entry = await init_integration(hass)
|
2022-07-11 14:00:13 +00:00
|
|
|
|
2024-04-20 16:43:40 +00:00
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
|
2023-04-22 16:09:54 +00:00
|
|
|
|
2022-07-11 14:00:13 +00:00
|
|
|
|
|
|
|
async def test_switch_on(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the switch can be turned on."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_block_page")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nextdns.NextDns.set_setting", return_value=True
|
|
|
|
) as mock_switch_on:
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call(
|
2022-07-11 14:00:13 +00:00
|
|
|
SWITCH_DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
{ATTR_ENTITY_ID: "switch.fake_profile_block_page"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_block_page")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
mock_switch_on.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_switch_off(hass: HomeAssistant) -> None:
|
|
|
|
"""Test the switch can be turned on."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_web3")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nextdns.NextDns.set_setting", return_value=True
|
|
|
|
) as mock_switch_on:
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call(
|
2022-07-11 14:00:13 +00:00
|
|
|
SWITCH_DOMAIN,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
{ATTR_ENTITY_ID: "switch.fake_profile_web3"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_web3")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
|
|
mock_switch_on.assert_called_once()
|
|
|
|
|
|
|
|
|
2024-06-27 21:12:20 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"exc",
|
|
|
|
[
|
|
|
|
ApiError("API Error"),
|
|
|
|
RetryError("Retry Error"),
|
|
|
|
TimeoutError,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_availability(hass: HomeAssistant, exc: Exception) -> None:
|
2022-07-11 14:00:13 +00:00
|
|
|
"""Ensure that we mark the entities unavailable correctly when service causes an error."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_web3")
|
|
|
|
assert state
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
future = utcnow() + timedelta(minutes=10)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nextdns.NextDns.get_settings",
|
2024-06-27 21:12:20 +00:00
|
|
|
side_effect=exc,
|
2022-07-11 14:00:13 +00:00
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, future)
|
2024-04-01 16:53:49 +00:00
|
|
|
await hass.async_block_till_done(wait_background_tasks=True)
|
2022-07-11 14:00:13 +00:00
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_web3")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
future = utcnow() + timedelta(minutes=20)
|
2024-04-01 16:53:49 +00:00
|
|
|
with mock_nextdns():
|
2022-07-11 14:00:13 +00:00
|
|
|
async_fire_time_changed(hass, future)
|
2024-04-01 16:53:49 +00:00
|
|
|
await hass.async_block_till_done(wait_background_tasks=True)
|
2022-07-11 14:00:13 +00:00
|
|
|
|
|
|
|
state = hass.states.get("switch.fake_profile_web3")
|
|
|
|
assert state
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
|
|
|
assert state.state == STATE_ON
|
2022-08-22 05:08:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"exc",
|
|
|
|
[
|
|
|
|
ApiError(Mock()),
|
2024-02-05 11:08:18 +00:00
|
|
|
TimeoutError,
|
2022-08-22 05:08:57 +00:00
|
|
|
ClientConnectorError(Mock(), Mock()),
|
|
|
|
ClientError,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_switch_failure(hass: HomeAssistant, exc: Exception) -> None:
|
|
|
|
"""Tests that the turn on/off service throws HomeAssistantError."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("homeassistant.components.nextdns.NextDns.set_setting", side_effect=exc),
|
|
|
|
pytest.raises(HomeAssistantError),
|
|
|
|
):
|
2023-01-27 10:52:49 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
SWITCH_DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
{ATTR_ENTITY_ID: "switch.fake_profile_block_page"},
|
|
|
|
blocking=True,
|
|
|
|
)
|