Skip to content

Commit 8e0c029

Browse files
committed
Cache results of Location.midnight & Location.noon
Do not force removal of suggested_display_precision from PeriodOfTimeSensor entities because, 1) it's no longer needed, and 2) the core sensor logic keeps changing it back to 2.
1 parent 677017f commit 8e0c029

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

custom_components/sun2/helpers.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ def from_loc_params(cls, lp: LocParams) -> Self:
108108
tzi = dt_util.get_time_zone(tz := lp.time_zone)
109109
if not tzi:
110110
LOGGER.warning("Did not find time zone: %s", lp.time_zone)
111-
return cls(Location(LocationInfo("", "", tz, lp.latitude, lp.longitude)), tzi)
111+
loc = Location(LocationInfo("", "", tz, lp.latitude, lp.longitude))
112+
# Cache results of methods used by many entity types.
113+
loc.midnight = lru_cache(10)(loc.midnight)
114+
loc.noon = lru_cache(10)(loc.noon)
115+
return cls(loc, tzi)
112116

113117

114118
@lru_cache
@@ -555,8 +559,16 @@ def _astral_dt_2_dttm_none(
555559
fmt_result = self._dttm_2_str(result)
556560
if local:
557561
result = nearest_second(result)
562+
if hasattr(func, "cache_info"):
563+
fmt_result += f" cache_info: {func.cache_info()}"
558564
LOGGER.debug(
559-
"%s: %-3s(%s)%35s-> %s", self._log_name, label, dt, "", fmt_result
565+
"%s: %-3s(%s, %5s)%28s-> %s",
566+
self._log_name,
567+
label,
568+
dt,
569+
local,
570+
"",
571+
fmt_result,
560572
)
561573
return result
562574

custom_components/sun2/sensor.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from astral.sun import SUN_APPARENT_RADIUS
1717

1818
from homeassistant.components.sensor import (
19-
DOMAIN as SENSOR_DOMAIN,
2019
SensorDeviceClass,
2120
SensorEntity,
2221
SensorEntityDescription,
@@ -39,7 +38,6 @@
3938
EventStateChangedData,
4039
callback,
4140
)
42-
from homeassistant.helpers import entity_registry as er
4341
from homeassistant.helpers.event import (
4442
async_track_point_in_utc_time,
4543
async_track_state_change_event,
@@ -838,30 +836,6 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
838836
)
839837
return data
840838

841-
async def async_added_to_hass(self) -> None:
842-
"""Run when entity about to be added to hass."""
843-
await super().async_added_to_hass()
844-
845-
# In 3.1.0 and earlier, entity_description.suggested_display_precision was set
846-
# to 3. Starting with HA 2024.2, that causes the state to be displayed as a
847-
# float instead of HH:MM:SS. To fix that
848-
# entity_description.suggested_display_precision is no longer being set.
849-
# However, due to a bug in the sensor component, that value is not getting
850-
# properly removed from the entity registry, causing the state to still be
851-
# displayed as a float. To work around that bug, we'll forcibly remove it from
852-
# the registry here if necessary.
853-
ent_reg = er.async_get(self.hass)
854-
sensor_options: Mapping[str, Any] = ent_reg.entities[
855-
self.entity_id
856-
].options.get(SENSOR_DOMAIN, {})
857-
if sensor_options.get("suggested_display_precision") is None:
858-
return
859-
sensor_options = dict(sensor_options)
860-
del sensor_options["suggested_display_precision"]
861-
ent_reg.async_update_entity_options(
862-
self.entity_id, SENSOR_DOMAIN, sensor_options or None
863-
)
864-
865839
def _astral_event(self, dt: date) -> float | None:
866840
"""Return astral event result."""
867841
if self._event == "daylight":

0 commit comments

Comments
 (0)