Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions homeassistant/components/modbus/base_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ class BasePlatform(Entity):
def __init__(self, hub: ModbusHub, entry: dict[str, Any]) -> None:
"""Initialize the Modbus binary sensor."""
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._address = int(entry[CONF_ADDRESS])
self._input_type = entry[CONF_INPUT_TYPE]
Expand Down Expand Up @@ -287,7 +283,7 @@ async def async_added_to_hass(self) -> None:

async def async_turn(self, command: int) -> None:
"""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
)
if result is None:
Expand Down Expand Up @@ -323,7 +319,7 @@ async def async_update(self, now: datetime | None = None) -> None:
if self._call_active:
return
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._call_active = False
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/modbus/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def async_update(self, now: datetime | None = None) -> None:
if self._call_active:
return
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._call_active = False
Expand Down
16 changes: 8 additions & 8 deletions homeassistant/components/modbus/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
if self._hvac_onoff_register is not None:
# Turn HVAC Off by writing 0 to the On/Off register, or 1 otherwise.
if self._hvac_onoff_write_registers:
await self._hub.async_pymodbus_call(
await self._hub.async_pb_call(
self._slave,
self._hvac_onoff_register,
[0 if hvac_mode == HVACMode.OFF else 1],
CALL_TYPE_WRITE_REGISTERS,
)
else:
await self._hub.async_pymodbus_call(
await self._hub.async_pb_call(
self._slave,
self._hvac_onoff_register,
0 if hvac_mode == HVACMode.OFF else 1,
Expand All @@ -174,14 +174,14 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
for value, mode in self._hvac_mode_mapping:
if mode == hvac_mode:
if self._hvac_mode_write_registers:
await self._hub.async_pymodbus_call(
await self._hub.async_pb_call(
self._slave,
self._hvac_mode_register,
[value],
CALL_TYPE_WRITE_REGISTERS,
)
else:
await self._hub.async_pymodbus_call(
await self._hub.async_pb_call(
self._slave,
self._hvac_mode_register,
value,
Expand Down Expand Up @@ -217,21 +217,21 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
DataType.UINT16,
):
if self._target_temperature_write_registers:
result = await self._hub.async_pymodbus_call(
result = await self._hub.async_pb_call(
self._slave,
self._target_temperature_register,
[int(float(registers[0]))],
CALL_TYPE_WRITE_REGISTERS,
)
else:
result = await self._hub.async_pymodbus_call(
result = await self._hub.async_pb_call(
self._slave,
self._target_temperature_register,
int(float(registers[0])),
CALL_TYPE_WRITE_REGISTER,
)
else:
result = await self._hub.async_pymodbus_call(
result = await self._hub.async_pb_call(
self._slave,
self._target_temperature_register,
[int(float(i)) for i in registers],
Expand Down Expand Up @@ -287,7 +287,7 @@ async def _async_read_register(
self, register_type: str, register: int, raw: bool | None = False
) -> float | None:
"""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
)
if result is None:
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/modbus/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ def _set_attr_state(self, value: str | bool | int) -> None:

async def async_open_cover(self, **kwargs: Any) -> None:
"""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._attr_available = result is not None
await self.async_update()

async def async_close_cover(self, **kwargs: Any) -> None:
"""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._attr_available = result is not None
Expand All @@ -142,7 +142,7 @@ async def async_update(self, now: datetime | None = None) -> None:
if self._call_active:
return
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._call_active = False
Expand Down
32 changes: 16 additions & 16 deletions homeassistant/components/modbus/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

ConfEntry = namedtuple("ConfEntry", "call_type attr func_name")
RunEntry = namedtuple("RunEntry", "attr func")
PYMODBUS_CALL = [
PB_CALL = [
ConfEntry(
CALL_TYPE_COIL,
"bits",
Expand Down Expand Up @@ -178,11 +178,11 @@ async def async_write_register(service: ServiceCall) -> None:
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
]
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
)
else:
await hub.async_pymodbus_call(
await hub.async_pb_call(
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
)

Expand All @@ -199,9 +199,9 @@ async def async_write_coil(service: ServiceCall) -> None:
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
]
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:
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 (
(SERVICE_WRITE_REGISTER, async_write_register, ATTR_VALUE, cv.positive_int),
Expand Down Expand Up @@ -264,7 +264,7 @@ def __init__(self, hass: HomeAssistant, client_config: dict[str, Any]) -> None:
self.name = client_config[CONF_NAME]
self._config_type = client_config[CONF_TYPE]
self._config_delay = client_config[CONF_DELAY]
self._pb_call: dict[str, RunEntry] = {}
self._pb_request: dict[str, RunEntry] = {}
self._pb_class = {
SERIAL: ModbusSerialClient,
TCP: ModbusTcpClient,
Expand Down Expand Up @@ -315,10 +315,10 @@ def _log_error(self, text: str, error_state: bool = True) -> None:
_LOGGER.error(log_text)
self._in_error = error_state

async def async_pymodbus_connect(self) -> None:
async def async_pb_connect(self) -> None:
"""Connect to device, async."""
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"
self._log_error(err, error_state=False)

Expand All @@ -330,12 +330,12 @@ async def async_setup(self) -> bool:
self._log_error(str(exception_error), error_state=False)
return False

for entry in PYMODBUS_CALL:
for entry in PB_CALL:
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.async_pymodbus_connect(), "modbus-connect"
self.async_pb_connect(), "modbus-connect"
)

# Start counting down to allow modbus requests.
Expand Down Expand Up @@ -374,7 +374,7 @@ async def async_close(self) -> None:
message = f"modbus {self.name} communication closed"
_LOGGER.warning(message)

def _pymodbus_connect(self) -> bool:
def pb_connect(self) -> bool:
"""Connect client."""
try:
self._client.connect() # type: ignore[union-attr]
Expand All @@ -386,12 +386,12 @@ def _pymodbus_connect(self) -> bool:
_LOGGER.info(message)
return True

def _pymodbus_call(
def pb_call(
self, unit: int | None, address: int, value: int | list[int], use_call: str
) -> ModbusResponse | None:
"""Call sync. pymodbus."""
kwargs = {"slave": unit} if unit else {}
entry = self._pb_call[use_call]
entry = self._pb_request[use_call]
try:
result: ModbusResponse = entry.func(address, value, **kwargs)
except ModbusException as exception_error:
Expand All @@ -403,7 +403,7 @@ def _pymodbus_call(
self._in_error = False
return result

async def async_pymodbus_call(
async def async_pb_call(
self,
unit: int | None,
address: int,
Expand All @@ -417,7 +417,7 @@ async def async_pymodbus_call(
if not self._client:
return None
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:
# small delay until next request/response
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/modbus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def async_update(self, now: datetime | None = None) -> None:
"""Update the state of the sensor."""
# remark "now" is a dummy parameter to avoid problems with
# 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
)
if raw_result is None:
Expand Down