From 559c978b63ac7f76c61202d3131456d2c4488763 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Fri, 19 Jul 2024 03:14:13 +0200 Subject: [PATCH] refactor: After merges --- custom_components/solarman/const.py | 8 ++- custom_components/solarman/services.py | 67 ++++++++++-------------- custom_components/solarman/services.yaml | 19 +++---- 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/custom_components/solarman/const.py b/custom_components/solarman/const.py index 35b241c..2856a40 100644 --- a/custom_components/solarman/const.py +++ b/custom_components/solarman/const.py @@ -55,9 +55,13 @@ REQUEST_START = "start" REQUEST_END = "end" +SERVICES_PARAM_DEVICE = "device" SERVICES_PARAM_REGISTER = "register" +SERVICES_PARAM_COUNT = "count" SERVICES_PARAM_VALUE = "value" SERVICES_PARAM_VALUES = "values" -SERVICE_WRITE_REGISTER = "write_holding_register" -SERVICE_WRITE_MULTIPLE_REGISTERS = "write_multiple_holding_registers" +SERVICE_READ_HOLDING_REGISTER = "read_holding_register" +SERVICE_READ_MULTIPLE_HOLDING_REGISTERS = "read_multiple_holding_registers" +SERVICE_WRITE_HOLDING_REGISTER = "write_holding_register" +SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS = "write_multiple_holding_registers" diff --git a/custom_components/solarman/services.py b/custom_components/solarman/services.py index f2c9091..93aa0d6 100644 --- a/custom_components/solarman/services.py +++ b/custom_components/solarman/services.py @@ -14,47 +14,36 @@ _LOGGER = logging.getLogger(__name__) -SERVICE_READ_HOLDING_REGISTER = 'read_holding_register' -SERVICE_READ_MULTIPLE_HOLDING_REGISTERS = 'read_multiple_holding_registers' -SERVICE_WRITE_HOLDING_REGISTER = 'write_holding_register' -SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS = 'write_multiple_holding_registers' -PARAM_DEVICE = 'device' -PARAM_REGISTER = 'register' -PARAM_COUNT = 'count' -PARAM_VALUE = 'value' -PARAM_VALUES = 'values' - # Register the services one can invoke on the inverter. # Apart from this, it also need to be defined in the file # services.yaml for the Home Assistant UI in "Developer Tools" - SERVICE_READ_REGISTER_SCHEMA = vol.Schema( { - vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), - vol.Required(PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)) + vol.Required(SERVICES_PARAM_DEVICE): vol.All(vol.Coerce(str)), + vol.Required(SERVICES_PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)) } ) SERVICE_READ_MULTIPLE_REGISTERS_SCHEMA = vol.Schema( { - vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), - vol.Required(PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)), - vol.Required(PARAM_COUNT): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)) + vol.Required(SERVICES_PARAM_DEVICE): vol.All(vol.Coerce(str)), + vol.Required(SERVICES_PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)), + vol.Required(SERVICES_PARAM_COUNT): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)) } ) -SERVICE_WRITE_REGISTER_SCHEMA = vol.Schema( +SERVICE_WRITE_HOLDING_REGISTER_SCHEMA = vol.Schema( { - vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), + vol.Required(SERVICES_PARAM_DEVICE): vol.All(vol.Coerce(str)), vol.Required(SERVICES_PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)), vol.Required(SERVICES_PARAM_VALUE): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)), } ) -SERVICE_WRITE_MULTIPLE_REGISTERS_SCHEMA = vol.Schema( +SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS_SCHEMA = vol.Schema( { - vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), + vol.Required(SERVICES_PARAM_DEVICE): vol.All(vol.Coerce(str)), vol.Required(SERVICES_PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535)), vol.Required(SERVICES_PARAM_VALUES): vol.All(cv.ensure_list, [vol.All(vol.Coerce(int), vol.Range(min = 0, max = 65535))]), } @@ -72,15 +61,15 @@ def getInverter(device_id): domain = entity_id.partition(".")[0] entity_comp = hass.data.get("entity_components", {}).get(domain) if entity_comp is None: - _LOGGER.info(f'read_holding_register: Component for {entity_id} not loaded') + _LOGGER.info(f"read_holding_register: Component for {entity_id} not loaded") continue if (entity_obj := entity_comp.get_entity(entity_id)) is None: - _LOGGER.info(f'read_holding_register: Entity {entity_id} not found') + _LOGGER.info(f"read_holding_register: Entity {entity_id} not found") continue if (inverter := entity_obj.inverter) is None: - _LOGGER.info(f'read_holding_register: Entity {entity_id} has no inverter') + _LOGGER.info(f"read_holding_register: Entity {entity_id} has no inverter") continue break @@ -88,8 +77,8 @@ def getInverter(device_id): return inverter async def read_holding_register(call: ServiceCall) -> int: - _LOGGER.debug(f'read_holding_register: {call}') - if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: + _LOGGER.debug(f"read_holding_register: {call}") + if (inverter := getInverter(call.data.get(SERVICES_PARAM_DEVICE))) is None: raise ServiceValidationError( "No communication interface for device found", translation_domain = DOMAIN, @@ -97,7 +86,7 @@ async def read_holding_register(call: ServiceCall) -> int: ) try: - response = inverter.service_read_holding_register(register = call.data.get(PARAM_REGISTER)) + response = inverter.service_read_holding_register(register = call.data.get(SERVICES_PARAM_REGISTER)) except Exception as e: raise ServiceValidationError( e, @@ -105,12 +94,12 @@ async def read_holding_register(call: ServiceCall) -> int: translation_key = "call_failed" ) - result = { call.data.get(PARAM_REGISTER): response[0] } + result = { call.data.get(SERVICES_PARAM_REGISTER): response[0] } return result async def read_multiple_holding_registers(call: ServiceCall) -> int: - _LOGGER.debug(f'read_multiple_holding_registers: {call}') - if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: + _LOGGER.debug(f"read_multiple_holding_registers: {call}") + if (inverter := getInverter(call.data.get(SERVICES_PARAM_DEVICE))) is None: raise ServiceValidationError( "No communication interface for device found", translation_domain = DOMAIN, @@ -119,8 +108,8 @@ async def read_multiple_holding_registers(call: ServiceCall) -> int: try: response = inverter.service_read_multiple_holding_registers( - register = call.data.get(PARAM_REGISTER), - count = call.data.get(PARAM_COUNT)) + register = call.data.get(SERVICES_PARAM_REGISTER), + count = call.data.get(SERVICES_PARAM_COUNT)) except Exception as e: raise ServiceValidationError( e, @@ -129,14 +118,14 @@ async def read_multiple_holding_registers(call: ServiceCall) -> int: ) result = {} - register = call.data.get(PARAM_REGISTER) - for i in range(0,call.data.get(PARAM_COUNT)): + register = call.data.get(SERVICES_PARAM_REGISTER) + for i in range(0,call.data.get(SERVICES_PARAM_COUNT)): result[register + i] = response[i] return result async def write_holding_register(call: ServiceCall) -> None: - _LOGGER.debug(f'write_holding_register: {call}') - if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: + _LOGGER.debug(f"write_holding_register: {call}") + if (inverter := getInverter(call.data.get(SERVICES_PARAM_DEVICE))) is None: raise ServiceValidationError( "No communication interface for device found", translation_domain = DOMAIN, @@ -156,8 +145,8 @@ async def write_holding_register(call: ServiceCall) -> None: return async def write_multiple_holding_registers(call: ServiceCall) -> None: - _LOGGER.debug(f'write_multiple_holding_registers: {call}') - if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: + _LOGGER.debug(f"write_multiple_holding_registers: {call}") + if (inverter := getInverter(call.data.get(SERVICES_PARAM_DEVICE))) is None: raise ServiceValidationError( "No communication interface for device found", translation_domain = DOMAIN, @@ -185,11 +174,11 @@ async def write_multiple_holding_registers(call: ServiceCall) -> None: ) hass.services.async_register( - DOMAIN, SERVICE_WRITE_HOLDING_REGISTER, write_holding_register, schema = SERVICE_WRITE_REGISTER_SCHEMA + DOMAIN, SERVICE_WRITE_HOLDING_REGISTER, write_holding_register, schema = SERVICE_WRITE_HOLDING_REGISTER_SCHEMA ) hass.services.async_register( - DOMAIN, SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS, write_multiple_holding_registers, schema = SERVICE_WRITE_MULTIPLE_REGISTERS_SCHEMA + DOMAIN, SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS, write_multiple_holding_registers, schema = SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS_SCHEMA ) return diff --git a/custom_components/solarman/services.yaml b/custom_components/solarman/services.yaml index 9e22bad..43089e2 100644 --- a/custom_components/solarman/services.yaml +++ b/custom_components/solarman/services.yaml @@ -1,12 +1,11 @@ read_holding_register: name: Read Holding Register (Modbus Function Code 3) description: Read a single register value - fields: device: name: Device description: The Device - example: "inverter_roof" + example: "Inverter" required: true selector: device: @@ -26,12 +25,11 @@ read_holding_register: read_multiple_holding_registers: name: Read Multiple Holding Registers (Modbus Function Code 3) description: Read values from multiple consecutive registers at once. - fields: device: name: Device description: The Device - example: "inverter_roof" + example: "Inverter" required: true selector: device: @@ -61,12 +59,11 @@ read_multiple_holding_registers: write_holding_register: name: Write Holding Register (Modbus Function Code 6) description: NOTE USE WITH CARE! (Some devices might not accept Code 6 in this case try to use 'Write Multiple Holding Registers') - fields: device: name: Device description: The Device - example: "inverter_roof" + example: "Inverter" required: true selector: device: @@ -83,23 +80,24 @@ write_holding_register: max: 65535 mode: box value: - name: Values + name: Value description: Value to write example: "1" required: true selector: number: + min: 0 + max: 65535 mode: box write_multiple_holding_registers: name: Write Multiple Holding Registers (Modbus Function Code 16) description: NOTE USE WITH CARE! (Some devices might not accept Code 16 in this case try to use 'Write Holding Register') - fields: device: name: Device description: The Device - example: "inverter_roof" + example: "Inverter" required: true selector: device: @@ -128,6 +126,3 @@ write_multiple_holding_registers: min: 0 max: 65535 mode: box - object: - -