44from collections import Counter
55from collections .abc import Hashable
66from dataclasses import asdict , dataclass
7- from datetime import datetime , timedelta
7+ from datetime import UTC , datetime , timedelta
8+ from itertools import chain
89import logging
910from typing import Any , Literal , TypedDict
1011import uuid
1314from homeassistant .config_entries import ConfigEntry
1415from homeassistant .const import __version__ as HA_VERSION # noqa
1516from homeassistant .core import HomeAssistant
17+ from homeassistant .helpers import entity_registry
1618from homeassistant .helpers .aiohttp_client import async_get_clientsession
1719from homeassistant .helpers .storage import Store
1820from 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