Skip to content

Commit

Permalink
refactor: Rename write register methods in conformity w/ umodbus
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 22, 2024
1 parent 65c4186 commit 8381431
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 21 deletions.
8 changes: 4 additions & 4 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ async def read_write(self, code, start, arg):
return await self.modbus.read_input_registers(start, arg)
case CODE.WRITE_SINGLE_COIL:
return await self.modbus.write_single_coil(start, arg)
case CODE.WRITE_HOLDING_REGISTER:
return await self.modbus.write_holding_register(start, arg)
case CODE.WRITE_SINGLE_REGISTER:
return await self.modbus.write_single_register(start, arg)
case CODE.WRITE_MULTIPLE_COILS:
return await self.modbus.write_multiple_coils(start, ensure_list(arg))
case CODE.WRITE_MULTIPLE_HOLDING_REGISTERS:
return await self.modbus.write_multiple_holding_registers(start, ensure_list(arg))
case CODE.WRITE_MULTIPLE_REGISTERS:
return await self.modbus.write_multiple_registers(start, ensure_list(arg))
case _:
raise Exception(f"[{self.serial}] Used incorrect modbus function code {code}")

Expand Down
12 changes: 7 additions & 5 deletions custom_components/solarman/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import types
import struct

from datetime import timedelta as td

Expand Down Expand Up @@ -112,9 +111,9 @@
CODE.READ_HOLDING_REGISTERS = 3
CODE.READ_INPUT = 4
CODE.WRITE_SINGLE_COIL = 5
CODE.WRITE_HOLDING_REGISTER = 6
CODE.WRITE_SINGLE_REGISTER = 6
CODE.WRITE_MULTIPLE_COILS = 15
CODE.WRITE_MULTIPLE_HOLDING_REGISTERS = 16
CODE.WRITE_MULTIPLE_REGISTERS = 16

SERVICES_PARAM_DEVICE = "device"
SERVICES_PARAM_REGISTER = "register"
Expand All @@ -125,8 +124,11 @@

SERVICE_READ_HOLDING_REGISTERS = "read_holding_registers"
SERVICE_READ_INPUT_REGISTERS = "read_input_registers"
SERVICE_WRITE_HOLDING_REGISTER = "write_holding_register"
SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS = "write_multiple_holding_registers"
SERVICE_WRITE_SINGLE_REGISTER = "write_single_register"
SERVICE_WRITE_MULTIPLE_REGISTERS = "write_multiple_registers"

DEPRECATION_SERVICE_WRITE_SINGLE_REGISTER = "write_holding_register"
DEPRECATION_SERVICE_WRITE_MULTIPLE_REGISTERS = "write_multiple_holding_registers"

DATETIME_FORMAT = "%y/%m/%d %H:%M:%S"
TIME_FORMAT = "%H:%M"
2 changes: 1 addition & 1 deletion custom_components/solarman/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, coordinator, sensor):
if not "control" in sensor:
self._attr_entity_category = EntityCategory.CONFIG

self.code = get_code(sensor, "write", CODE.WRITE_MULTIPLE_HOLDING_REGISTERS)
self.code = get_code(sensor, "write", CODE.WRITE_MULTIPLE_REGISTERS)
self.register = min(self.registers) if len(self.registers) > 0 else None

async def write(self, value, state = None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/solarman/include
24 changes: 16 additions & 8 deletions custom_components/solarman/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ async def read_holding_registers(call: ServiceCall) -> int:

return result

async def write_holding_register(call: ServiceCall) -> None:
_LOGGER.debug(f"write_holding_register: {call}")
async def write_single_register(call: ServiceCall) -> None:
_LOGGER.debug(f"write_single_register: {call}")

inverter = get_device(call.data.get(SERVICES_PARAM_DEVICE))

try:
await inverter.call(CODE.WRITE_HOLDING_REGISTER, call.data.get(SERVICES_PARAM_REGISTER), call.data.get(SERVICES_PARAM_VALUE))
await inverter.call(CODE.WRITE_SINGLE_REGISTER, call.data.get(SERVICES_PARAM_REGISTER), call.data.get(SERVICES_PARAM_VALUE))
except Exception as e:
raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed")

async def write_multiple_holding_registers(call: ServiceCall) -> None:
_LOGGER.debug(f"write_multiple_holding_registers: {call}")
async def write_multiple_registers(call: ServiceCall) -> None:
_LOGGER.debug(f"write_multiple_registers: {call}")

inverter = get_device(call.data.get(SERVICES_PARAM_DEVICE))

try:
await inverter.call(CODE.WRITE_MULTIPLE_HOLDING_REGISTERS, call.data.get(SERVICES_PARAM_REGISTER), call.data.get(SERVICES_PARAM_VALUES))
await inverter.call(CODE.WRITE_MULTIPLE_REGISTERS, call.data.get(SERVICES_PARAM_REGISTER), call.data.get(SERVICES_PARAM_VALUES))
except Exception as e:
raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed")

Expand All @@ -108,9 +108,17 @@ async def write_multiple_holding_registers(call: ServiceCall) -> None:
)

