diff --git a/custom_components/victron/coordinator.py b/custom_components/victron/coordinator.py index 2f9f7c5..8fb16f0 100644 --- a/custom_components/victron/coordinator.py +++ b/custom_components/victron/coordinator.py @@ -70,10 +70,10 @@ async def _async_update_data(self) -> dict: #TODO change this to work with partial updates for key,value in register_info_dict[name].items(): full_key = str(unit) + "." + key - self.data["data"][full_key] = None + # self.data["data"][full_key] = None unavailable_entities[full_key] = False - _LOGGER.warning(f"no valid data returned for entities of slave: {unit} if device was physically removed please force a rescan to resolve this issue") + _LOGGER.warning(f"no valid data returned for entities of slave: {unit} (if the device continues to no longer update) check if the device was physically removed. Before opening an issue please force a rescan to attempt to resolve this issue") else: parsed_data = OrderedDict(list(parsed_data.items()) + list(self.parse_register_data(data, register_info_dict[name], unit).items())) for key,value in register_info_dict[name].items(): diff --git a/custom_components/victron/hub.py b/custom_components/victron/hub.py index 6999fb1..634877d 100644 --- a/custom_components/victron/hub.py +++ b/custom_components/victron/hub.py @@ -18,6 +18,9 @@ def __init__(self, host: str, port: int) -> None: self._client = ModbusTcpClient(host=self.host, port=self.port) self._lock = threading.Lock() + def is_still_connected(self): + return self._client.is_socket_open() + def connect(self): return self._client.connect() diff --git a/custom_components/victron/sensor.py b/custom_components/victron/sensor.py index 2452796..935aec4 100644 --- a/custom_components/victron/sensor.py +++ b/custom_components/victron/sensor.py @@ -7,7 +7,7 @@ from datetime import timedelta from homeassistant.util import utcnow from homeassistant.helpers import event, entity -from homeassistant.core import HomeAssistant, HassJob +from homeassistant.core import HomeAssistant, HassJob, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.config_entries import ConfigEntry @@ -147,31 +147,23 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio super().__init__(coordinator) - async def async_update(self) -> None: + @callback + def _handle_coordinator_update(self) -> None: """Get the latest data and updates the states.""" try: - data = self.description.value_fn(self.coordinator.processed_data(), self.description.slave, self.description.key) - if self.entity_type is not None and isinstance(self.entity_type, TextReadEntityType): - self._attr_native_value = self.entity_type.decodeEnum(data).name.split("_DUPLICATE")[0] - else: - self._attr_native_value = data + if self.available: + data = self.description.value_fn(self.coordinator.processed_data(), self.description.slave, self.description.key) + if self.entity_type is not None and isinstance(self.entity_type, TextReadEntityType): + self._attr_native_value = self.entity_type.decodeEnum(data).name.split("_DUPLICATE")[0] + else: + self._attr_native_value = data + + self.async_write_ha_state() except (TypeError, IndexError): _LOGGER.debug("failed to retrieve value") # No data available self._attr_native_value = None - # Cancel the currently scheduled event if there is any - if self._unsub_update: - self._unsub_update() - self._unsub_update = None - - # Schedule the next update at exactly the next whole hour sharp - self._unsub_update = event.async_track_point_in_utc_time( - self.hass, - self._update_job, - utcnow() + timedelta(seconds=self.coordinator.interval), - ) - @property def available(self) -> bool: full_key = str(self.description.slave) + "." + self.description.key