Skip to content

Commit

Permalink
Fixes for black
Browse files Browse the repository at this point in the history
Signed-off-by: Lars Kusch <[email protected]>
  • Loading branch information
LarsK1 committed Jan 12, 2025
1 parent f4d17b1 commit ff306e7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 130 deletions.
16 changes: 4 additions & 12 deletions custom_components/solvis_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):

new_data = {**config_entry.data}
if current_version == 1 and current_minor_version < 3:
_LOGGER.info(
f"Migrating from version {current_version}_{current_minor_version}"
)
_LOGGER.info(f"Migrating from version {current_version}_{current_minor_version}")
if CONF_OPTION_1 not in new_data:
new_data[CONF_OPTION_1] = False
if CONF_OPTION_2 not in new_data:
Expand All @@ -130,18 +128,14 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
new_data[DEVICE_VERSION] = "SC3"
current_minor_version = 3
if current_version == 1 and current_minor_version < 4:
_LOGGER.info(
f"Migrating from version {current_version}_{current_minor_version}"
)
_LOGGER.info(f"Migrating from version {current_version}_{current_minor_version}")
if POLL_RATE_DEFAULT not in new_data:
new_data[POLL_RATE_DEFAULT] = 30
if POLL_RATE_SLOW not in new_data:
new_data[POLL_RATE_SLOW] = 300
current_minor_version = 4
if current_version == 1 and current_minor_version == 4:
_LOGGER.info(
f"Migrating from version {current_version}_{current_minor_version}"
)
_LOGGER.info(f"Migrating from version {current_version}_{current_minor_version}")
current_minor_version = 0
current_version = 2
hass.config_entries.async_update_entry(
Expand All @@ -150,8 +144,6 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
minor_version=current_minor_version,
version=current_version,
)
_LOGGER.info(
f"Migration to version {current_version}_{current_minor_version} successful"
)
_LOGGER.info(f"Migration to version {current_version}_{current_minor_version} successful")

return True
119 changes: 28 additions & 91 deletions custom_components/solvis_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,9 @@ def get_solvis_modules(data: ConfigType) -> Schema:
def get_solvis_devices(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(
DEVICE_VERSION, default=str(SolvisDeviceVersion.SC3)
): SolvisVersionSelect,
vol.Required(POLL_RATE_DEFAULT, default=30): vol.All(
vol.Coerce(int), vol.Range(min=30)
),
vol.Required(POLL_RATE_SLOW, default=300): vol.All(
vol.Coerce(int), vol.Range(min=60)
),
vol.Required(DEVICE_VERSION, default=str(SolvisDeviceVersion.SC3)): SolvisVersionSelect,
vol.Required(POLL_RATE_DEFAULT, default=30): vol.All(vol.Coerce(int), vol.Range(min=30)),
vol.Required(POLL_RATE_SLOW, default=300): vol.All(vol.Coerce(int), vol.Range(min=60)),
},
extra=vol.ALLOW_EXTRA,
)
Expand All @@ -93,34 +87,20 @@ def get_solvis_devices(data: ConfigType) -> Schema:
def get_solvis_modules_options(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(
CONF_OPTION_1, default=data.get(CONF_OPTION_1, False)
): bool, # HKR 2
vol.Required(
CONF_OPTION_2, default=data.get(CONF_OPTION_2, False)
): bool, # HKR 3
vol.Required(
CONF_OPTION_3, default=data.get(CONF_OPTION_3, False)
): bool, # solar collectors
vol.Required(
CONF_OPTION_4, default=data.get(CONF_OPTION_4, False)
): bool, # heat pump
vol.Required(CONF_OPTION_1, default=data.get(CONF_OPTION_1, False)): bool, # HKR 2
vol.Required(CONF_OPTION_2, default=data.get(CONF_OPTION_2, False)): bool, # HKR 3
vol.Required(CONF_OPTION_3, default=data.get(CONF_OPTION_3, False)): bool, # solar collectors
vol.Required(CONF_OPTION_4, default=data.get(CONF_OPTION_4, False)): bool, # heat pump
}
)


