Skip to content

Commit 09143c8

Browse files
authored
fix: display of moon age in days (#2)
1 parent d0a29bd commit 09143c8

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

custom_components/lunar_phase/coordinator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def _async_update_data(self):
9292
)
9393
except UpdateFailed:
9494
_LOGGER.error("Error fetching moon phase data")
95-
_LOGGER.debug("Location: %s", self.location)
95+
_LOGGER.debug("Age: %s", attributes.get("age"))
9696
return {
9797
"moon_phase": moon_phase,
9898
"attributes": attributes,

custom_components/lunar_phase/sensor.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ async def async_setup_entry(
4949
sensors = [
5050
MoonPhaseSensor(coordinator, config_entry),
5151
MoonAgeSensor(
52-
coordinator, config_entry, STATE_ATTR_AGE, "Moon Age", "moon_age"
52+
coordinator,
53+
config_entry,
54+
STATE_ATTR_AGE,
55+
"Moon Age",
56+
"moon_age",
57+
"mdi:progress-clock",
5358
),
5459
MoonDistanceSensor(
5560
coordinator,
@@ -219,6 +224,11 @@ class MoonTimestampSensor(MoonAttributeSensor):
219224

220225
_attr_device_class = SensorDeviceClass.TIMESTAMP
221226

227+
@callback
228+
def _handle_coordinator_update(self) -> None:
229+
"""Update sensor with latest data from coordinator."""
230+
self.async_write_ha_state()
231+
222232
@property
223233
def native_value(self):
224234
"""Return the state of the sensor."""
@@ -236,16 +246,52 @@ class MoonDistanceSensor(MoonAttributeSensor):
236246
_attr_native_unit_of_measurement = UnitOfLength.KILOMETERS
237247
_attr_state_class = SensorStateClass.MEASUREMENT
238248

249+
@callback
250+
def _handle_coordinator_update(self) -> None:
251+
"""Update sensor with latest data from coordinator."""
252+
self.async_write_ha_state()
253+
254+
@property
255+
def native_value(self):
256+
"""Return the state of the sensor."""
257+
attributes = self.coordinator.data.get("attributes", {})
258+
return attributes.get(self._attribute)
259+
239260

240261
class MoonAgeSensor(MoonAttributeSensor):
241262
"""Representation of a Moon Age sensor."""
242263

243-
_attr_device_class = SensorDeviceClass.DURATION
264+
_attr_state_class = SensorStateClass.MEASUREMENT
244265
_attr_native_unit_of_measurement = UnitOfTime.DAYS
245266

267+
@callback
268+
def _handle_coordinator_update(self) -> None:
269+
"""Update sensor with latest data from coordinator."""
270+
self.async_write_ha_state()
271+
272+
@property
273+
def native_value(self):
274+
"""Return the state of the sensor."""
275+
attributes = self.coordinator.data.get("attributes", {})
276+
value_in_days = attributes.get(self._attribute)
277+
if value_in_days is not None:
278+
return round(value_in_days, 2)
279+
return None
280+
246281

247282
class MoonIlluminationFractionSensor(MoonAttributeSensor):
248283
"""Representation of a Moon Illumination Fraction sensor."""
249284

250285
_attr_native_unit_of_measurement = PERCENTAGE
251286
_attr_state_class = SensorStateClass.MEASUREMENT
287+
288+
@callback
289+
def _handle_coordinator_update(self) -> None:
290+
"""Update sensor with latest data from coordinator."""
291+
self.async_write_ha_state()
292+
293+
@property
294+
def native_value(self):
295+
"""Return the state of the sensor."""
296+
attributes = self.coordinator.data.get("attributes", {})
297+
return attributes.get(self._attribute)

0 commit comments

Comments
 (0)