From 09143c8bc42082b0f206f36097cf01967b8fdd05 Mon Sep 17 00:00:00 2001 From: Viet Ngoc <96962827+ngocjohn@users.noreply.github.com> Date: Sun, 21 Jul 2024 17:59:49 +0200 Subject: [PATCH] fix: display of moon age in days (#2) --- custom_components/lunar_phase/coordinator.py | 2 +- custom_components/lunar_phase/sensor.py | 50 +++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/custom_components/lunar_phase/coordinator.py b/custom_components/lunar_phase/coordinator.py index 3fd1cf7..58ec669 100644 --- a/custom_components/lunar_phase/coordinator.py +++ b/custom_components/lunar_phase/coordinator.py @@ -92,7 +92,7 @@ async def _async_update_data(self): ) except UpdateFailed: _LOGGER.error("Error fetching moon phase data") - _LOGGER.debug("Location: %s", self.location) + _LOGGER.debug("Age: %s", attributes.get("age")) return { "moon_phase": moon_phase, "attributes": attributes, diff --git a/custom_components/lunar_phase/sensor.py b/custom_components/lunar_phase/sensor.py index 94e95a5..518d474 100644 --- a/custom_components/lunar_phase/sensor.py +++ b/custom_components/lunar_phase/sensor.py @@ -49,7 +49,12 @@ async def async_setup_entry( sensors = [ MoonPhaseSensor(coordinator, config_entry), MoonAgeSensor( - coordinator, config_entry, STATE_ATTR_AGE, "Moon Age", "moon_age" + coordinator, + config_entry, + STATE_ATTR_AGE, + "Moon Age", + "moon_age", + "mdi:progress-clock", ), MoonDistanceSensor( coordinator, @@ -219,6 +224,11 @@ class MoonTimestampSensor(MoonAttributeSensor): _attr_device_class = SensorDeviceClass.TIMESTAMP + @callback + def _handle_coordinator_update(self) -> None: + """Update sensor with latest data from coordinator.""" + self.async_write_ha_state() + @property def native_value(self): """Return the state of the sensor.""" @@ -236,16 +246,52 @@ class MoonDistanceSensor(MoonAttributeSensor): _attr_native_unit_of_measurement = UnitOfLength.KILOMETERS _attr_state_class = SensorStateClass.MEASUREMENT + @callback + def _handle_coordinator_update(self) -> None: + """Update sensor with latest data from coordinator.""" + self.async_write_ha_state() + + @property + def native_value(self): + """Return the state of the sensor.""" + attributes = self.coordinator.data.get("attributes", {}) + return attributes.get(self._attribute) + class MoonAgeSensor(MoonAttributeSensor): """Representation of a Moon Age sensor.""" - _attr_device_class = SensorDeviceClass.DURATION + _attr_state_class = SensorStateClass.MEASUREMENT _attr_native_unit_of_measurement = UnitOfTime.DAYS + @callback + def _handle_coordinator_update(self) -> None: + """Update sensor with latest data from coordinator.""" + self.async_write_ha_state() + + @property + def native_value(self): + """Return the state of the sensor.""" + attributes = self.coordinator.data.get("attributes", {}) + value_in_days = attributes.get(self._attribute) + if value_in_days is not None: + return round(value_in_days, 2) + return None + class MoonIlluminationFractionSensor(MoonAttributeSensor): """Representation of a Moon Illumination Fraction sensor.""" _attr_native_unit_of_measurement = PERCENTAGE _attr_state_class = SensorStateClass.MEASUREMENT + + @callback + def _handle_coordinator_update(self) -> None: + """Update sensor with latest data from coordinator.""" + self.async_write_ha_state() + + @property + def native_value(self): + """Return the state of the sensor.""" + attributes = self.coordinator.data.get("attributes", {}) + return attributes.get(self._attribute)