Skip to content

Commit 7bfd5a5

Browse files
committed
HA dev 20250815.
1 parent 22397a2 commit 7bfd5a5

File tree

9 files changed

+25
-38
lines changed

9 files changed

+25
-38
lines changed

custom_components/modbus/binary_sensor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import logging
65
from typing import Any
76

87
from homeassistant.components.binary_sensor import BinarySensorEntity
@@ -24,6 +23,7 @@
2423

2524
from . import get_hub
2625
from .const import (
26+
_LOGGER,
2727
CALL_TYPE_COIL,
2828
CALL_TYPE_DISCRETE,
2929
CONF_SLAVE_COUNT,
@@ -32,8 +32,6 @@
3232
from .entity import BasePlatform
3333
from .modbus import ModbusHub
3434

35-
_LOGGER = logging.getLogger(__name__)
36-
3735
PARALLEL_UPDATES = 1
3836

3937

custom_components/modbus/climate.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import logging
65
import struct
76
from typing import Any, cast
87

@@ -44,6 +43,7 @@
4443

4544
from . import get_hub
4645
from .const import (
46+
_LOGGER,
4747
CALL_TYPE_COIL,
4848
CALL_TYPE_REGISTER_HOLDING,
4949
CALL_TYPE_WRITE_COIL,
@@ -104,8 +104,6 @@
104104
from .entity import BaseStructPlatform
105105
from .modbus import ModbusHub
106106

107-
_LOGGER = logging.getLogger(__name__)
108-
109107
PARALLEL_UPDATES = 1
110108

111109
HVACMODE_TO_TARG_TEMP_REG_INDEX_ARRAY = {
@@ -469,9 +467,6 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
469467

470468
async def _async_update(self) -> None:
471469
"""Update Target & Current Temperature."""
472-
# remark "now" is a dummy parameter to avoid problems with
473-
# async_track_time_interval
474-
475470
self._attr_target_temperature = await self._async_read_register(
476471
CALL_TYPE_REGISTER_HOLDING,
477472
self._target_temperature_register[

custom_components/modbus/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Constants used in modbus integration."""
22

33
from enum import Enum
4+
import logging
45

56
from homeassistant.const import (
67
CONF_ADDRESS,
@@ -96,6 +97,7 @@
9697
CONF_WRITE_TYPE = "write_type"
9798
CONF_ZERO_SUPPRESS = "zero_suppress"
9899

100+
DEVICE_ID = "device_id"
99101
RTUOVERTCP = "rtuovertcp"
100102
SERIAL = "serial"
101103
TCP = "tcp"
@@ -177,3 +179,5 @@ class DataType(str, Enum):
177179
LIGHT_MODBUS_SCALE_MIN = 0
178180
LIGHT_MODBUS_SCALE_MAX = 100
179181
LIGHT_MODBUS_INVALID_VALUE = 0xFFFF
182+
183+
_LOGGER = logging.getLogger(__package__)

custom_components/modbus/cover.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ async def async_close_cover(self, **kwargs: Any) -> None:
123123

124124
async def _async_update(self) -> None:
125125
"""Update the state of the cover."""
126-
# remark "now" is a dummy parameter to avoid problems with
127-
# async_track_time_interval
128126
result = await self._hub.async_pb_call(
129127
self._slave, self._address, 1, self._input_type
130128
)

custom_components/modbus/entity.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import asyncio
77
from collections.abc import Callable
88
from datetime import datetime, timedelta
9-
import logging
109
import struct
1110
from typing import Any, cast
1211

@@ -33,6 +32,7 @@
3332
from homeassistant.helpers.restore_state import RestoreEntity
3433

3534
from .const import (
35+
_LOGGER,
3636
CALL_TYPE_COIL,
3737
CALL_TYPE_DISCRETE,
3838
CALL_TYPE_REGISTER_HOLDING,
@@ -68,8 +68,6 @@
6868
)
6969
from .modbus import ModbusHub
7070

71-
_LOGGER = logging.getLogger(__name__)
72-
7371

7472
class BasePlatform(Entity):
7573
"""Base for readonly platforms."""
@@ -382,8 +380,6 @@ async def async_turn_off(self, **kwargs: Any) -> None:
382380

383381
async def _async_update(self) -> None:
384382
"""Update the entity state."""
385-
# remark "now" is a dummy parameter to avoid problems with
386-
# async_track_time_interval
387383
if not self._verify_active:
388384
self._attr_available = True
389385
return

custom_components/modbus/light.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import logging
65
from typing import Any
76

87
from homeassistant.components.light import (
@@ -35,7 +34,6 @@
3534
from .modbus import ModbusHub
3635

3736
PARALLEL_UPDATES = 1
38-
_LOGGER = logging.getLogger(__name__)
3937

4038

4139
async def async_setup_platform(

custom_components/modbus/manifest.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
"documentation": "https://www.home-assistant.io/integrations/modbus",
66
"iot_class": "local_polling",
77
"loggers": ["pymodbus"],
8-
"requirements": ["pymodbus==3.9.2"],
9-
"version": "1.0.0"
8+
"requirements": ["pymodbus==3.11.1"]
109
}

custom_components/modbus/modbus.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
from collections import namedtuple
77
from collections.abc import Callable
8-
import logging
98
from typing import Any
109

1110
from pymodbus.client import (
@@ -38,6 +37,7 @@
3837
from homeassistant.util.hass_dict import HassKey
3938

4039
from .const import (
40+
_LOGGER,
4141
ATTR_ADDRESS,
4242
ATTR_HUB,
4343
ATTR_SLAVE,
@@ -57,6 +57,7 @@
5757
CONF_PARITY,
5858
CONF_STOPBITS,
5959
DEFAULT_HUB,
60+
DEVICE_ID,
6061
MODBUS_DOMAIN as DOMAIN,
6162
PLATFORMS,
6263
RTUOVERTCP,
@@ -70,7 +71,6 @@
7071
)
7172
from .validators import check_config
7273

73-
_LOGGER = logging.getLogger(__name__)
7474
DATA_MODBUS_HUBS: HassKey[dict[str, ModbusHub]] = HassKey(DOMAIN)
7575

7676

@@ -262,6 +262,8 @@ def __init__(self, hass: HomeAssistant, client_config: dict[str, Any]) -> None:
262262
self._config_type = client_config[CONF_TYPE]
263263
self._config_delay = client_config[CONF_DELAY]
264264
self._pb_request: dict[str, RunEntry] = {}
265+
self._connect_task: asyncio.Task
266+
self._last_log_error: str = ""
265267
self._pb_class = {
266268
SERIAL: AsyncModbusSerialClient,
267269
TCP: AsyncModbusTcpClient,
@@ -302,13 +304,12 @@ def __init__(self, hass: HomeAssistant, client_config: dict[str, Any]) -> None:
302304
else:
303305
self._msg_wait = 0
304306

305-
def _log_error(self, text: str, error_state: bool = True) -> None:
307+
def _log_error(self, text: str) -> None:
308+
if text == self._last_log_error:
309+
return
310+
self._last_log_error = text
306311
log_text = f"Pymodbus: {self.name}: {text}"
307-
if self._in_error:
308-
_LOGGER.debug(log_text)
309-
else:
310-
_LOGGER.error(log_text)
311-
self._in_error = error_state
312+
_LOGGER.error(log_text)
312313

313314
async def async_pb_connect(self) -> None:
314315
"""Connect to device, async."""
@@ -317,7 +318,7 @@ async def async_pb_connect(self) -> None:
317318
await self._client.connect() # type: ignore[union-attr]
318319
except ModbusException as exception_error:
319320
err = f"{self.name} connect failed, retry in pymodbus ({exception_error!s})"
320-
self._log_error(err, error_state=False)
321+
self._log_error(err)
321322
return
322323
message = f"modbus {self.name} communication open"
323324
_LOGGER.info(message)
@@ -327,7 +328,7 @@ async def async_setup(self) -> bool:
327328
try:
328329
self._client = self._pb_class[self._config_type](**self._pb_params)
329330
except ModbusException as exception_error:
330-
self._log_error(str(exception_error), error_state=False)
331+
self._log_error(str(exception_error))
331332
return False
332333

333334
for entry in PB_CALL:
@@ -336,7 +337,7 @@ async def async_setup(self) -> bool:
336337
entry.attr, func, entry.value_attr_name
337338
)
338339

339-
self.hass.async_create_background_task(
340+
self._connect_task = self.hass.async_create_background_task(
340341
self.async_pb_connect(), "modbus-connect"
341342
)
342343

@@ -365,6 +366,9 @@ async def async_close(self) -> None:
365366
if self._async_cancel_listener:
366367
self._async_cancel_listener()
367368
self._async_cancel_listener = None
369+
if not self._connect_task.done():
370+
self._connect_task.cancel()
371+
368372
async with self._lock:
369373
if self._client:
370374
try:
@@ -381,7 +385,7 @@ async def low_level_pb_call(
381385
) -> ModbusPDU | None:
382386
"""Call sync. pymodbus."""
383387
kwargs: dict[str, Any] = (
384-
{ATTR_SLAVE: slave} if slave is not None else {ATTR_SLAVE: 1}
388+
{DEVICE_ID: slave} if slave is not None else {DEVICE_ID: 1}
385389
)
386390
entry = self._pb_request[use_call]
387391

custom_components/modbus/sensor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import logging
65
from typing import Any
76

87
from homeassistant.components.sensor import (
@@ -26,12 +25,10 @@
2625
)
2726

2827
from . import get_hub
29-
from .const import CONF_SLAVE_COUNT, CONF_VIRTUAL_COUNT
28+
from .const import _LOGGER, CONF_SLAVE_COUNT, CONF_VIRTUAL_COUNT
3029
from .entity import BaseStructPlatform
3130
from .modbus import ModbusHub
3231

33-
_LOGGER = logging.getLogger(__name__)
34-
3532
PARALLEL_UPDATES = 1
3633

3734

@@ -109,8 +106,6 @@ async def async_added_to_hass(self) -> None:
109106

110107
async def _async_update(self) -> None:
111108
"""Update the state of the sensor."""
112-
# remark "now" is a dummy parameter to avoid problems with
113-
# async_track_time_interval
114109
self._cancel_call = None
115110
raw_result = await self._hub.async_pb_call(
116111
self._slave, self._address, self._count, self._input_type

0 commit comments

Comments
 (0)