Skip to content

Commit 16af09f

Browse files
author
sfstar
authored
Merge pull request #90 from sfstar/feature/improve_connection_logic
Feature/improve connection logic
2 parents 7517db5 + f1a802f commit 16af09f

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

custom_components/victron/coordinator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ async def _async_update_data(self) -> dict:
7070
#TODO change this to work with partial updates
7171
for key,value in register_info_dict[name].items():
7272
full_key = str(unit) + "." + key
73-
self.data["data"][full_key] = None
73+
# self.data["data"][full_key] = None
7474
unavailable_entities[full_key] = False
7575

76-
_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")
76+
_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")
7777
else:
7878
parsed_data = OrderedDict(list(parsed_data.items()) + list(self.parse_register_data(data, register_info_dict[name], unit).items()))
7979
for key,value in register_info_dict[name].items():

custom_components/victron/hub.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def __init__(self, host: str, port: int) -> None:
1818
self._client = ModbusTcpClient(host=self.host, port=self.port)
1919
self._lock = threading.Lock()
2020

21+
def is_still_connected(self):
22+
return self._client.is_socket_open()
23+
2124
def connect(self):
2225
return self._client.connect()
2326

custom_components/victron/sensor.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datetime import timedelta
88
from homeassistant.util import utcnow
99
from homeassistant.helpers import event, entity
10-
from homeassistant.core import HomeAssistant, HassJob
10+
from homeassistant.core import HomeAssistant, HassJob, callback
1111
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1212
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1313
from homeassistant.config_entries import ConfigEntry
@@ -147,31 +147,23 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
147147

148148
super().__init__(coordinator)
149149

150-
async def async_update(self) -> None:
150+
@callback
151+
def _handle_coordinator_update(self) -> None:
151152
"""Get the latest data and updates the states."""
152153
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()
158162
except (TypeError, IndexError):
159163
_LOGGER.debug("failed to retrieve value")
160164
# No data available
161165
self._attr_native_value = None
162166

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-
175167
@property
176168
def available(self) -> bool:
177169
full_key = str(self.description.slave) + "." + self.description.key

0 commit comments

Comments
 (0)