Skip to content

Commit fc3d5d7

Browse files
committed
feat: add group type counts to analytics
1 parent c4992ad commit fc3d5d7

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

custom_components/powercalc/analytics/analytics.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
DATA_ANALYTICS,
2424
DATA_CONFIG_TYPES,
2525
DATA_GROUP_SIZES,
26+
DATA_GROUP_TYPES,
2627
DATA_HAS_GROUP_INCLUDE,
2728
DATA_POWER_PROFILES,
2829
DATA_SENSOR_TYPES,
@@ -32,6 +33,7 @@
3233
DOMAIN_CONFIG,
3334
ENTRY_GLOBAL_CONFIG_UNIQUE_ID,
3435
CalculationStrategy,
36+
GroupType,
3537
SensorType,
3638
)
3739
from custom_components.powercalc.power_profile.library import ProfileLibrary
@@ -54,6 +56,7 @@ class RuntimeAnalyticsData(TypedDict, total=False):
5456
strategies: Counter[CalculationStrategy]
5557
power_profiles: list[PowerProfile]
5658
source_domains: Counter[str]
59+
group_types: Counter[GroupType]
5760
group_sizes: list[int]
5861
uses_include: bool
5962
_seen: dict[str, set[str]]
@@ -170,6 +173,7 @@ async def _prepare_payload(self) -> dict:
170173
"by_model": Counter(f"{profile.manufacturer}:{profile.model}" for profile in power_profiles),
171174
"by_strategy": runtime_data.setdefault(DATA_STRATEGIES, Counter()),
172175
"by_source_domain": runtime_data.setdefault(DATA_SOURCE_DOMAINS, Counter()),
176+
"by_group_type": runtime_data.setdefault(DATA_GROUP_TYPES, Counter()),
173177
},
174178
}
175179

custom_components/powercalc/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
DATA_SENSOR_TYPES: Literal["sensor_types"] = "sensor_types"
3636
DATA_CONFIG_TYPES: Literal["config_types"] = "config_types"
3737
DATA_SOURCE_DOMAINS: Literal["source_domains"] = "source_domains"
38+
DATA_GROUP_TYPES: Literal["group_types"] = "group_types"
3839
DATA_STRATEGIES: Literal["strategies"] = "strategies"
3940
DATA_GROUP_SIZES: Literal["group_sizes"] = "group_sizes"
4041
DATA_HAS_GROUP_INCLUDE: Literal["has_group_include"] = "has_group_include"

custom_components/powercalc/sensors/group/factory.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from homeassistant.helpers.entity import Entity
44
from homeassistant.helpers.typing import ConfigType
55

6-
from custom_components.powercalc.const import CONF_GROUP_TYPE, GroupType
6+
from custom_components.powercalc.analytics.analytics import collect_analytics
7+
from custom_components.powercalc.const import CONF_GROUP_TYPE, DATA_GROUP_TYPES, GroupType
78
from custom_components.powercalc.errors import SensorConfigurationError
89
import custom_components.powercalc.sensors.group.custom as custom_group
910
import custom_components.powercalc.sensors.group.domain as domain_group
@@ -20,6 +21,8 @@ async def create_group_sensors(
2021
) -> list[Entity]:
2122
"""Create group sensors for a given sensor configuration."""
2223
group_type: GroupType = GroupType(sensor_config.get(CONF_GROUP_TYPE, GroupType.CUSTOM))
24+
collect_analytics(hass, config_entry).inc(DATA_GROUP_TYPES, group_type)
25+
2326
if group_type == GroupType.DOMAIN:
2427
return await domain_group.create_domain_group_sensor(
2528
hass,

tests/analytics/test_analytics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
DOMAIN_CONFIG,
2424
SERVICE_RELOAD,
2525
CalculationStrategy,
26+
GroupType,
2627
SensorType,
2728
)
2829
from tests.common import get_simple_fixed_config, run_powercalc_setup, setup_config_entry
@@ -240,3 +241,4 @@ async def test_group_sizes(hass: HomeAssistant) -> None:
240241
payload = await analytics._prepare_payload() # noqa: SLF001
241242

242243
assert payload["group_sizes"] == {6: 1, 10: 1}
244+
assert payload["counts"]["by_group_type"] == {GroupType.CUSTOM: 2, GroupType.STANDBY: 1}

0 commit comments

Comments
 (0)