diff --git a/homeassistant/components/nextdns/strings.json b/homeassistant/components/nextdns/strings.json index db3cf88cf39..59319881c02 100644 --- a/homeassistant/components/nextdns/strings.json +++ b/homeassistant/components/nextdns/strings.json @@ -20,5 +20,10 @@ "abort": { "already_configured": "This NextDNS profile is already configured." } + }, + "system_health": { + "info": { + "can_reach_server": "Reach server" + } } } diff --git a/homeassistant/components/nextdns/system_health.py b/homeassistant/components/nextdns/system_health.py new file mode 100644 index 00000000000..0fa31e75f1e --- /dev/null +++ b/homeassistant/components/nextdns/system_health.py @@ -0,0 +1,25 @@ +"""Provide info to system health.""" +from __future__ import annotations + +from typing import Any + +from nextdns.const import API_ENDPOINT + +from homeassistant.components import system_health +from homeassistant.core import HomeAssistant, callback + + +@callback +def async_register( # pylint:disable=unused-argument + hass: HomeAssistant, + register: system_health.SystemHealthRegistration, +) -> None: + """Register system health callbacks.""" + register.async_register_info(system_health_info) + + +async def system_health_info(hass: HomeAssistant) -> dict[str, Any]: + """Get info for the info page.""" + return { + "can_reach_server": system_health.async_check_can_reach_url(hass, API_ENDPOINT) + } diff --git a/tests/components/nextdns/test_system_health.py b/tests/components/nextdns/test_system_health.py new file mode 100644 index 00000000000..4fc3a1a7e72 --- /dev/null +++ b/tests/components/nextdns/test_system_health.py @@ -0,0 +1,40 @@ +"""Test NextDNS system health.""" +import asyncio + +from aiohttp import ClientError +from nextdns.const import API_ENDPOINT + +from homeassistant.components.nextdns.const import DOMAIN +from homeassistant.setup import async_setup_component + +from tests.common import get_system_health_info + + +async def test_nextdns_system_health(hass, aioclient_mock): + """Test NextDNS system health.""" + aioclient_mock.get(API_ENDPOINT, text="") + hass.config.components.add(DOMAIN) + assert await async_setup_component(hass, "system_health", {}) + + info = await get_system_health_info(hass, DOMAIN) + + for key, val in info.items(): + if asyncio.iscoroutine(val): + info[key] = await val + + assert info == {"can_reach_server": "ok"} + + +async def test_nextdns_system_health_fail(hass, aioclient_mock): + """Test NextDNS system health.""" + aioclient_mock.get(API_ENDPOINT, exc=ClientError) + hass.config.components.add(DOMAIN) + assert await async_setup_component(hass, "system_health", {}) + + info = await get_system_health_info(hass, DOMAIN) + + for key, val in info.items(): + if asyncio.iscoroutine(val): + info[key] = await val + + assert info == {"can_reach_server": {"type": "failed", "error": "unreachable"}}