hass.services.async_register(
DOMAIN, SERVICE_WRITE_HOLDING_REGISTER, write_holding_register, schema = vol.Schema(HEADER_SCHEMA | VALUE_SCHEMA)
DOMAIN, SERVICE_WRITE_SINGLE_REGISTER, write_single_register, schema = vol.Schema(HEADER_SCHEMA | VALUE_SCHEMA)
)

hass.services.async_register(
DOMAIN, SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS, write_multiple_holding_registers, schema = vol.Schema(HEADER_SCHEMA | VALUES_SCHEMA)
DOMAIN, SERVICE_WRITE_MULTIPLE_REGISTERS, write_multiple_registers, schema = vol.Schema(HEADER_SCHEMA | VALUES_SCHEMA)
)

hass.services.async_register(
DOMAIN, DEPRECATION_SERVICE_WRITE_SINGLE_REGISTER, write_single_register, schema = vol.Schema(HEADER_SCHEMA | VALUE_SCHEMA)
)

hass.services.async_register(
DOMAIN, DEPRECATION_SERVICE_WRITE_MULTIPLE_REGISTERS, write_multiple_registers, schema = vol.Schema(HEADER_SCHEMA | VALUES_SCHEMA)
)
75 changes: 73 additions & 2 deletions custom_components/solarman/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,79 @@ read_input_registers:
max: 125
mode: box

write_single_register:
name: Write Single Register (Modbus Function Code 6)
description: USE WITH CARE! (Some devices might not accept Code 6 in this case try to use 'Write Multiple Registers')
fields:
device:
name: Device
description: The Device
example: "Inverter"
required: true
selector:
device:
filter:
- integration: solarman
register:
name: Register
description: Modbus register address
example: 16384
required: true
selector:
number:
min: 0
max: 65535
mode: box
value:
name: Value
description: Value to write
example: "1"
required: true
selector:
number:
min: 0
max: 65535
mode: box

write_multiple_registers:
name: Write Multiple Registers (Modbus Function Code 16)
description: USE WITH CARE! (Some devices might not accept Code 16 in this case try to use 'Write Single Register')
fields:
device:
name: Device
description: The Device
example: "Inverter"
required: true
selector:
device:
filter:
- integration: solarman
register:
name: Register
description: Modbus register address
example: 16384
required: true
selector:
number:
min: 0
max: 65535
mode: box
values:
name: Values
description: Values to write
example: |
- 1
- 2
- 3
required: true
selector:
number:
min: 0
max: 65535
mode: box

write_holding_register:
name: Write Holding Register (Modbus Function Code 6)
name: DEPRECATED! Write Holding Register (Modbus Function Code 6)
description: USE WITH CARE! (Some devices might not accept Code 6 in this case try to use 'Write Multiple Holding Registers')
fields:
device:
Expand Down Expand Up @@ -101,7 +172,7 @@ write_holding_register:
mode: box

write_multiple_holding_registers:
name: Write Multiple Holding Registers (Modbus Function Code 16)
name: DEPRECATED! Write Multiple Holding Registers (Modbus Function Code 16)
description: USE WITH CARE! (Some devices might not accept Code 16 in this case try to use 'Write Holding Register')
fields:
device:
Expand Down

0 comments on commit 8381431

Please sign in to comment.