diff --git a/custom_components/solarman/services.py b/custom_components/solarman/services.py index 940911c..42f4e3c 100644 --- a/custom_components/solarman/services.py +++ b/custom_components/solarman/services.py @@ -55,18 +55,16 @@ async def read_holding_registers(call: ServiceCall) -> int: register = call.data.get(SERVICES_PARAM_REGISTER) quantity = call.data.get(SERVICES_PARAM_QUANTITY) + result = {} try: - response = await inverter.call(CODE.READ_HOLDING_REGISTERS, register, quantity) + if (response := await inverter.call(CODE.READ_HOLDING_REGISTERS, register, quantity)) is not None: + for i in range(0, quantity): + result[register + i] = response[i] + except Exception as e: raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed") - result = {} - - if response: - for i in range(0, quantity): - result[register + i] = response[i] - return result async def read_input_registers(call: ServiceCall) -> int: @@ -81,18 +79,16 @@ async def read_input_registers(call: ServiceCall) -> int: register = call.data.get(SERVICES_PARAM_REGISTER) quantity = call.data.get(SERVICES_PARAM_QUANTITY) + result = {} try: - response = await inverter.call(CODE.READ_INPUT, register, quantity) + if (response := await inverter.call(CODE.READ_INPUT, register, quantity)) is not None: + for i in range(0, quantity): + result[register + i] = response[i] + except Exception as e: raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed") - result = {} - - if response: - for i in range(0, quantity): - result[register + i] = response[i] - return result async def write_holding_register(call: ServiceCall) -> None: @@ -110,8 +106,6 @@ async def write_holding_register(call: ServiceCall) -> None: except Exception as e: raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed") - return - async def write_multiple_holding_registers(call: ServiceCall) -> None: _LOGGER.debug(f"write_multiple_holding_registers: {call}") @@ -127,8 +121,6 @@ async def write_multiple_holding_registers(call: ServiceCall) -> None: except Exception as e: raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed") - return - hass.services.async_register( DOMAIN, SERVICE_READ_HOLDING_REGISTERS, read_holding_registers, schema = vol.Schema(HEADER_SCHEMA | QUANTITY_SCHEMA), supports_response = SupportsResponse.OPTIONAL )