mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Set quality scale to platinum in the NextDNS integration (#77099)
* Set quality scale to platinum * Catch exceptions on when service calls * Add tests
This commit is contained in:
parent
eef7bdb44b
commit
cba2893862
@ -6,5 +6,6 @@
|
|||||||
"requirements": ["nextdns==1.1.1"],
|
"requirements": ["nextdns==1.1.1"],
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["nextdns"]
|
"loggers": ["nextdns"],
|
||||||
|
"quality_scale": "platinum"
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
"""Support for the NextDNS service."""
|
"""Support for the NextDNS service."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Generic
|
from typing import Any, Generic
|
||||||
|
|
||||||
from nextdns import Settings
|
from aiohttp import ClientError
|
||||||
|
from aiohttp.client_exceptions import ClientConnectorError
|
||||||
|
from nextdns import ApiError, Settings
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity import EntityCategory
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
@ -564,20 +568,28 @@ class NextDnsSwitch(CoordinatorEntity[NextDnsSettingsUpdateCoordinator], SwitchE
|
|||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn on switch."""
|
"""Turn on switch."""
|
||||||
result = await self.coordinator.nextdns.set_setting(
|
await self.async_set_setting(True)
|
||||||
self.coordinator.profile_id, self.entity_description.key, True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result:
|
|
||||||
self._attr_is_on = True
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn off switch."""
|
"""Turn off switch."""
|
||||||
result = await self.coordinator.nextdns.set_setting(
|
await self.async_set_setting(False)
|
||||||
self.coordinator.profile_id, self.entity_description.key, False
|
|
||||||
)
|
async def async_set_setting(self, new_state: bool) -> None:
|
||||||
|
"""Set the new state."""
|
||||||
|
try:
|
||||||
|
result = await self.coordinator.nextdns.set_setting(
|
||||||
|
self.coordinator.profile_id, self.entity_description.key, new_state
|
||||||
|
)
|
||||||
|
except (
|
||||||
|
ApiError,
|
||||||
|
ClientConnectorError,
|
||||||
|
asyncio.TimeoutError,
|
||||||
|
ClientError,
|
||||||
|
) as err:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
f"NextDNS API returned an error calling set_setting for {self.entity_id}: {err}"
|
||||||
|
) from err
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
self._attr_is_on = False
|
self._attr_is_on = new_state
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
"""Test switch of NextDNS integration."""
|
"""Test switch of NextDNS integration."""
|
||||||
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
|
from aiohttp import ClientError
|
||||||
|
from aiohttp.client_exceptions import ClientConnectorError
|
||||||
from nextdns import ApiError
|
from nextdns import ApiError
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.nextdns.const import DOMAIN
|
from homeassistant.components.nextdns.const import DOMAIN
|
||||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
@ -15,6 +19,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
@ -977,3 +982,26 @@ async def test_availability(hass: HomeAssistant) -> None:
|
|||||||
assert state
|
assert state
|
||||||
assert state.state != STATE_UNAVAILABLE
|
assert state.state != STATE_UNAVAILABLE
|
||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"exc",
|
||||||
|
[
|
||||||
|
ApiError(Mock()),
|
||||||
|
asyncio.TimeoutError,
|
||||||
|
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)
|
||||||
|
|
||||||
|
with patch("homeassistant.components.nextdns.NextDns.set_setting", side_effect=exc):
|
||||||
|
with pytest.raises(HomeAssistantError):
|
||||||
|
await hass.services.async_call(
|
||||||
|
SWITCH_DOMAIN,
|
||||||
|
SERVICE_TURN_ON,
|
||||||
|
{ATTR_ENTITY_ID: "switch.fake_profile_block_page"},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user