mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Bring modbus naming in sync with standard (#99285)
This commit is contained in:
parent
fead9d3a92
commit
71207e112e
@ -168,11 +168,12 @@ async def async_modbus_setup(
|
|||||||
|
|
||||||
async def async_write_register(service: ServiceCall) -> None:
|
async def async_write_register(service: ServiceCall) -> None:
|
||||||
"""Write Modbus registers."""
|
"""Write Modbus registers."""
|
||||||
unit = 0
|
slave = 0
|
||||||
if ATTR_UNIT in service.data:
|
if ATTR_UNIT in service.data:
|
||||||
unit = int(float(service.data[ATTR_UNIT]))
|
slave = int(float(service.data[ATTR_UNIT]))
|
||||||
|
|
||||||
if ATTR_SLAVE in service.data:
|
if ATTR_SLAVE in service.data:
|
||||||
unit = int(float(service.data[ATTR_SLAVE]))
|
slave = int(float(service.data[ATTR_SLAVE]))
|
||||||
address = int(float(service.data[ATTR_ADDRESS]))
|
address = int(float(service.data[ATTR_ADDRESS]))
|
||||||
value = service.data[ATTR_VALUE]
|
value = service.data[ATTR_VALUE]
|
||||||
hub = hub_collect[
|
hub = hub_collect[
|
||||||
@ -180,29 +181,32 @@ async def async_modbus_setup(
|
|||||||
]
|
]
|
||||||
if isinstance(value, list):
|
if isinstance(value, list):
|
||||||
await hub.async_pb_call(
|
await hub.async_pb_call(
|
||||||
unit, address, [int(float(i)) for i in value], CALL_TYPE_WRITE_REGISTERS
|
slave,
|
||||||
|
address,
|
||||||
|
[int(float(i)) for i in value],
|
||||||
|
CALL_TYPE_WRITE_REGISTERS,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await hub.async_pb_call(
|
await hub.async_pb_call(
|
||||||
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
|
slave, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_write_coil(service: ServiceCall) -> None:
|
async def async_write_coil(service: ServiceCall) -> None:
|
||||||
"""Write Modbus coil."""
|
"""Write Modbus coil."""
|
||||||
unit = 0
|
slave = 0
|
||||||
if ATTR_UNIT in service.data:
|
if ATTR_UNIT in service.data:
|
||||||
unit = int(float(service.data[ATTR_UNIT]))
|
slave = int(float(service.data[ATTR_UNIT]))
|
||||||
if ATTR_SLAVE in service.data:
|
if ATTR_SLAVE in service.data:
|
||||||
unit = int(float(service.data[ATTR_SLAVE]))
|
slave = int(float(service.data[ATTR_SLAVE]))
|
||||||
address = service.data[ATTR_ADDRESS]
|
address = service.data[ATTR_ADDRESS]
|
||||||
state = service.data[ATTR_STATE]
|
state = service.data[ATTR_STATE]
|
||||||
hub = hub_collect[
|
hub = hub_collect[
|
||||||
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_pb_call(unit, address, state, CALL_TYPE_WRITE_COILS)
|
await hub.async_pb_call(slave, address, state, CALL_TYPE_WRITE_COILS)
|
||||||
else:
|
else:
|
||||||
await hub.async_pb_call(unit, address, state, CALL_TYPE_WRITE_COIL)
|
await hub.async_pb_call(slave, 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),
|
||||||
@ -405,10 +409,10 @@ class ModbusHub:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def pb_call(
|
def pb_call(
|
||||||
self, unit: int | None, address: int, value: int | list[int], use_call: str
|
self, slave: 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": slave} if slave else {}
|
||||||
entry = self._pb_request[use_call]
|
entry = self._pb_request[use_call]
|
||||||
try:
|
try:
|
||||||
result: ModbusResponse = entry.func(address, value, **kwargs)
|
result: ModbusResponse = entry.func(address, value, **kwargs)
|
||||||
|
@ -566,17 +566,17 @@ SERVICE = "service"
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"do_unit",
|
"do_slave",
|
||||||
[
|
[
|
||||||
ATTR_UNIT,
|
|
||||||
ATTR_SLAVE,
|
ATTR_SLAVE,
|
||||||
|
ATTR_UNIT,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_pb_service_write(
|
async def test_pb_service_write(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
do_write,
|
do_write,
|
||||||
do_return,
|
do_return,
|
||||||
do_unit,
|
do_slave,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
mock_modbus_with_pymodbus,
|
mock_modbus_with_pymodbus,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -591,7 +591,7 @@ async def test_pb_service_write(
|
|||||||
|
|
||||||
data = {
|
data = {
|
||||||
ATTR_HUB: TEST_MODBUS_NAME,
|
ATTR_HUB: TEST_MODBUS_NAME,
|
||||||
do_unit: 17,
|
do_slave: 17,
|
||||||
ATTR_ADDRESS: 16,
|
ATTR_ADDRESS: 16,
|
||||||
do_write[DATA]: do_write[VALUE],
|
do_write[DATA]: do_write[VALUE],
|
||||||
}
|
}
|
||||||
@ -932,7 +932,7 @@ async def test_write_no_client(hass: HomeAssistant, mock_modbus) -> None:
|
|||||||
|
|
||||||
data = {
|
data = {
|
||||||
ATTR_HUB: TEST_MODBUS_NAME,
|
ATTR_HUB: TEST_MODBUS_NAME,
|
||||||
ATTR_UNIT: 17,
|
ATTR_SLAVE: 17,
|
||||||
ATTR_ADDRESS: 16,
|
ATTR_ADDRESS: 16,
|
||||||
ATTR_STATE: True,
|
ATTR_STATE: True,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user