Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix display of moon age in days #2

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading