Add base energy analytics (#53855)

This commit is contained in:
Joakim Sørensen 2021-08-02 18:46:07 +02:00 committed by GitHub
parent bffa9f960d
commit 1c38e9168c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 4 deletions

View File

@ -8,6 +8,10 @@ import async_timeout
from homeassistant.components import hassio
from homeassistant.components.api import ATTR_INSTALLATION_TYPE
from homeassistant.components.automation.const import DOMAIN as AUTOMATION_DOMAIN
from homeassistant.components.energy import (
DOMAIN as ENERGY_DOMAIN,
is_configured as energy_is_configured,
)
from homeassistant.const import ATTR_DOMAIN, __version__ as HA_VERSION
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -26,8 +30,10 @@ from .const import (
ATTR_AUTOMATION_COUNT,
ATTR_BASE,
ATTR_BOARD,
ATTR_CONFIGURED,
ATTR_CUSTOM_INTEGRATIONS,
ATTR_DIAGNOSTICS,
ATTR_ENERGY,
ATTR_HEALTHY,
ATTR_INTEGRATION_COUNT,
ATTR_INTEGRATIONS,
@ -222,6 +228,11 @@ class Analytics:
if supervisor_info is not None:
payload[ATTR_ADDONS] = addons
if ENERGY_DOMAIN in integrations:
payload[ATTR_ENERGY] = {
ATTR_CONFIGURED: await energy_is_configured(self.hass)
}
if self.preferences.get(ATTR_STATISTICS, False):
payload[ATTR_STATE_COUNT] = len(self.hass.states.async_all())
payload[ATTR_AUTOMATION_COUNT] = len(

View File

@ -21,8 +21,10 @@ ATTR_AUTO_UPDATE = "auto_update"
ATTR_AUTOMATION_COUNT = "automation_count"
ATTR_BASE = "base"
ATTR_BOARD = "board"
ATTR_CONFIGURED = "configured"
ATTR_CUSTOM_INTEGRATIONS = "custom_integrations"
ATTR_DIAGNOSTICS = "diagnostics"
ATTR_ENERGY = "energy"
ATTR_HEALTHY = "healthy"
ATTR_INSTALLATION_TYPE = "installation_type"
ATTR_INTEGRATION_COUNT = "integration_count"

View File

@ -2,8 +2,17 @@
"domain": "analytics",
"name": "Analytics",
"documentation": "https://www.home-assistant.io/integrations/analytics",
"codeowners": ["@home-assistant/core", "@ludeeus"],
"dependencies": ["api", "websocket_api"],
"codeowners": [
"@home-assistant/core",
"@ludeeus"
],
"dependencies": [
"api",
"websocket_api"
],
"after_dependencies": [
"energy"
],
"quality_scale": "internal",
"iot_class": "cloud_push"
}
}

View File

@ -8,6 +8,13 @@ from homeassistant.helpers.typing import ConfigType
from . import websocket_api
from .const import DOMAIN
from .data import async_get_manager
async def is_configured(hass: HomeAssistant) -> bool:
"""Return a boolean to indicate if energy is configured."""
manager = await async_get_manager(hass)
return bool(manager.data != manager.default_preferences())
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

View File

@ -424,3 +424,72 @@ async def test_nightly_endpoint(hass, aioclient_mock):
payload = aioclient_mock.mock_calls[0]
assert str(payload[1]) == ANALYTICS_ENDPOINT_URL
async def test_send_with_no_energy(hass, aioclient_mock):
"""Test send base prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
analytics = Analytics(hass)
await analytics.save_preferences({ATTR_BASE: True, ATTR_USAGE: True})
with patch("uuid.UUID.hex", new_callable=PropertyMock) as hex, patch(
"homeassistant.components.analytics.analytics.HA_VERSION", MOCK_VERSION
), patch(
"homeassistant.components.analytics.analytics.energy_is_configured", AsyncMock()
) as energy_is_configured:
energy_is_configured.return_value = False
hex.return_value = MOCK_UUID
await analytics.send_analytics()
postdata = aioclient_mock.mock_calls[-1][2]
assert "energy" not in postdata
async def test_send_with_no_energy_config(hass, aioclient_mock):
"""Test send base prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
analytics = Analytics(hass)
await analytics.save_preferences({ATTR_BASE: True, ATTR_USAGE: True})
assert await async_setup_component(
hass, "energy", {"recorder": {"db_url": "sqlite://"}}
)
with patch("uuid.UUID.hex", new_callable=PropertyMock) as hex, patch(
"homeassistant.components.analytics.analytics.HA_VERSION", MOCK_VERSION
), patch(
"homeassistant.components.analytics.analytics.energy_is_configured", AsyncMock()
) as energy_is_configured:
energy_is_configured.return_value = False
hex.return_value = MOCK_UUID
await analytics.send_analytics()
postdata = aioclient_mock.mock_calls[-1][2]
assert not postdata["energy"]["configured"]
async def test_send_with_energy_config(hass, aioclient_mock):
"""Test send base prefrences are defined."""
aioclient_mock.post(ANALYTICS_ENDPOINT_URL, status=200)
analytics = Analytics(hass)
await analytics.save_preferences({ATTR_BASE: True, ATTR_USAGE: True})
assert await async_setup_component(
hass, "energy", {"recorder": {"db_url": "sqlite://"}}
)
with patch("uuid.UUID.hex", new_callable=PropertyMock) as hex, patch(
"homeassistant.components.analytics.analytics.HA_VERSION", MOCK_VERSION
), patch(
"homeassistant.components.analytics.analytics.energy_is_configured", AsyncMock()
) as energy_is_configured:
energy_is_configured.return_value = True
hex.return_value = MOCK_UUID
await analytics.send_analytics()
postdata = aioclient_mock.mock_calls[-1][2]
assert postdata["energy"]["configured"]

View File

@ -1,7 +1,7 @@
"""Test the Energy websocket API."""
import pytest
from homeassistant.components.energy import data
from homeassistant.components.energy import data, is_configured
from homeassistant.setup import async_setup_component
from tests.common import flush_store
@ -34,6 +34,8 @@ async def test_get_preferences_default(hass, hass_ws_client, hass_storage) -> No
manager.data = data.EnergyManager.default_preferences()
client = await hass_ws_client(hass)
assert not await is_configured(hass)
await client.send_json({"id": 5, "type": "energy/get_prefs"})
msg = await client.receive_json()
@ -119,6 +121,8 @@ async def test_save_preferences(hass, hass_ws_client, hass_storage) -> None:
assert hass_storage[data.STORAGE_KEY]["data"] == new_prefs
assert await is_configured(hass)
# Verify info reflects data.
await client.send_json({"id": 7, "type": "energy/info"})