diff --git a/homeassistant/components/airly/strings.json b/homeassistant/components/airly/strings.json index 3453fb7b38a..afda73ae887 100644 --- a/homeassistant/components/airly/strings.json +++ b/homeassistant/components/airly/strings.json @@ -19,5 +19,10 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_location%]" } + }, + "system_health": { + "info": { + "can_reach_server": "Reach Airly server" + } } } diff --git a/homeassistant/components/airly/system_health.py b/homeassistant/components/airly/system_health.py new file mode 100644 index 00000000000..6b683518ebd --- /dev/null +++ b/homeassistant/components/airly/system_health.py @@ -0,0 +1,22 @@ +"""Provide info to system health.""" +from airly import Airly + +from homeassistant.components import system_health +from homeassistant.core import HomeAssistant, callback + + +@callback +def async_register( + hass: HomeAssistant, register: system_health.SystemHealthRegistration +) -> None: + """Register system health callbacks.""" + register.async_register_info(system_health_info) + + +async def system_health_info(hass): + """Get info for the info page.""" + return { + "can_reach_server": system_health.async_check_can_reach_url( + hass, Airly.AIRLY_API_URL + ) + } diff --git a/tests/components/airly/test_system_health.py b/tests/components/airly/test_system_health.py new file mode 100644 index 00000000000..b1f8119e880 --- /dev/null +++ b/tests/components/airly/test_system_health.py @@ -0,0 +1,50 @@ +"""Test Airly system health.""" +import asyncio + +from aiohttp import ClientError + +from homeassistant.components.airly.const import DOMAIN +from homeassistant.setup import async_setup_component + +from tests.async_mock import Mock +from tests.common import get_system_health_info + + +async def test_airly_system_health(hass, aioclient_mock): + """Test Airly system health.""" + aioclient_mock.get("https://airapi.airly.eu/v2/", text="") + hass.config.components.add(DOMAIN) + assert await async_setup_component(hass, "system_health", {}) + + hass.data[DOMAIN] = {} + hass.data[DOMAIN]["0123xyz"] = Mock( + airly=Mock(AIRLY_API_URL="https://airapi.airly.eu/v2/") + ) + + 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_airly_system_health_fail(hass, aioclient_mock): + """Test Airly system health.""" + aioclient_mock.get("https://airapi.airly.eu/v2/", exc=ClientError) + hass.config.components.add(DOMAIN) + assert await async_setup_component(hass, "system_health", {}) + + hass.data[DOMAIN] = {} + hass.data[DOMAIN]["0123xyz"] = Mock( + airly=Mock(AIRLY_API_URL="https://airapi.airly.eu/v2/") + ) + + 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"}}