Skip to content

Commit 7393900

Browse files
committed
feat: improve install_date calculation for analytics
1 parent c5fab93 commit 7393900

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

custom_components/powercalc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def start_schedule(_event: Event) -> None:
232232
"""Start the send schedule after the started event."""
233233
async_call_later(
234234
hass,
235-
900,
235+
10,
236236
HassJob(
237237
analytics.send_analytics,
238238
name="powercalc analytics startup",

custom_components/powercalc/analytics/analytics.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from collections import Counter
55
from collections.abc import Hashable
66
from dataclasses import asdict, dataclass
7-
from datetime import datetime, timedelta
7+
from datetime import UTC, datetime, timedelta
8+
from itertools import chain
89
import logging
910
from typing import Any, Literal, TypedDict
1011
import uuid
@@ -13,6 +14,7 @@
1314
from homeassistant.config_entries import ConfigEntry
1415
from homeassistant.const import __version__ as HA_VERSION # noqa
1516
from homeassistant.core import HomeAssistant
17+
from homeassistant.helpers import entity_registry
1618
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1719
from homeassistant.helpers.storage import Store
1820
from homeassistant.loader import async_get_integration
@@ -156,7 +158,7 @@ async def _prepare_payload(self) -> dict:
156158
DOMAIN,
157159
ENTRY_GLOBAL_CONFIG_UNIQUE_ID,
158160
)
159-
payload: dict = {
161+
return {
160162
"install_id": self.install_id,
161163
"install_date": await self._get_install_date(),
162164
"powercalc_version": powercalc_integration.version,
@@ -179,18 +181,21 @@ async def _prepare_payload(self) -> dict:
179181
},
180182
}
181183

182-
return payload
183-
184184
async def _get_install_date(self) -> str | None:
185-
min_date = None
186-
for entry in self.hass.config_entries.async_entries(DOMAIN):
187-
if min_date is None or entry.created_at < min_date:
188-
min_date = entry.created_at
189-
190-
if min_date:
191-
return min_date.isoformat()
185+
"""
186+
Get the install date based on the earliest created_at date of config entries and entities.
187+
"""
188+
cutoff = datetime(2000, 1, 1, tzinfo=UTC)
189+
dates = chain(
190+
(e.created_at for e in self.hass.config_entries.async_entries(DOMAIN) if e.created_at > cutoff),
191+
(e.created_at for e in entity_registry.async_get(self.hass).entities.values() if e.domain != DOMAIN and e.created_at > cutoff),
192+
)
192193

193-
return None
194+
try:
195+
install_date = min(dates)
196+
return install_date.isoformat(timespec="seconds").replace("+00:00", "Z")
197+
except ValueError:
198+
return None
194199

195200
async def _get_custom_profile_count(self) -> int:
196201
loader = ProfileLibrary.create_loader(self.hass, True)

0 commit comments

Comments
 (0)