Skip to content

Commit

Permalink
Merge pull request #90 from sfstar/feature/improve_connection_logic
Browse files Browse the repository at this point in the history
Feature/improve connection logic
  • Loading branch information
sfstar authored Jun 30, 2023
2 parents 7517db5 + f1a802f commit 16af09f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions custom_components/victron/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
3 changes: 3 additions & 0 deletions custom_components/victron/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
30 changes: 11 additions & 19 deletions custom_components/victron/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16af09f

Please sign in to comment.