modbus: use pb not pymodbus consistently as name. (#97780)

Use pb not pymodbus consistently as name.
This commit is contained in:
jan iversen 2023-08-04 20:14:32 +02:00 committed by GitHub
parent b286da211a
commit bbc34bae87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 35 deletions

View File

@ -73,10 +73,6 @@ class BasePlatform(Entity):
def __init__(self, hub: ModbusHub, entry: dict[str, Any]) -> None: def __init__(self, hub: ModbusHub, entry: dict[str, Any]) -> None:
"""Initialize the Modbus binary sensor.""" """Initialize the Modbus binary sensor."""
self._hub = hub self._hub = hub
# temporary fix,
# make sure slave is always defined to avoid an error in pymodbus
# attr(in_waiting) not defined.
# see issue #657 and PR #660 in riptideio/pymodbus
self._slave = entry.get(CONF_SLAVE, 0) self._slave = entry.get(CONF_SLAVE, 0)
self._address = int(entry[CONF_ADDRESS]) self._address = int(entry[CONF_ADDRESS])
self._input_type = entry[CONF_INPUT_TYPE] self._input_type = entry[CONF_INPUT_TYPE]
@ -287,7 +283,7 @@ class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
async def async_turn(self, command: int) -> None: async def async_turn(self, command: int) -> None:
"""Evaluate switch result.""" """Evaluate switch result."""
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._address, command, self._write_type self._slave, self._address, command, self._write_type
) )
if result is None: if result is None:
@ -323,7 +319,7 @@ class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
if self._call_active: if self._call_active:
return return
self._call_active = True self._call_active = True
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._verify_address, 1, self._verify_type self._slave, self._verify_address, 1, self._verify_type
) )
self._call_active = False self._call_active = False

View File

@ -97,7 +97,7 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
if self._call_active: if self._call_active:
return return
self._call_active = True self._call_active = True
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._address, self._count, self._input_type self._slave, self._address, self._count, self._input_type
) )
self._call_active = False self._call_active = False

View File

@ -155,14 +155,14 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
if self._hvac_onoff_register is not None: if self._hvac_onoff_register is not None:
# Turn HVAC Off by writing 0 to the On/Off register, or 1 otherwise. # Turn HVAC Off by writing 0 to the On/Off register, or 1 otherwise.
if self._hvac_onoff_write_registers: if self._hvac_onoff_write_registers:
await self._hub.async_pymodbus_call( await self._hub.async_pb_call(
self._slave, self._slave,
self._hvac_onoff_register, self._hvac_onoff_register,
[0 if hvac_mode == HVACMode.OFF else 1], [0 if hvac_mode == HVACMode.OFF else 1],
CALL_TYPE_WRITE_REGISTERS, CALL_TYPE_WRITE_REGISTERS,
) )
else: else:
await self._hub.async_pymodbus_call( await self._hub.async_pb_call(
self._slave, self._slave,
self._hvac_onoff_register, self._hvac_onoff_register,
0 if hvac_mode == HVACMode.OFF else 1, 0 if hvac_mode == HVACMode.OFF else 1,
@ -174,14 +174,14 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
for value, mode in self._hvac_mode_mapping: for value, mode in self._hvac_mode_mapping:
if mode == hvac_mode: if mode == hvac_mode:
if self._hvac_mode_write_registers: if self._hvac_mode_write_registers:
await self._hub.async_pymodbus_call( await self._hub.async_pb_call(
self._slave, self._slave,
self._hvac_mode_register, self._hvac_mode_register,
[value], [value],
CALL_TYPE_WRITE_REGISTERS, CALL_TYPE_WRITE_REGISTERS,
) )
else: else:
await self._hub.async_pymodbus_call( await self._hub.async_pb_call(
self._slave, self._slave,
self._hvac_mode_register, self._hvac_mode_register,
value, value,
@ -217,21 +217,21 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
DataType.UINT16, DataType.UINT16,
): ):
if self._target_temperature_write_registers: if self._target_temperature_write_registers:
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._slave,
self._target_temperature_register, self._target_temperature_register,
[int(float(registers[0]))], [int(float(registers[0]))],
CALL_TYPE_WRITE_REGISTERS, CALL_TYPE_WRITE_REGISTERS,
) )
else: else:
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._slave,
self._target_temperature_register, self._target_temperature_register,
int(float(registers[0])), int(float(registers[0])),
CALL_TYPE_WRITE_REGISTER, CALL_TYPE_WRITE_REGISTER,
) )
else: else:
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._slave,
self._target_temperature_register, self._target_temperature_register,
[int(float(i)) for i in registers], [int(float(i)) for i in registers],
@ -287,7 +287,7 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self, register_type: str, register: int, raw: bool | None = False self, register_type: str, register: int, raw: bool | None = False
) -> float | None: ) -> float | None:
"""Read register using the Modbus hub slave.""" """Read register using the Modbus hub slave."""
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, register, self._count, register_type self._slave, register, self._count, register_type
) )
if result is None: if result is None:

