Skip to content

Commit

Permalink
fix: Some if statements from 'not' to 'is not None'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 12, 2024
1 parent 800cd90 commit 88c5593
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
6 changes: 3 additions & 3 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async def try_read_write(self, code, start, arg, message, incremental_wait):
while attempts_left > 0 and response is None:
attempts_left -= 1
try:
if (response := await self.read_write(code, start, arg)) and (length := ilen(response)) and (expected := arg if code < CODE.WRITE_SINGLE_COIL else 1) and length != expected:
if (response := await self.read_write(code, start, arg)) and (length := ilen(response)) is None and (expected := arg if code < CODE.WRITE_SINGLE_COIL else 1) and length != expected:
raise Exception(f"[{self.config.serial}] Unexpected response: Invalid length! (Length: {length}, Expected: {expected})")

_LOGGER.debug(f"[{self.config.serial}] {message} succeeded, response: {response}")
Expand All @@ -219,7 +219,7 @@ async def try_read_write(self, code, start, arg, message, incremental_wait):
return response

async def get(self, runtime = 0, requests = None):
scheduled, scheduled_count = ensure_list_safe_len(self.profile.parser.schedule_requests(runtime) if not requests else requests)
scheduled, scheduled_count = ensure_list_safe_len(self.profile.parser.schedule_requests(runtime) if requests is None else requests)
responses, result = {}, {}

_LOGGER.debug(f"[{self.config.serial}] Scheduling {scheduled_count} query request{'' if scheduled_count == 1 else 's'}. ^{runtime}")
Expand All @@ -232,7 +232,7 @@ async def get(self, runtime = 0, requests = None):
quantity = end - start + 1
responses[(code, start)] = await self.try_read_write(code, start, quantity, f"Querying {code:02X} ~ {start:04} - {end:04} | 0x{start:04X} - 0x{end:04X} #{quantity:03}", True)

result = self.profile.parser.process(responses) if not requests else responses
result = self.profile.parser.process(responses) if requests is None else responses

if (rc := len(result) if result else 0) > 0:
_LOGGER.debug(f"[{self.config.serial}] Returning {rc} new values to the Coordinator. [Previous State: {self.state.print} ({self.state.value})]")
Expand Down
4 changes: 1 addition & 3 deletions custom_components/solarman/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def available(self) -> bool:

@property
def is_on(self) -> bool | None:
if not self._attr_state:
return None
return self._attr_state > -1
return self._attr_state > -1 if self._attr_state is not None else False

def update(self):
self.set_state(self.coordinator.inverter.state.value)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/solarman/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def do_validate(self, key, value, rule):
return True

def process(self, data):
if data:
if data is not None:
for i in self._items:
if not (self.is_valid(i) and self.is_enabled(i)):
continue
Expand Down
48 changes: 13 additions & 35 deletions custom_components/solarman/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,18 @@ def get_device(device_id) -> Inverter:
if config_entry in hass.data[DOMAIN] and isinstance(hass.data[DOMAIN][config_entry], InverterCoordinator):
return hass.data[DOMAIN][config_entry].inverter

return None
raise ServiceValidationError("No communication interface for device found", translation_domain = DOMAIN, translation_key = "no_interface_found")

async def read_holding_registers(call: ServiceCall) -> int:
_LOGGER.debug(f"read_holding_registers: {call}")

if (inverter := get_device(call.data.get(SERVICES_PARAM_DEVICE))) is None:
raise ServiceValidationError(
"No communication interface for device found",
translation_domain = DOMAIN,
translation_key = "no_interface_found"
)
async def read_input_registers(call: ServiceCall) -> int:
_LOGGER.debug(f"read_input_registers: {call}")

inverter = get_device(call.data.get(SERVICES_PARAM_DEVICE))
register = call.data.get(SERVICES_PARAM_REGISTER)
quantity = call.data.get(SERVICES_PARAM_QUANTITY)
result = {}

try:
if (response := await inverter.call(CODE.READ_HOLDING_REGISTERS, register, quantity)) is not None:
if (response := await inverter.call(CODE.READ_INPUT, register, quantity)) is not None:
for i in range(0, quantity):
result[register + i] = response[i]

Expand All @@ -67,22 +61,16 @@ async def read_holding_registers(call: ServiceCall) -> int:

return result

async def read_input_registers(call: ServiceCall) -> int:
_LOGGER.debug(f"read_input_registers: {call}")

if (inverter := get_device(call.data.get(SERVICES_PARAM_DEVICE))) is None:
raise ServiceValidationError(
"No communication interface for device found",
translation_domain = DOMAIN,
translation_key = "no_interface_found"
)
async def read_holding_registers(call: ServiceCall) -> int:
_LOGGER.debug(f"read_holding_registers: {call}")

inverter = get_device(call.data.get(SERVICES_PARAM_DEVICE))
register = call.data.get(SERVICES_PARAM_REGISTER)
quantity = call.data.get(SERVICES_PARAM_QUANTITY)
result = {}

try:
if (response := await inverter.call(CODE.READ_INPUT, register, quantity)) is not None:
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]

Expand All @@ -94,12 +82,7 @@ async def read_input_registers(call: ServiceCall) -> int:
async def write_holding_register(call: ServiceCall) -> None:
_LOGGER.debug(f"write_holding_register: {call}")

if (inverter := get_device(call.data.get(SERVICES_PARAM_DEVICE))) is None:
raise ServiceValidationError(
"No communication interface for device found",
translation_domain = DOMAIN,
translation_key = "no_interface_found",
)
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))
Expand All @@ -109,24 +92,19 @@ async def write_holding_register(call: ServiceCall) -> None:
async def write_multiple_holding_registers(call: ServiceCall) -> None:
_LOGGER.debug(f"write_multiple_holding_registers: {call}")

if (inverter := get_device(call.data.get(SERVICES_PARAM_DEVICE))) is None:
raise ServiceValidationError(
"No communication interface for device found",
translation_domain = DOMAIN,
translation_key = "no_interface_found",
)
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))
except Exception as e:
raise ServiceValidationError(e, translation_domain = DOMAIN, translation_key = "call_failed")

hass.services.async_register(
DOMAIN, SERVICE_READ_HOLDING_REGISTERS, read_holding_registers, schema = vol.Schema(HEADER_SCHEMA | QUANTITY_SCHEMA), supports_response = SupportsResponse.OPTIONAL
DOMAIN, SERVICE_READ_INPUT_REGISTERS, read_input_registers, schema = vol.Schema(HEADER_SCHEMA | QUANTITY_SCHEMA), supports_response = SupportsResponse.OPTIONAL
)

hass.services.async_register(
DOMAIN, SERVICE_READ_INPUT_REGISTERS, read_input_registers, schema = vol.Schema(HEADER_SCHEMA | QUANTITY_SCHEMA), supports_response = SupportsResponse.OPTIONAL
DOMAIN, SERVICE_READ_HOLDING_REGISTERS, read_holding_registers, schema = vol.Schema(HEADER_SCHEMA | QUANTITY_SCHEMA), supports_response = SupportsResponse.OPTIONAL
)

hass.services.async_register(
Expand Down

0 comments on commit 88c5593

Please sign in to comment.