def get_solvis_devices_options(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(
DEVICE_VERSION, default=str(SolvisDeviceVersion.SC3)
): SolvisVersionSelect,
vol.Required(POLL_RATE_DEFAULT, default=30): vol.All(
vol.Coerce(int), vol.Range(min=30)
),
vol.Required(POLL_RATE_SLOW, default=300): vol.All(
vol.Coerce(int), vol.Range(min=60)
),
vol.Required(DEVICE_VERSION, default=str(SolvisDeviceVersion.SC3)): SolvisVersionSelect,
vol.Required(POLL_RATE_DEFAULT, default=30): vol.All(vol.Coerce(int), vol.Range(min=30)),
vol.Required(POLL_RATE_SLOW, default=300): vol.All(vol.Coerce(int), vol.Range(min=60)),
},
extra=vol.ALLOW_EXTRA,
)
Expand Down Expand Up @@ -151,9 +131,7 @@ async def async_step_user(self, user_input: ConfigType | None = None) -> FlowRes
if user_input is not None:
self.data = user_input
self._abort_if_unique_id_configured()
modbussocket = ModbusClient.AsyncModbusTcpClient(
host=user_input[CONF_HOST], port=user_input[CONF_PORT]
)
modbussocket = ModbusClient.AsyncModbusTcpClient(host=user_input[CONF_HOST], port=user_input[CONF_PORT])
try:
await modbussocket.connect()
_LOGGER.debug("Connected to Modbus for Solvis")
Expand All @@ -165,6 +143,7 @@ async def async_step_user(self, user_input: ConfigType | None = None) -> FlowRes
data_schema=get_host_schema_config(self.data),
errors=errors,
)

except ModbusException as exc:
errors["base"] = "unknown"
errors["device"] = exc
Expand All @@ -183,33 +162,17 @@ async def async_step_user(self, user_input: ConfigType | None = None) -> FlowRes
)
else:
versionsc = await modbussocket.read_input_registers(32770, 1, 1)
versionsc = str(
BinaryPayloadDecoder.fromRegisters(
versionsc.registers, byteorder=Endian.BIG
).decode_16bit_int()
)
versionsc = str(BinaryPayloadDecoder.fromRegisters(versionsc.registers, byteorder=Endian.BIG).decode_16bit_int())
versionnbg = await modbussocket.read_input_registers(32771, 1, 1)
versionnbg = str(
BinaryPayloadDecoder.fromRegisters(
versionnbg.registers, byteorder=Endian.BIG
).decode_16bit_int()
)
user_input["VERSIONSC"] = (
f"{versionsc[0]}.{versionnbg[1:3]}.{versionsc[3:5]}"
)
user_input["VERSIONNBG"] = (
f"{versionnbg[0]}.{versionnbg[1:3]}.{versionnbg[3:5]}"
)
versionnbg = str(BinaryPayloadDecoder.fromRegisters(versionnbg.registers, byteorder=Endian.BIG).decode_16bit_int())
user_input["VERSIONSC"] = f"{versionsc[0]}.{versionnbg[1:3]}.{versionsc[3:5]}"
user_input["VERSIONNBG"] = f"{versionnbg[0]}.{versionnbg[1:3]}.{versionnbg[3:5]}"
modbussocket.close()
return await self.async_step_device()

return self.async_show_form(
step_id="user", data_schema=get_host_schema_config(self.data), errors=errors
)
return self.async_show_form(step_id="user", data_schema=get_host_schema_config(self.data), errors=errors)

async def async_step_device(
self, user_input: ConfigType | None = None
) -> FlowResult:
async def async_step_device(self, user_input: ConfigType | None = None) -> FlowResult:
"""Handle the device step."""
errors = {}
if user_input is not None:
Expand All @@ -226,14 +189,10 @@ async def async_step_device(
errors=errors,
)

async def async_step_features(
self, user_input: ConfigType | None = None
) -> FlowResult:
async def async_step_features(self, user_input: ConfigType | None = None) -> FlowResult:
"""Handle the feature step."""
if user_input is None:
return self.async_show_form(
step_id="features", data_schema=get_solvis_modules(self.data)
)
return self.async_show_form(step_id="features", data_schema=get_solvis_modules(self.data))
self.data.update(user_input)
return self.async_create_entry(title=self.data[CONF_NAME], data=self.data)

Expand Down Expand Up @@ -262,11 +221,7 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
_LOGGER.debug(f"Options flow values_1: {str(self.data)}", DOMAIN)
if user_input is not None:
self.data.update(user_input)
modbussocket: ModbusClient.AsyncModbusTcpClient = (
ModbusClient.AsyncModbusTcpClient(
host=user_input[CONF_HOST], port=user_input[CONF_PORT]
)
)
modbussocket: ModbusClient.AsyncModbusTcpClient = ModbusClient.AsyncModbusTcpClient(host=user_input[CONF_HOST], port=user_input[CONF_PORT])
try:
await modbussocket.connect()
_LOGGER.debug("Connected to Modbus for Solvis")
Expand Down Expand Up @@ -296,23 +251,11 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
)
else:
versionsc = await modbussocket.read_input_registers(32770, 1, 1)
versionsc = str(
BinaryPayloadDecoder.fromRegisters(
versionsc.registers, byteorder=Endian.BIG
).decode_16bit_int()
)
versionsc = str(BinaryPayloadDecoder.fromRegisters(versionsc.registers, byteorder=Endian.BIG).decode_16bit_int())
versionnbg = await modbussocket.read_input_registers(32771, 1, 1)
versionnbg = str(
BinaryPayloadDecoder.fromRegisters(
versionnbg.registers, byteorder=Endian.BIG
).decode_16bit_int()
)
user_input["VERSIONSC"] = (
f"{versionsc[0]}.{versionnbg[1:3]}.{versionsc[3:5]}"
)
user_input["VERSIONNBG"] = (
f"{versionnbg[0]}.{versionnbg[1:3]}.{versionnbg[3:5]}"
)
versionnbg = str(BinaryPayloadDecoder.fromRegisters(versionnbg.registers, byteorder=Endian.BIG).decode_16bit_int())
user_input["VERSIONSC"] = f"{versionsc[0]}.{versionnbg[1:3]}.{versionsc[3:5]}"
user_input["VERSIONNBG"] = f"{versionnbg[0]}.{versionnbg[1:3]}.{versionnbg[3:5]}"
modbussocket.close()
return await self.async_step_device()

