Skip to content

Commit

Permalink
Give OhmeChargeSchedulesCoordinator a kick to update
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Jan 3, 2024
1 parent 3deeccf commit e50ffa1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion custom_components/ohme/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Component constants"""
DOMAIN = "ohme"
USER_AGENT = "dan-r-homeassistant-ohme"
INTEGRATION_VERSION = "0.3.0"
INTEGRATION_VERSION = "0.3.1"

DATA_CLIENT = "client"
DATA_COORDINATORS = "coordinators"
Expand Down
21 changes: 19 additions & 2 deletions custom_components/ohme/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def __init__(self, coordinator, coordinator_schedules, hass: HomeAssistant, clie

self._attr_device_info = client.get_device_info()

async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self.coordinator.async_add_listener(
self._handle_coordinator_update, None
)
)
self.async_on_remove(
self.coordinator_schedules.async_add_listener(
self._handle_coordinator_update, None
)
)

@property
def unique_id(self):
"""The unique ID of the switch."""
Expand All @@ -65,8 +79,8 @@ def icon(self):
"""Icon of the sensor."""
return "mdi:battery-heart"

@property
def native_value(self):
@callback
def _handle_coordinator_update(self) -> None:
"""Get value from data returned from API by coordinator"""
# Set with the same logic as reading
if session_in_progress(self.coordinator.data):
Expand All @@ -75,4 +89,7 @@ def native_value(self):
target = round(self.coordinator_schedules.data['targetPercent'])

self._state = target if target > 0 else None

@property
def native_value(self):
return self._state
24 changes: 21 additions & 3 deletions custom_components/ohme/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ def __init__(self, coordinator, coordinator_schedules, hass: HomeAssistant, clie
"number.{}", "ohme_target_time", hass=hass)

self._attr_device_info = client.get_device_info()

async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self.coordinator.async_add_listener(
self._handle_coordinator_update, None
)
)
self.async_on_remove(
self.coordinator_schedules.async_add_listener(
self._handle_coordinator_update, None
)
)

@property
def unique_id(self):
Expand All @@ -68,10 +82,10 @@ def icon(self):
"""Icon of the sensor."""
return "mdi:alarm-check"

@property
def native_value(self):
@callback
def _handle_coordinator_update(self) -> None:
"""Get value from data returned from API by coordinator"""
# Set with the same logic as reading
# Read with the same logic as setting
target = None
if session_in_progress(self.coordinator.data):
target = self.coordinator.data['appliedRule']['targetTime']
Expand All @@ -84,4 +98,8 @@ def native_value(self):
minute=(target % 3600) // 60,
second=0
)
self.async_write_ha_state()

@property
def native_value(self):
return self._state

0 comments on commit e50ffa1

Please sign in to comment.