Skip to content

Commit

Permalink
fix: display of moon age in days (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocjohn authored Jul 21, 2024
1 parent d0a29bd commit 09143c8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion custom_components/lunar_phase/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 48 additions & 2 deletions custom_components/lunar_phase/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand All @@ -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)

0 comments on commit 09143c8

Please sign in to comment.