Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Jan 12, 2025
2 parents 86c9793 + 21aabbe commit f4d17b1
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 225 deletions.
77 changes: 40 additions & 37 deletions custom_components/solvis_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,43 +112,46 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
)
current_version = config_entry.version
current_minor_version = config_entry.minor_version
if config_entry.version > 1:
# This means the user has downgraded from a future version
return False

if config_entry.version == 1:

new_data = {**config_entry.data}
if config_entry.minor_version < 3:
_LOGGER.info(f"Migrating from version {config_entry.version}_{config_entry.minor_version}")
if CONF_OPTION_1 not in new_data:
new_data[CONF_OPTION_1] = False
if CONF_OPTION_2 not in new_data:
new_data[CONF_OPTION_2] = False
if CONF_OPTION_3 not in new_data:
new_data[CONF_OPTION_3] = False
if CONF_OPTION_4 not in new_data:
new_data[CONF_OPTION_4] = False
if DEVICE_VERSION not in new_data:
new_data[DEVICE_VERSION] = "SC3"
current_minor_version = 3
if config_entry.minor_version < 4:
_LOGGER.info(f"Migrating from version {config_entry.version}_{config_entry.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 config_entry.version < 2 and config_entry.minor_version == 4:
_LOGGER.info(f"Migrating from version {config_entry.version}_{config_entry.minor_version}")
current_minor_version = 0
current_version = 2
hass.config_entries.async_update_entry(
config_entry,
data=new_data,
minor_version=current_minor_version,
version=current_version,
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}"
)
if CONF_OPTION_1 not in new_data:
new_data[CONF_OPTION_1] = False
if CONF_OPTION_2 not in new_data:
new_data[CONF_OPTION_2] = False
if CONF_OPTION_3 not in new_data:
new_data[CONF_OPTION_3] = False
if CONF_OPTION_4 not in new_data:
new_data[CONF_OPTION_4] = False
if DEVICE_VERSION not in new_data:
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}"
)
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"Migration to version {current_version}_{current_minor_version} successful")
current_minor_version = 0
current_version = 2
hass.config_entries.async_update_entry(
config_entry,
data=new_data,
minor_version=current_minor_version,
version=current_version,
)
_LOGGER.info(
f"Migration to version {current_version}_{current_minor_version} successful"
)

return True
return True
158 changes: 123 additions & 35 deletions custom_components/solvis_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ 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 @@ -87,20 +93,34 @@ 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 @@ -131,35 +151,65 @@ 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")
except ConnectionException as exc:
errors["base"] = "cannot_connect"
errors["device"] = exc
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,
)
except ModbusException as exc:
errors["base"] = "unknown"
errors["device"] = exc
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,
)
except Exception as exc:
errors["base"] = "unknown"
errors["device"] = exc
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,
)
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 @@ -176,10 +226,14 @@ async def async_step_device(self, user_input: ConfigType | None = None) -> FlowR
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 All @@ -193,8 +247,8 @@ def async_get_options_flow(


class SolvisOptionsFlow(config_entries.OptionsFlow):
VERSION = 1
MINOR_VERSION = 4
VERSION = 2
MINOR_VERSION = 0

def __init__(self, config) -> None:
"""Init the ConfigFlow."""
Expand All @@ -208,29 +262,57 @@ 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")
except ConnectionException as exc:
errors["base"] = "cannot_connect"
errors["device"] = exc
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,
)
except ModbusException as exc:
errors["base"] = "unknown"
errors["device"] = exc
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,
)
except Exception as exc:
errors["base"] = "unknown"
errors["device"] = exc
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,
)
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 @@ -240,7 +322,9 @@ 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 @@ -258,11 +342,15 @@ async def async_step_device(self, user_input: ConfigType | None = None) -> FlowR
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)
)
Loading

0 comments on commit f4d17b1

Please sign in to comment.