2021-02-12 15:33:18 +00:00
|
|
|
"""Support for Modbus."""
|
2021-08-08 20:48:33 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
import asyncio
|
2021-07-31 21:17:23 +00:00
|
|
|
from collections import namedtuple
|
2021-09-29 14:19:06 +00:00
|
|
|
from collections.abc import Callable
|
2021-02-12 15:33:18 +00:00
|
|
|
import logging
|
2021-09-29 14:19:06 +00:00
|
|
|
from typing import Any
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
from pymodbus.client.sync import (
|
|
|
|
BaseModbusClient,
|
|
|
|
ModbusSerialClient,
|
|
|
|
ModbusTcpClient,
|
|
|
|
ModbusUdpClient,
|
|
|
|
)
|
2021-04-25 21:11:01 +00:00
|
|
|
from pymodbus.constants import Defaults
|
2021-04-19 15:18:15 +00:00
|
|
|
from pymodbus.exceptions import ModbusException
|
2021-09-20 12:59:30 +00:00
|
|
|
from pymodbus.pdu import ModbusResponse
|
2021-07-05 09:45:50 +00:00
|
|
|
from pymodbus.transaction import ModbusRtuFramer
|
2021-09-20 16:47:05 +00:00
|
|
|
import voluptuous as vol
|
2021-02-12 15:33:18 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_DELAY,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_METHOD,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_TIMEOUT,
|
|
|
|
CONF_TYPE,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
)
|
2021-09-20 16:47:05 +00:00
|
|
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
2021-05-15 17:54:17 +00:00
|
|
|
from homeassistant.helpers.discovery import async_load_platform
|
2021-09-14 07:42:50 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2021-09-20 16:47:05 +00:00
|
|
|
from homeassistant.helpers.event import Event, async_call_later
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2021-02-12 15:33:18 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_ADDRESS,
|
|
|
|
ATTR_HUB,
|
2021-04-04 12:02:47 +00:00
|
|
|
ATTR_STATE,
|
2021-02-12 15:33:18 +00:00
|
|
|
ATTR_UNIT,
|
|
|
|
ATTR_VALUE,
|
2021-05-17 09:20:12 +00:00
|
|
|
CALL_TYPE_COIL,
|
|
|
|
CALL_TYPE_DISCRETE,
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CALL_TYPE_REGISTER_INPUT,
|
|
|
|
CALL_TYPE_WRITE_COIL,
|
|
|
|
CALL_TYPE_WRITE_COILS,
|
|
|
|
CALL_TYPE_WRITE_REGISTER,
|
|
|
|
CALL_TYPE_WRITE_REGISTERS,
|
2021-02-12 15:33:18 +00:00
|
|
|
CONF_BAUDRATE,
|
|
|
|
CONF_BYTESIZE,
|
2021-05-14 08:54:23 +00:00
|
|
|
CONF_CLOSE_COMM_ON_ERROR,
|
2021-08-08 04:10:08 +00:00
|
|
|
CONF_MSG_WAIT,
|
2021-02-12 15:33:18 +00:00
|
|
|
CONF_PARITY,
|
2021-06-06 07:13:50 +00:00
|
|
|
CONF_RETRIES,
|
|
|
|
CONF_RETRY_ON_EMPTY,
|
2021-02-12 15:33:18 +00:00
|
|
|
CONF_STOPBITS,
|
2021-04-19 15:18:15 +00:00
|
|
|
DEFAULT_HUB,
|
2021-02-12 15:33:18 +00:00
|
|
|
MODBUS_DOMAIN as DOMAIN,
|
2021-05-10 17:28:38 +00:00
|
|
|
PLATFORMS,
|
2021-08-18 10:56:54 +00:00
|
|
|
RTUOVERTCP,
|
|
|
|
SERIAL,
|
2021-09-14 07:42:50 +00:00
|
|
|
SERVICE_RESTART,
|
|
|
|
SERVICE_STOP,
|
2021-02-12 15:33:18 +00:00
|
|
|
SERVICE_WRITE_COIL,
|
|
|
|
SERVICE_WRITE_REGISTER,
|
2021-09-14 07:42:50 +00:00
|
|
|
SIGNAL_START_ENTITY,
|
|
|
|
SIGNAL_STOP_ENTITY,
|
2021-08-18 10:56:54 +00:00
|
|
|
TCP,
|
|
|
|
UDP,
|
2021-02-12 15:33:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-08-08 20:48:33 +00:00
|
|
|
|
2021-07-31 21:17:23 +00:00
|
|
|
ConfEntry = namedtuple("ConfEntry", "call_type attr func_name")
|
|
|
|
RunEntry = namedtuple("RunEntry", "attr func")
|
|
|
|
PYMODBUS_CALL = [
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_COIL,
|
|
|
|
"bits",
|
|
|
|
"read_coils",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_DISCRETE,
|
|
|
|
"bits",
|
|
|
|
"read_discrete_inputs",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
"registers",
|
|
|
|
"read_holding_registers",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_REGISTER_INPUT,
|
|
|
|
"registers",
|
|
|
|
"read_input_registers",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_WRITE_COIL,
|
|
|
|
"value",
|
|
|
|
"write_coil",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_WRITE_COILS,
|
|
|
|
"count",
|
|
|
|
"write_coils",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_WRITE_REGISTER,
|
|
|
|
"value",
|
|
|
|
"write_register",
|
|
|
|
),
|
|
|
|
ConfEntry(
|
|
|
|
CALL_TYPE_WRITE_REGISTERS,
|
|
|
|
"count",
|
|
|
|
"write_registers",
|
|
|
|
),
|
|
|
|
]
|
2021-06-18 09:20:44 +00:00
|
|
|
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
async def async_modbus_setup(
|
2021-09-20 16:47:05 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
service_write_register_schema: vol.Schema,
|
|
|
|
service_write_coil_schema: vol.Schema,
|
|
|
|
service_stop_start_schema: vol.Schema,
|
|
|
|
) -> bool:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Set up Modbus component."""
|
|
|
|
|
2021-04-19 15:18:15 +00:00
|
|
|
hass.data[DOMAIN] = hub_collect = {}
|
2021-02-12 15:33:18 +00:00
|
|
|
for conf_hub in config[DOMAIN]:
|
2021-05-15 17:54:17 +00:00
|
|
|
my_hub = ModbusHub(hass, conf_hub)
|
|
|
|
hub_collect[conf_hub[CONF_NAME]] = my_hub
|
2021-02-12 15:33:18 +00:00
|
|
|
|
|
|
|
# modbus needs to be activated before components are loaded
|
|
|
|
# to avoid a racing problem
|
2021-06-05 12:39:09 +00:00
|
|
|
if not await my_hub.async_setup():
|
|
|
|
return False
|
2021-02-12 15:33:18 +00:00
|
|
|
|
|
|
|
# load platforms
|
2021-05-10 17:28:38 +00:00
|
|
|
for component, conf_key in PLATFORMS:
|
2021-02-12 15:33:18 +00:00
|
|
|
if conf_key in conf_hub:
|
2021-05-15 17:54:17 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
async_load_platform(hass, component, DOMAIN, conf_hub, config)
|
|
|
|
)
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_stop_modbus(event: Event) -> None:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Stop Modbus service."""
|
2021-04-19 15:18:15 +00:00
|
|
|
|
2021-09-14 07:42:50 +00:00
|
|
|
async_dispatcher_send(hass, SIGNAL_STOP_ENTITY)
|
2021-02-12 15:33:18 +00:00
|
|
|
for client in hub_collect.values():
|
2021-05-15 17:54:17 +00:00
|
|
|
await client.async_close()
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_modbus)
|
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_write_register(service: ServiceCall) -> None:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Write Modbus registers."""
|
|
|
|
unit = int(float(service.data[ATTR_UNIT]))
|
|
|
|
address = int(float(service.data[ATTR_ADDRESS]))
|
|
|
|
value = service.data[ATTR_VALUE]
|
2021-09-14 07:42:50 +00:00
|
|
|
hub = hub_collect[
|
2021-04-19 15:18:15 +00:00
|
|
|
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
|
2021-09-14 07:42:50 +00:00
|
|
|
]
|
2021-02-12 15:33:18 +00:00
|
|
|
if isinstance(value, list):
|
2021-09-14 07:42:50 +00:00
|
|
|
await hub.async_pymodbus_call(
|
2021-05-19 09:39:53 +00:00
|
|
|
unit, address, [int(float(i)) for i in value], CALL_TYPE_WRITE_REGISTERS
|
2021-02-12 15:33:18 +00:00
|
|
|
)
|
|
|
|
else:
|
2021-09-14 07:42:50 +00:00
|
|
|
await hub.async_pymodbus_call(
|
2021-05-19 09:39:53 +00:00
|
|
|
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
|
2021-05-15 17:54:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_WRITE_REGISTER,
|
|
|
|
async_write_register,
|
|
|
|
schema=service_write_register_schema,
|
|
|
|
)
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_write_coil(service: ServiceCall) -> None:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Write Modbus coil."""
|
|
|
|
unit = service.data[ATTR_UNIT]
|
|
|
|
address = service.data[ATTR_ADDRESS]
|
|
|
|
state = service.data[ATTR_STATE]
|
2021-09-14 07:42:50 +00:00
|
|
|
hub = hub_collect[
|
2021-04-19 15:18:15 +00:00
|
|
|
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
|
2021-09-14 07:42:50 +00:00
|
|
|
]
|
2021-04-03 11:15:01 +00:00
|
|
|
if isinstance(state, list):
|
2021-09-14 07:42:50 +00:00
|
|
|
await hub.async_pymodbus_call(unit, address, state, CALL_TYPE_WRITE_COILS)
|
2021-04-03 11:15:01 +00:00
|
|
|
else:
|
2021-09-14 07:42:50 +00:00
|
|
|
await hub.async_pymodbus_call(unit, address, state, CALL_TYPE_WRITE_COIL)
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_WRITE_COIL, async_write_coil, schema=service_write_coil_schema
|
2021-02-12 15:33:18 +00:00
|
|
|
)
|
2021-09-14 07:42:50 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_stop_hub(service: ServiceCall) -> None:
|
2021-09-14 07:42:50 +00:00
|
|
|
"""Stop Modbus hub."""
|
|
|
|
async_dispatcher_send(hass, SIGNAL_STOP_ENTITY)
|
|
|
|
hub = hub_collect[service.data[ATTR_HUB]]
|
|
|
|
await hub.async_close()
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_STOP, async_stop_hub, schema=service_stop_start_schema
|
|
|
|
)
|
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_restart_hub(service: ServiceCall) -> None:
|
2021-09-14 07:42:50 +00:00
|
|
|
"""Restart Modbus hub."""
|
|
|
|
async_dispatcher_send(hass, SIGNAL_START_ENTITY)
|
|
|
|
hub = hub_collect[service.data[ATTR_HUB]]
|
|
|
|
await hub.async_restart()
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_RESTART, async_restart_hub, schema=service_stop_start_schema
|
|
|
|
)
|
2021-02-12 15:33:18 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class ModbusHub:
|
|
|
|
"""Thread safe wrapper class for pymodbus."""
|
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
def __init__(self, hass: HomeAssistant, client_config: dict[str, Any]) -> None:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Initialize the Modbus hub."""
|
|
|
|
|
|
|
|
# generic configuration
|
2021-09-20 16:47:05 +00:00
|
|
|
self._client: BaseModbusClient | None = None
|
|
|
|
self._async_cancel_listener: Callable[[], None] | None = None
|
2021-04-19 15:18:15 +00:00
|
|
|
self._in_error = False
|
2021-05-15 17:54:17 +00:00
|
|
|
self._lock = asyncio.Lock()
|
|
|
|
self.hass = hass
|
2021-08-08 20:48:33 +00:00
|
|
|
self.name = client_config[CONF_NAME]
|
2021-02-12 15:33:18 +00:00
|
|
|
self._config_type = client_config[CONF_TYPE]
|
2021-05-08 11:28:35 +00:00
|
|
|
self._config_delay = client_config[CONF_DELAY]
|
2021-09-20 16:47:05 +00:00
|
|
|
self._pb_call: dict[str, RunEntry] = {}
|
2021-06-18 09:20:44 +00:00
|
|
|
self._pb_class = {
|
2021-08-18 10:56:54 +00:00
|
|
|
SERIAL: ModbusSerialClient,
|
|
|
|
TCP: ModbusTcpClient,
|
|
|
|
UDP: ModbusUdpClient,
|
|
|
|
RTUOVERTCP: ModbusTcpClient,
|
2021-06-18 09:20:44 +00:00
|
|
|
}
|
|
|
|
self._pb_params = {
|
|
|
|
"port": client_config[CONF_PORT],
|
|
|
|
"timeout": client_config[CONF_TIMEOUT],
|
|
|
|
"reset_socket": client_config[CONF_CLOSE_COMM_ON_ERROR],
|
|
|
|
"retries": client_config[CONF_RETRIES],
|
|
|
|
"retry_on_empty": client_config[CONF_RETRY_ON_EMPTY],
|
|
|
|
}
|
2021-08-18 10:56:54 +00:00
|
|
|
if self._config_type == SERIAL:
|
2021-02-12 15:33:18 +00:00
|
|
|
# serial configuration
|
2021-06-18 09:20:44 +00:00
|
|
|
self._pb_params.update(
|
|
|
|
{
|
|
|
|
"method": client_config[CONF_METHOD],
|
|
|
|
"baudrate": client_config[CONF_BAUDRATE],
|
|
|
|
"stopbits": client_config[CONF_STOPBITS],
|
|
|
|
"bytesize": client_config[CONF_BYTESIZE],
|
|
|
|
"parity": client_config[CONF_PARITY],
|
|
|
|
}
|
|
|
|
)
|
2021-02-12 15:33:18 +00:00
|
|
|
else:
|
|
|
|
# network configuration
|
2021-06-18 09:20:44 +00:00
|
|
|
self._pb_params["host"] = client_config[CONF_HOST]
|
2021-08-18 10:56:54 +00:00
|
|
|
if self._config_type == RTUOVERTCP:
|
2021-07-05 09:45:50 +00:00
|
|
|
self._pb_params["framer"] = ModbusRtuFramer
|
2021-06-18 09:20:44 +00:00
|
|
|
|
|
|
|
Defaults.Timeout = client_config[CONF_TIMEOUT]
|
2021-08-08 04:10:08 +00:00
|
|
|
if CONF_MSG_WAIT in client_config:
|
|
|
|
self._msg_wait = client_config[CONF_MSG_WAIT] / 1000
|
2021-08-18 10:56:54 +00:00
|
|
|
elif self._config_type == SERIAL:
|
2021-08-08 04:10:08 +00:00
|
|
|
self._msg_wait = 30 / 1000
|
|
|
|
else:
|
|
|
|
self._msg_wait = 0
|
2021-05-17 09:20:12 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
def _log_error(self, text: str, error_state: bool = True) -> None:
|
2021-08-27 16:26:57 +00:00
|
|
|
log_text = f"Pymodbus: {self.name}: {text}"
|
2021-04-19 15:18:15 +00:00
|
|
|
if self._in_error:
|
2021-04-29 13:59:17 +00:00
|
|
|
_LOGGER.debug(log_text)
|
2021-04-19 15:18:15 +00:00
|
|
|
else:
|
2021-04-29 13:59:17 +00:00
|
|
|
_LOGGER.error(log_text)
|
2021-04-19 15:18:15 +00:00
|
|
|
self._in_error = error_state
|
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_setup(self) -> bool:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Set up pymodbus client."""
|
2021-04-19 15:18:15 +00:00
|
|
|
try:
|
2021-06-18 09:20:44 +00:00
|
|
|
self._client = self._pb_class[self._config_type](**self._pb_params)
|
2021-04-19 15:18:15 +00:00
|
|
|
except ModbusException as exception_error:
|
2021-06-05 12:39:09 +00:00
|
|
|
self._log_error(str(exception_error), error_state=False)
|
|
|
|
return False
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-07-31 21:17:23 +00:00
|
|
|
for entry in PYMODBUS_CALL:
|
|
|
|
func = getattr(self._client, entry.func_name)
|
|
|
|
self._pb_call[entry.call_type] = RunEntry(entry.attr, func)
|
2021-06-18 09:20:44 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
async with self._lock:
|
2021-06-05 12:39:09 +00:00
|
|
|
if not await self.hass.async_add_executor_job(self._pymodbus_connect):
|
2021-08-08 20:48:33 +00:00
|
|
|
err = f"{self.name} connect failed, retry in pymodbus"
|
2021-07-19 11:01:50 +00:00
|
|
|
self._log_error(err, error_state=False)
|
2021-10-11 18:26:36 +00:00
|
|
|
return False
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-05-08 11:28:35 +00:00
|
|
|
# Start counting down to allow modbus requests.
|
|
|
|
if self._config_delay:
|
2021-05-15 17:54:17 +00:00
|
|
|
self._async_cancel_listener = async_call_later(
|
|
|
|
self.hass, self._config_delay, self.async_end_delay
|
|
|
|
)
|
2021-10-11 18:26:36 +00:00
|
|
|
return True
|
2021-05-08 11:28:35 +00:00
|
|
|
|
2021-05-15 17:54:17 +00:00
|
|
|
@callback
|
2021-09-20 16:47:05 +00:00
|
|
|
def async_end_delay(self, args: Any) -> None:
|
2021-05-08 11:28:35 +00:00
|
|
|
"""End startup delay."""
|
2021-05-15 17:54:17 +00:00
|
|
|
self._async_cancel_listener = None
|
2021-05-08 11:28:35 +00:00
|
|
|
self._config_delay = 0
|
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_restart(self) -> None:
|
2021-09-14 07:42:50 +00:00
|
|
|
"""Reconnect client."""
|
|
|
|
if self._client:
|
|
|
|
await self.async_close()
|
|
|
|
|
|
|
|
await self.async_setup()
|
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
async def async_close(self) -> None:
|
2021-08-08 20:48:33 +00:00
|
|
|
"""Disconnect client."""
|
|
|
|
if self._async_cancel_listener:
|
|
|
|
self._async_cancel_listener()
|
|
|
|
self._async_cancel_listener = None
|
2021-08-28 21:04:33 +00:00
|
|
|
async with self._lock:
|
|
|
|
if self._client:
|
2021-08-26 13:23:00 +00:00
|
|
|
try:
|
|
|
|
self._client.close()
|
|
|
|
except ModbusException as exception_error:
|
|
|
|
self._log_error(str(exception_error))
|
2021-09-14 07:42:50 +00:00
|
|
|
del self._client
|
2021-08-28 21:04:33 +00:00
|
|
|
self._client = None
|
2021-09-14 07:42:50 +00:00
|
|
|
message = f"modbus {self.name} communication closed"
|
|
|
|
_LOGGER.warning(message)
|
2021-05-15 17:54:17 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
def _pymodbus_connect(self) -> bool:
|
2021-02-12 15:33:18 +00:00
|
|
|
"""Connect client."""
|
2021-05-15 17:54:17 +00:00
|
|
|
try:
|
2021-10-11 18:26:36 +00:00
|
|
|
self._client.connect() # type: ignore[union-attr]
|
2021-05-15 17:54:17 +00:00
|
|
|
except ModbusException as exception_error:
|
2021-06-05 12:39:09 +00:00
|
|
|
self._log_error(str(exception_error), error_state=False)
|
|
|
|
return False
|
2021-09-14 07:42:50 +00:00
|
|
|
else:
|
|
|
|
message = f"modbus {self.name} communication open"
|
|
|
|
_LOGGER.warning(message)
|
|
|
|
return True
|
2021-02-12 15:33:18 +00:00
|
|
|
|
2021-09-20 16:47:05 +00:00
|
|
|
def _pymodbus_call(
|
|
|
|
self, unit: int, address: int, value: int | list[int], use_call: str
|
|
|
|
) -> ModbusResponse:
|
2021-05-15 17:54:17 +00:00
|
|
|
"""Call sync. pymodbus."""
|
|
|
|
kwargs = {"unit": unit} if unit else {}
|
2021-07-31 21:17:23 +00:00
|
|
|
entry = self._pb_call[use_call]
|
2021-05-15 17:54:17 +00:00
|
|
|
try:
|
2021-07-31 21:17:23 +00:00
|
|
|
result = entry.func(address, value, **kwargs)
|
2021-05-15 17:54:17 +00:00
|
|
|
except ModbusException as exception_error:
|
2021-06-05 12:39:09 +00:00
|
|
|
self._log_error(str(exception_error))
|
2021-06-14 08:56:24 +00:00
|
|
|
return None
|
2021-07-31 21:17:23 +00:00
|
|
|
if not hasattr(result, entry.attr):
|
2021-06-05 12:39:09 +00:00
|
|
|
self._log_error(str(result))
|
2021-05-08 11:28:35 +00:00
|
|
|
return None
|
2021-05-15 17:54:17 +00:00
|
|
|
self._in_error = False
|
|
|
|
return result
|
|
|
|
|
2021-09-20 12:59:30 +00:00
|
|
|
async def async_pymodbus_call(
|
|
|
|
self,
|
2021-09-20 16:47:05 +00:00
|
|
|
unit: int | None,
|
2021-09-20 12:59:30 +00:00
|
|
|
address: int,
|
2021-09-20 16:47:05 +00:00
|
|
|
value: int | list[int],
|
|
|
|
use_call: str,
|
2021-09-20 12:59:30 +00:00
|
|
|
) -> ModbusResponse | None:
|
2021-05-15 17:54:17 +00:00
|
|
|
"""Convert async to sync pymodbus call."""
|
2021-05-08 11:28:35 +00:00
|
|
|
if self._config_delay:
|
|
|
|
return None
|
2021-05-15 17:54:17 +00:00
|
|
|
async with self._lock:
|
2021-08-28 21:04:33 +00:00
|
|
|
if not self._client:
|
|
|
|
return None
|
2021-06-05 12:41:32 +00:00
|
|
|
result = await self.hass.async_add_executor_job(
|
2021-05-17 09:20:12 +00:00
|
|
|
self._pymodbus_call, unit, address, value, use_call
|
2021-05-15 17:54:17 +00:00
|
|
|
)
|
2021-08-08 04:10:08 +00:00
|
|
|
if self._msg_wait:
|
2021-06-05 12:41:32 +00:00
|
|
|
# small delay until next request/response
|
2021-08-08 04:10:08 +00:00
|
|
|
await asyncio.sleep(self._msg_wait)
|
2021-06-05 12:41:32 +00:00
|
|
|
return result
|