Add sensor for total installations and integrations to Analytics Insights (#127248)
* Add sensor for total installations and integrations * Fix tests * Use pytest fixturepull/127331/head
parent
c6fa160c02
commit
3184951625
|
@ -31,6 +31,8 @@ if TYPE_CHECKING:
|
|||
class AnalyticsData:
|
||||
"""Analytics data class."""
|
||||
|
||||
active_installations: int
|
||||
reports_integrations: int
|
||||
core_integrations: dict[str, int]
|
||||
custom_integrations: dict[str, int]
|
||||
|
||||
|
@ -76,7 +78,12 @@ class HomeassistantAnalyticsDataUpdateCoordinator(DataUpdateCoordinator[Analytic
|
|||
integration: get_custom_integration_value(custom_data, integration)
|
||||
for integration in self._tracked_custom_integrations
|
||||
}
|
||||
return AnalyticsData(core_integrations, custom_integrations)
|
||||
return AnalyticsData(
|
||||
data.active_installations,
|
||||
data.reports_integrations,
|
||||
core_integrations,
|
||||
custom_integrations,
|
||||
)
|
||||
|
||||
|
||||
def get_custom_integration_value(
|
||||
|
|
|
@ -6,6 +6,12 @@
|
|||
},
|
||||
"custom_integrations": {
|
||||
"default": "mdi:puzzle-edit"
|
||||
},
|
||||
"total_active_installations": {
|
||||
"default": "mdi:puzzle"
|
||||
},
|
||||
"total_reports_integrations": {
|
||||
"default": "mdi:puzzle"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,6 +57,26 @@ def get_custom_integration_entity_description(
|
|||
)
|
||||
|
||||
|
||||
GENERAL_SENSORS = [
|
||||
AnalyticsSensorEntityDescription(
|
||||
key="total_active_installations",
|
||||
translation_key="total_active_installations",
|
||||
entity_registry_enabled_default=False,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
native_unit_of_measurement="active installations",
|
||||
value_fn=lambda data: data.active_installations,
|
||||
),
|
||||
AnalyticsSensorEntityDescription(
|
||||
key="total_reports_integrations",
|
||||
translation_key="total_reports_integrations",
|
||||
entity_registry_enabled_default=False,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
native_unit_of_measurement="active installations",
|
||||
value_fn=lambda data: data.reports_integrations,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: AnalyticsInsightsConfigEntry,
|
||||
|
@ -85,6 +105,12 @@ async def async_setup_entry(
|
|||
)
|
||||
for integration_domain in coordinator.data.custom_integrations
|
||||
)
|
||||
|
||||
entities.extend(
|
||||
HomeassistantAnalyticsSensor(coordinator, entity_description)
|
||||
for entity_description in GENERAL_SENSORS
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
|
|
|
@ -44,6 +44,12 @@
|
|||
"sensor": {
|
||||
"custom_integrations": {
|
||||
"name": "{custom_integration_domain} (custom)"
|
||||
},
|
||||
"total_active_installations": {
|
||||
"name": "Total active installations"
|
||||
},
|
||||
"total_reports_integrations": {
|
||||
"name": "Total reported integrations"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,6 +149,106 @@
|
|||
'state': '24388',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.homeassistant_analytics_total_active_installations-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.homeassistant_analytics_total_active_installations',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Total active installations',
|
||||
'platform': 'analytics_insights',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'total_active_installations',
|
||||
'unique_id': 'total_active_installations',
|
||||
'unit_of_measurement': 'active installations',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.homeassistant_analytics_total_active_installations-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Homeassistant Analytics Total active installations',
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
'unit_of_measurement': 'active installations',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.homeassistant_analytics_total_active_installations',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '310400',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.homeassistant_analytics_total_reported_integrations-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.homeassistant_analytics_total_reported_integrations',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Total reported integrations',
|
||||
'platform': 'analytics_insights',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'total_reports_integrations',
|
||||
'unique_id': 'total_reports_integrations',
|
||||
'unit_of_measurement': 'active installations',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.homeassistant_analytics_total_reported_integrations-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Homeassistant Analytics Total reported integrations',
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
'unit_of_measurement': 'active installations',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.homeassistant_analytics_total_reported_integrations',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '249256',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.homeassistant_analytics_youtube-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
|
@ -199,3 +299,103 @@
|
|||
'state': '339',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.total_active_installations-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.total_active_installations',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Total active installations',
|
||||
'platform': 'analytics_insights',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'total_active_installations',
|
||||
'unique_id': 'total_active_installations',
|
||||
'unit_of_measurement': 'active installations',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.total_active_installations-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Homeassistant Analytics Total active installations',
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
'unit_of_measurement': 'active installations',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.total_active_installations',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '310400',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.total_reports_integrations-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.total_reports_integrations',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Total reported integrations',
|
||||
'platform': 'analytics_insights',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'total_reports_integrations',
|
||||
'unique_id': 'total_reports_integrations',
|
||||
'unit_of_measurement': 'active installations',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.total_reports_integrations-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Homeassistant Analytics Total reported integrations',
|
||||
'state_class': <SensorStateClass.TOTAL: 'total'>,
|
||||
'unit_of_measurement': 'active installations',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.total_reports_integrations',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '249256',
|
||||
})
|
||||
# ---
|
||||
|
|
|
@ -4,6 +4,7 @@ from datetime import timedelta
|
|||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from python_homeassistant_analytics import (
|
||||
HomeassistantAnalyticsConnectionError,
|
||||
HomeassistantAnalyticsNotModifiedError,
|
||||
|
@ -19,6 +20,7 @@ from . import setup_integration
|
|||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
|
|
Loading…
Reference in New Issue