Add recorder engine name and version to analytics (#87784)

* Add recorder to analytics

* Add test

* Add recorder to after_dependencies

* Add version

* dialect can not be None
This commit is contained in:
Joakim Sørensen 2023-02-13 09:18:12 +01:00 committed by GitHub
parent 9008556457
commit e842f90767
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 1 deletions

View file

@ -17,6 +17,10 @@ from homeassistant.components.energy import (
DOMAIN as ENERGY_DOMAIN, DOMAIN as ENERGY_DOMAIN,
is_configured as energy_is_configured, is_configured as energy_is_configured,
) )
from homeassistant.components.recorder import (
DOMAIN as RECORDER_DOMAIN,
get_instance as get_recorder_instance,
)
from homeassistant.const import ATTR_DOMAIN, __version__ as HA_VERSION from homeassistant.const import ATTR_DOMAIN, __version__ as HA_VERSION
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -40,11 +44,13 @@ from .const import (
ATTR_CUSTOM_INTEGRATIONS, ATTR_CUSTOM_INTEGRATIONS,
ATTR_DIAGNOSTICS, ATTR_DIAGNOSTICS,
ATTR_ENERGY, ATTR_ENERGY,
ATTR_ENGINE,
ATTR_HEALTHY, ATTR_HEALTHY,
ATTR_INTEGRATION_COUNT, ATTR_INTEGRATION_COUNT,
ATTR_INTEGRATIONS, ATTR_INTEGRATIONS,
ATTR_OPERATING_SYSTEM, ATTR_OPERATING_SYSTEM,
ATTR_PROTECTED, ATTR_PROTECTED,
ATTR_RECORDER,
ATTR_SLUG, ATTR_SLUG,
ATTR_STATE_COUNT, ATTR_STATE_COUNT,
ATTR_STATISTICS, ATTR_STATISTICS,
@ -252,6 +258,15 @@ class Analytics:
ATTR_CONFIGURED: await energy_is_configured(self.hass) ATTR_CONFIGURED: await energy_is_configured(self.hass)
} }
if RECORDER_DOMAIN in integrations:
instance = get_recorder_instance(self.hass)
engine = instance.database_engine
if engine and engine.version is not None:
payload[ATTR_RECORDER] = {
ATTR_ENGINE: engine.dialect.value,
ATTR_VERSION: engine.version,
}
if self.preferences.get(ATTR_STATISTICS, False): if self.preferences.get(ATTR_STATISTICS, False):
payload[ATTR_STATE_COUNT] = len(self.hass.states.async_all()) payload[ATTR_STATE_COUNT] = len(self.hass.states.async_all())
payload[ATTR_AUTOMATION_COUNT] = len( payload[ATTR_AUTOMATION_COUNT] = len(

View file

@ -26,6 +26,7 @@ ATTR_CONFIGURED = "configured"
ATTR_CUSTOM_INTEGRATIONS = "custom_integrations" ATTR_CUSTOM_INTEGRATIONS = "custom_integrations"
ATTR_DIAGNOSTICS = "diagnostics" ATTR_DIAGNOSTICS = "diagnostics"
ATTR_ENERGY = "energy" ATTR_ENERGY = "energy"
ATTR_ENGINE = "engine"
ATTR_HEALTHY = "healthy" ATTR_HEALTHY = "healthy"
ATTR_INSTALLATION_TYPE = "installation_type" ATTR_INSTALLATION_TYPE = "installation_type"
ATTR_INTEGRATION_COUNT = "integration_count" ATTR_INTEGRATION_COUNT = "integration_count"
@ -34,6 +35,7 @@ ATTR_ONBOARDED = "onboarded"
ATTR_OPERATING_SYSTEM = "operating_system" ATTR_OPERATING_SYSTEM = "operating_system"
ATTR_PREFERENCES = "preferences" ATTR_PREFERENCES = "preferences"
ATTR_PROTECTED = "protected" ATTR_PROTECTED = "protected"
ATTR_RECORDER = "recorder"
ATTR_SLUG = "slug" ATTR_SLUG = "slug"
ATTR_STATE_COUNT = "state_count" ATTR_STATE_COUNT = "state_count"
ATTR_STATISTICS = "statistics" ATTR_STATISTICS = "statistics"

View file

@ -1,7 +1,7 @@
{ {
"domain": "analytics", "domain": "analytics",
"name": "Analytics", "name": "Analytics",
"after_dependencies": ["energy"], "after_dependencies": ["energy", "recorder"],
"codeowners": ["@home-assistant/core", "@ludeeus"], "codeowners": ["@home-assistant/core", "@ludeeus"],
"dependencies": ["api", "websocket_api"], "dependencies": ["api", "websocket_api"],
"documentation": "https://www.home-assistant.io/integrations/analytics", "documentation": "https://www.home-assistant.io/integrations/analytics",

View file

@ -576,3 +576,22 @@ async def test_send_usage_with_certificate(
await analytics.send_analytics() await analytics.send_analytics()
assert "'certificate': True" in caplog.text assert "'certificate': True" in caplog.text
async def test_send_with_recorder(
recorder_mock: AsyncMock,
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test recorder information."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
analytics = Analytics(hass)
hass.http = Mock(ssl_certificate="/some/path/to/cert.pem")
await analytics.save_preferences({ATTR_BASE: True, ATTR_USAGE: True})
with patch("homeassistant.components.analytics.analytics.HA_VERSION", MOCK_VERSION):
await analytics.send_analytics()
postdata = aioclient_mock.mock_calls[-1][2]
assert postdata["recorder"]["engine"] == "sqlite"