Skip to content

Commit

Permalink
additional potential fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pail23 committed Dec 4, 2024
1 parent 5ab8411 commit fd5b80d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
11 changes: 6 additions & 5 deletions custom_components/stiebel_eltron_isg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ async def async_setup_entry(
):
"""Set up this integration using UI."""

name = entry.data.get(CONF_NAME)
host = entry.data.get(CONF_HOST)
port = entry.data.get(CONF_PORT)
scan_interval = entry.data[CONF_SCAN_INTERVAL]
name = str(entry.data.get(CONF_NAME))
host = str(entry.data.get(CONF_HOST))
port_data = entry.data.get(CONF_PORT)
port = int(port_data) if port_data is not None else 502
scan_interval = int(entry.data[CONF_SCAN_INTERVAL])

try:
model = await get_controller_model(host, port)
Expand Down Expand Up @@ -144,7 +145,7 @@ async def async_unload_entry(
) -> bool:
"""Handle removal of an entry."""
coordinator = entry.runtime_data.coordinator
await coordinator.shutdown()
await coordinator.close()
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


Expand Down
45 changes: 26 additions & 19 deletions custom_components/stiebel_eltron_isg/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
https://github.com/pail23/stiebel_eltron_isg
"""

import logging
import asyncio
import logging
from datetime import timedelta

from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand All @@ -24,7 +24,7 @@
_LOGGER: logging.Logger = logging.getLogger(__package__)


def get_isg_scaled_value(value, factor=10) -> float:
def get_isg_scaled_value(value: float, factor: float = 10) -> float | None:
"""Calculate the value out of a modbus register by scaling it."""
return value / factor if value != -32768 else None

Expand All @@ -35,15 +35,15 @@ class StiebelEltronModbusDataCoordinator(DataUpdateCoordinator):
def __init__(
self,
hass,
name,
host,
port,
name: str,
host: str,
port: int,
scan_interval,
):
"""Initialize the Modbus hub."""
self._hass = hass
self._host = host
self._model_id = 0
self._model_id: int = 0
self._client: AsyncModbusTcpClient = AsyncModbusTcpClient(host=host, port=port)
self._lock = asyncio.Lock()
self._scan_interval = timedelta(seconds=scan_interval)
Expand All @@ -53,20 +53,16 @@ def __init__(

async def close(self) -> None:
"""Disconnect client."""
_LOGGER.debug("Closing connection to %s", self._host)
async with self._lock:
self._client.close()

async def connect(self) -> None:
"""Connect client."""
_LOGGER.debug("Connecting to %s", self._host)
async with self._lock:
await self._client.connect()

async def shutdown(self) -> None:
"""Shutdown the coordinator and close all connections."""
if self.is_connected:
await self.close()
self._client = None

@property
def is_connected(self) -> bool:
"""Check modbus client connection status."""
Expand Down Expand Up @@ -101,16 +97,23 @@ def is_wpm(self) -> bool:

async def read_input_registers(self, slave, address, count):
"""Read input registers."""
_LOGGER.debug(
f"Reading {count} input registers from {address} with slave {slave}"
)
async with self._lock:
return await self._client.read_input_registers(address, count, slave)

async def read_holding_registers(self, slave, address, count):
"""Read holding registers."""
_LOGGER.debug(
f"Reading {count} holding registers from {address} with slave {slave}"
)
async with self._lock:
return await self._client.read_holding_registers(address, count, slave)

async def write_register(self, address, value, slave):
"""Write holding register."""
_LOGGER.debug(f"Writing {value} to register {address} with slave {slave}")
async with self._lock:
return await self._client.write_registers(address, value, slave)

Expand Down Expand Up @@ -162,12 +165,16 @@ def assign_if_increased(self, value: float | int, key: str) -> float:
"""Assign the value as new value or keep the old value from the internal cache in case the old value is larger than value."""
if value == 0:
return 0
if self.data and self.data.get(key) is not None:
old_value = float(self.data.get(key))
_LOGGER.debug(f"old value for {key} is {old_value} new value is {value}")
if old_value > value:
_LOGGER.info(
f"Value for {key} is not strictly increasing existing value is {old_value} and new value is {value}",
if self.data:
data = self.data.get(key)
if data is not None:
old_value = float(data)
_LOGGER.debug(
f"old value for {key} is {old_value} new value is {value}"
)
return old_value
if old_value > value:
_LOGGER.info(
f"Value for {key} is not strictly increasing existing value is {old_value} and new value is {value}",
)
return old_value
return value

0 comments on commit fd5b80d

Please sign in to comment.