Expand All @@ -322,9 +265,7 @@ async def async_step_init(self, user_input: ConfigType | None = None) -> FlowRes
errors=errors,
)

async def async_step_device(
self, user_input: ConfigType | None = None
) -> FlowResult:
async def async_step_device(self, user_input: ConfigType | None = None) -> FlowResult:
"""Handle the device step."""
errors = {}
_LOGGER.debug(f"Options flow values_1: {str(self.data)}", DOMAIN)
Expand All @@ -342,15 +283,11 @@ async def async_step_device(
errors=errors,
)

async def async_step_features(
self, user_input: ConfigType | None = None
) -> FlowResult:
async def async_step_features(self, user_input: ConfigType | None = None) -> FlowResult:
"""Handle the feature step."""
_LOGGER.debug(f"Options flow values_2: {str(self.data)}", DOMAIN)
if user_input is not None:
self.data.update(user_input)
self.hass.config_entries.async_update_entry(self.config, data=self.data)
return self.async_create_entry(title=self.data[CONF_NAME], data=self.data)
return self.async_show_form(
step_id="features", data_schema=get_solvis_modules_options(self.data)
)
return self.async_show_form(step_id="features", data_schema=get_solvis_modules_options(self.data))
36 changes: 9 additions & 27 deletions custom_components/solvis_control/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ async def _async_update_data(self):

try:
await self.modbus.connect()
_LOGGER.debug(
"Connected to Modbus for Solvis"
) # Moved here for better context
_LOGGER.debug("Connected to Modbus for Solvis") # Moved here for better context

for register in REGISTERS:
if not self.option_hkr2 and register.conf_option == 1:
Expand All @@ -84,9 +82,7 @@ async def _async_update_data(self):
if register.poll_rate:
if register.poll_time > 0:
register.poll_time -= self.poll_rate_default
_LOGGER.debug(
f"Skipping entity {register.name}/{register.address} due to slow poll rate. Remaining time: {register.poll_time}s"
)
_LOGGER.debug(f"Skipping entity {register.name}/{register.address} due to slow poll rate. Remaining time: {register.poll_time}s")
continue
if register.poll_time <= 0:
register.poll_time = self.poll_rate_slow
Expand All @@ -99,41 +95,27 @@ async def _async_update_data(self):
try:
if register.register == 1:

result = await self.modbus.read_input_registers(
register.address, 1, 1
)
result = await self.modbus.read_input_registers(register.address, 1, 1)
_LOGGER.debug(f"Reading input register {register.name}")
else:
result = await self.modbus.read_holding_registers(
register.address, 1, 1
)
_LOGGER.debug(
f"Reading holding register {register.name}/{register.address}"
)
result = await self.modbus.read_holding_registers(register.address, 1, 1)
_LOGGER.debug(f"Reading holding register {register.name}/{register.address}")
if type(result) is not pymodbus.ExceptionResponse:
decoder = BinaryPayloadDecoder.fromRegisters(
result.registers, byteorder=Endian.BIG
)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.BIG)
try:
rawvalue = decoder.decode_16bit_int()
_LOGGER.debug(f"Decoded raw value: {rawvalue}")
value = round(rawvalue * register.multiplier, 2)
except struct.error:
parsed_data[register.name] = -300
else:
parsed_data[register.name] = (
abs(value) if register.absolute_value else value
)
parsed_data[register.name] = abs(value) if register.absolute_value else value
else:
_LOGGER.error(
f"Modbus error reading register {register.name}: {result}"
)
_LOGGER.error(f"Modbus error reading register {register.name}: {result}")
continue

except ModbusException as error:
_LOGGER.error(
f"Modbus error reading register {register.name}: {error}"
)
_LOGGER.error(f"Modbus error reading register {register.name}: {error}")

except ConnectionException:
_LOGGER.warning("Couldn't connect to Solvis device")
Expand Down

0 comments on commit ff306e7

Please sign in to comment.