View File

@ -120,7 +120,7 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
async def async_open_cover(self, **kwargs: Any) -> None: async def async_open_cover(self, **kwargs: Any) -> None:
"""Open cover.""" """Open cover."""
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._write_address, self._state_open, self._write_type self._slave, self._write_address, self._state_open, self._write_type
) )
self._attr_available = result is not None self._attr_available = result is not None
@ -128,7 +128,7 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
async def async_close_cover(self, **kwargs: Any) -> None: async def async_close_cover(self, **kwargs: Any) -> None:
"""Close cover.""" """Close cover."""
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._write_address, self._state_closed, self._write_type self._slave, self._write_address, self._state_closed, self._write_type
) )
self._attr_available = result is not None self._attr_available = result is not None
@ -142,7 +142,7 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
if self._call_active: if self._call_active:
return return
self._call_active = True self._call_active = True
result = await self._hub.async_pymodbus_call( result = await self._hub.async_pb_call(
self._slave, self._address, 1, self._input_type self._slave, self._address, 1, self._input_type
) )
self._call_active = False self._call_active = False

View File

@ -79,7 +79,7 @@ _LOGGER = logging.getLogger(__name__)
ConfEntry = namedtuple("ConfEntry", "call_type attr func_name") ConfEntry = namedtuple("ConfEntry", "call_type attr func_name")
RunEntry = namedtuple("RunEntry", "attr func") RunEntry = namedtuple("RunEntry", "attr func")
PYMODBUS_CALL = [ PB_CALL = [
ConfEntry( ConfEntry(
CALL_TYPE_COIL, CALL_TYPE_COIL,
"bits", "bits",
@ -178,11 +178,11 @@ async def async_modbus_setup(
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
] ]
if isinstance(value, list): if isinstance(value, list):
await hub.async_pymodbus_call( await hub.async_pb_call(
unit, address, [int(float(i)) for i in value], CALL_TYPE_WRITE_REGISTERS unit, address, [int(float(i)) for i in value], CALL_TYPE_WRITE_REGISTERS
) )
else: else:
await hub.async_pymodbus_call( await hub.async_pb_call(
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
) )
@ -199,9 +199,9 @@ async def async_modbus_setup(
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
] ]
if isinstance(state, list): if isinstance(state, list):
await hub.async_pymodbus_call(unit, address, state, CALL_TYPE_WRITE_COILS) await hub.async_pb_call(unit, address, state, CALL_TYPE_WRITE_COILS)
else: else:
await hub.async_pymodbus_call(unit, address, state, CALL_TYPE_WRITE_COIL) await hub.async_pb_call(unit, address, state, CALL_TYPE_WRITE_COIL)
for x_write in ( for x_write in (
(SERVICE_WRITE_REGISTER, async_write_register, ATTR_VALUE, cv.positive_int), (SERVICE_WRITE_REGISTER, async_write_register, ATTR_VALUE, cv.positive_int),
@ -264,7 +264,7 @@ class ModbusHub:
self.name = client_config[CONF_NAME] self.name = client_config[CONF_NAME]
self._config_type = client_config[CONF_TYPE] self._config_type = client_config[CONF_TYPE]
self._config_delay = client_config[CONF_DELAY] self._config_delay = client_config[CONF_DELAY]
self._pb_call: dict[str, RunEntry] = {} self._pb_request: dict[str, RunEntry] = {}
self._pb_class = { self._pb_class = {
SERIAL: ModbusSerialClient, SERIAL: ModbusSerialClient,
TCP: ModbusTcpClient, TCP: ModbusTcpClient,
@ -315,10 +315,10 @@ class ModbusHub:
_LOGGER.error(log_text) _LOGGER.error(log_text)
self._in_error = error_state self._in_error = error_state
async def async_pymodbus_connect(self) -> None: async def async_pb_connect(self) -> None:
"""Connect to device, async.""" """Connect to device, async."""
async with self._lock: async with self._lock:
if not await self.hass.async_add_executor_job(self._pymodbus_connect): if not await self.hass.async_add_executor_job(self.pb_connect):
err = f"{self.name} connect failed, retry in pymodbus" err = f"{self.name} connect failed, retry in pymodbus"
self._log_error(err, error_state=False) self._log_error(err, error_state=False)
@ -330,12 +330,12 @@ class ModbusHub:
self._log_error(str(exception_error), error_state=False) self._log_error(str(exception_error), error_state=False)
return False return False
for entry in PYMODBUS_CALL: for entry in PB_CALL:
func = getattr(self._client, entry.func_name) func = getattr(self._client, entry.func_name)
self._pb_call[entry.call_type] = RunEntry(entry.attr, func) self._pb_request[entry.call_type] = RunEntry(entry.attr, func)
self.hass.async_create_background_task( self.hass.async_create_background_task(
self.async_pymodbus_connect(), "modbus-connect" self.async_pb_connect(), "modbus-connect"
) )
# Start counting down to allow modbus requests. # Start counting down to allow modbus requests.
@ -374,7 +374,7 @@ class ModbusHub:
message = f"modbus {self.name} communication closed" message = f"modbus {self.name} communication closed"
_LOGGER.warning(message) _LOGGER.warning(message)
def _pymodbus_connect(self) -> bool: def pb_connect(self) -> bool:
"""Connect client.""" """Connect client."""
try: try:
self._client.connect() # type: ignore[union-attr] self._client.connect() # type: ignore[union-attr]
@ -386,12 +386,12 @@ class ModbusHub:
_LOGGER.info(message) _LOGGER.info(message)
return True return True
def _pymodbus_call( def pb_call(
self, unit: int | None, address: int, value: int | list[int], use_call: str self, unit: int | None, address: int, value: int | list[int], use_call: str
) -> ModbusResponse | None: ) -> ModbusResponse | None:
"""Call sync. pymodbus.""" """Call sync. pymodbus."""
kwargs = {"slave": unit} if unit else {} kwargs = {"slave": unit} if unit else {}
entry = self._pb_call[use_call] entry = self._pb_request[use_call]
try: try:
result: ModbusResponse = entry.func(address, value, **kwargs) result: ModbusResponse = entry.func(address, value, **kwargs)
except ModbusException as exception_error: except ModbusException as exception_error:
@ -403,7 +403,7 @@ class ModbusHub:
self._in_error = False self._in_error = False
return result return result
async def async_pymodbus_call( async def async_pb_call(
self, self,
unit: int | None, unit: int | None,
address: int, address: int,
@ -417,7 +417,7 @@ class ModbusHub:
if not self._client: if not self._client:
return None return None
result = await self.hass.async_add_executor_job( result = await self.hass.async_add_executor_job(
self._pymodbus_call, unit, address, value, use_call self.pb_call, unit, address, value, use_call
) )
if self._msg_wait: if self._msg_wait:
# small delay until next request/response # small delay until next request/response

View File

@ -101,7 +101,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreSensor, SensorEntity):
"""Update the state of the sensor.""" """Update the state of the sensor."""
# remark "now" is a dummy parameter to avoid problems with # remark "now" is a dummy parameter to avoid problems with
# async_track_time_interval # async_track_time_interval
raw_result = await self._hub.async_pymodbus_call( raw_result = await self._hub.async_pb_call(
self._slave, self._address, self._count, self._input_type self._slave, self._address, self._count, self._input_type
) )
if raw_result is None: if raw_result is None: