|
7 | 7 | from datetime import timedelta |
8 | 8 | from homeassistant.util import utcnow |
9 | 9 | from homeassistant.helpers import event, entity |
10 | | -from homeassistant.core import HomeAssistant, HassJob |
| 10 | +from homeassistant.core import HomeAssistant, HassJob, callback |
11 | 11 | from homeassistant.helpers.entity_platform import AddEntitiesCallback |
12 | 12 | from homeassistant.helpers.update_coordinator import CoordinatorEntity |
13 | 13 | from homeassistant.config_entries import ConfigEntry |
@@ -147,31 +147,23 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio |
147 | 147 |
|
148 | 148 | super().__init__(coordinator) |
149 | 149 |
|
150 | | - async def async_update(self) -> None: |
| 150 | + @callback |
| 151 | + def _handle_coordinator_update(self) -> None: |
151 | 152 | """Get the latest data and updates the states.""" |
152 | 153 | try: |
153 | | - data = self.description.value_fn(self.coordinator.processed_data(), self.description.slave, self.description.key) |
154 | | - if self.entity_type is not None and isinstance(self.entity_type, TextReadEntityType): |
155 | | - self._attr_native_value = self.entity_type.decodeEnum(data).name.split("_DUPLICATE")[0] |
156 | | - else: |
157 | | - self._attr_native_value = data |
| 154 | + if self.available: |
| 155 | + data = self.description.value_fn(self.coordinator.processed_data(), self.description.slave, self.description.key) |
| 156 | + if self.entity_type is not None and isinstance(self.entity_type, TextReadEntityType): |
| 157 | + self._attr_native_value = self.entity_type.decodeEnum(data).name.split("_DUPLICATE")[0] |
| 158 | + else: |
| 159 | + self._attr_native_value = data |
| 160 | + |
| 161 | + self.async_write_ha_state() |
158 | 162 | except (TypeError, IndexError): |
159 | 163 | _LOGGER.debug("failed to retrieve value") |
160 | 164 | # No data available |
161 | 165 | self._attr_native_value = None |
162 | 166 |
|
163 | | - # Cancel the currently scheduled event if there is any |
164 | | - if self._unsub_update: |
165 | | - self._unsub_update() |
166 | | - self._unsub_update = None |
167 | | - |
168 | | - # Schedule the next update at exactly the next whole hour sharp |
169 | | - self._unsub_update = event.async_track_point_in_utc_time( |
170 | | - self.hass, |
171 | | - self._update_job, |
172 | | - utcnow() + timedelta(seconds=self.coordinator.interval), |
173 | | - ) |
174 | | - |
175 | 167 | @property |
176 | 168 | def available(self) -> bool: |
177 | 169 | full_key = str(self.description.slave) + "." + self.description.key |
|
0 commit comments