Add basic system health data to the recorder (#71086)

pull/71121/head
J. Nick Koston 2022-04-30 11:10:20 -05:00 committed by GitHub
parent f14bc1cece
commit 66a21e0bc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 0 deletions

View File

@ -53,6 +53,13 @@ class RunHistory:
"""Return the time the recorder started recording states."""
return self._recording_start
@property
def first(self) -> RecorderRuns:
"""Get the first run."""
if runs_by_timestamp := self._run_history.runs_by_timestamp:
return next(iter(runs_by_timestamp.values()))
return self.current
@property
def current(self) -> RecorderRuns:
"""Get the current run."""

View File

@ -0,0 +1,8 @@
{
"system_health": {
"info": {
"oldest_recorder_run": "Oldest Run Start Time",
"current_recorder_run": "Current Run Start Time"
}
}
}

View File

@ -0,0 +1,24 @@
"""Provide info to system health."""
from homeassistant.components import system_health
from homeassistant.core import HomeAssistant, callback
from . import get_instance
@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: HomeAssistant):
"""Get info for the info page."""
instance = get_instance(hass)
run_history = instance.run_history
return {
"oldest_recorder_run": run_history.first.start,
"current_recorder_run": run_history.current.start,
}

View File

@ -0,0 +1,8 @@
{
"system_health": {
"info": {
"current_recorder_run": "Current Run Start Time",
"oldest_recorder_run": "Oldest Run Start Time"
}
}
}

View File

@ -0,0 +1,38 @@
"""Test recorder system health."""
from unittest.mock import patch
from homeassistant.components.recorder import get_instance
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .common import async_wait_recording_done
from tests.common import SetupRecorderInstanceT, get_system_health_info
async def test_recorder_system_health(hass, recorder_mock):
"""Test recorder system health."""
assert await async_setup_component(hass, "system_health", {})
await async_wait_recording_done(hass)
info = await get_system_health_info(hass, "recorder")
instance = get_instance(hass)
assert info == {
"current_recorder_run": instance.run_history.current.start,
"oldest_recorder_run": instance.run_history.first.start,
}
async def test_recorder_system_health_crashed_recorder_runs_table(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT
):
"""Test recorder system health with crashed recorder runs table."""
with patch("homeassistant.components.recorder.run_history.RunHistory.load_from_db"):
assert await async_setup_component(hass, "system_health", {})
instance = await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
info = await get_system_health_info(hass, "recorder")
assert info == {
"current_recorder_run": instance.run_history.current.start,
"oldest_recorder_run": instance.run_history.current.start,
}