diff --git a/CHANGELOG.md b/CHANGELOG.md index d99217a5..17307485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,11 @@ ## v1.0.x +* Changed: Fix issue on first driver startup, when no device setting in dbus exists by @mr-manuel + + +## v1.0.20240102beta + * Added: Bluetooth: Show signal strength of BMS in log by @mr-manuel * Added: Configure logging level in `config.ini` by @mr-manuel * Added: Create unique identifier, if not provided from BMS by @mr-manuel diff --git a/etc/dbus-serialbattery/battery.py b/etc/dbus-serialbattery/battery.py index 1db9aa0f..aa02a2de 100644 --- a/etc/dbus-serialbattery/battery.py +++ b/etc/dbus-serialbattery/battery.py @@ -493,8 +493,6 @@ def manage_charge_voltage_linear(self) -> None: if self.soc_reset_requested: # logger.info("set soc_reset_requested to False") self.soc_reset_requested = False - # IDEA: Save "soc_reset_last_reached" in the dbus path com.victronenergy.settings - # to make it restart persistent self.soc_reset_last_reached = current_time if self.control_voltage: # check if battery changed from bulk/absoprtion to float diff --git a/etc/dbus-serialbattery/dbushelper.py b/etc/dbus-serialbattery/dbushelper.py index 0636c7e1..2a06aa19 100644 --- a/etc/dbus-serialbattery/dbushelper.py +++ b/etc/dbus-serialbattery/dbushelper.py @@ -126,126 +126,130 @@ def setup_instance(self): # } # loop through devices in dbus settings - for key, value in settings_from_dbus["Settings"]["Devices"].items(): - # check if it's a serialbattery - if "serialbattery" in key: - # check used device instances - if "ClassAndVrmInstance" in value: - device_instances_used.append( - value["ClassAndVrmInstance"][ - value["ClassAndVrmInstance"].rfind(":") + 1 : - ] - ) - - # check the unique identifier, if the battery was already connected once - # if so, get the last saved data - if ( - "UniqueIdentifier" in value - and value["UniqueIdentifier"] == self.bms_id - ): - # set found_bms to true - found_bms = True - - # get the instance from the object name - device_instance = int( - value["ClassAndVrmInstance"][ - value["ClassAndVrmInstance"].rfind(":") + 1 : - ] - ) - logger.info( - f"Found existing battery with DeviceInstance = {device_instance}" - ) + if ( + "Settings" in settings_from_dbus + and "Devices" in settings_from_dbus["Settings"] + ): + for key, value in settings_from_dbus["Settings"]["Devices"].items(): + # check if it's a serialbattery + if "serialbattery" in key: + # check used device instances + if "ClassAndVrmInstance" in value: + device_instances_used.append( + value["ClassAndVrmInstance"][ + value["ClassAndVrmInstance"].rfind(":") + 1 : + ] + ) - if "AllowMaxVoltage" in value and isinstance( - value["AllowMaxVoltage"], int + # check the unique identifier, if the battery was already connected once + # if so, get the last saved data + if ( + "UniqueIdentifier" in value + and value["UniqueIdentifier"] == self.bms_id ): - self.battery.allow_max_voltage = ( - True if value["AllowMaxVoltage"] == 1 else False + # set found_bms to true + found_bms = True + + # get the instance from the object name + device_instance = int( + value["ClassAndVrmInstance"][ + value["ClassAndVrmInstance"].rfind(":") + 1 : + ] + ) + logger.info( + f"Found existing battery with DeviceInstance = {device_instance}" ) - self.battery.max_voltage_start_time = None - # check if the battery has a custom name - if "CustomName" in value and value["CustomName"] != "": - custom_name = value["CustomName"] + if "AllowMaxVoltage" in value and isinstance( + value["AllowMaxVoltage"], int + ): + self.battery.allow_max_voltage = ( + True if value["AllowMaxVoltage"] == 1 else False + ) + self.battery.max_voltage_start_time = None - if "MaxVoltageStartTime" in value and isinstance( - value["MaxVoltageStartTime"], int - ): - self.battery.max_voltage_start_time = int( - value["MaxVoltageStartTime"] - ) + # check if the battery has a custom name + if "CustomName" in value and value["CustomName"] != "": + custom_name = value["CustomName"] - # load SOC from dbus only if SOC_CALCULATION is enabled - if utils.SOC_CALCULATION: - if "SocCalc" in value: - self.battery.soc_calc = float(value["SocCalc"]) - logger.info( - f"Soc_calc read from dbus: {self.battery.soc_calc}" + if "MaxVoltageStartTime" in value and isinstance( + value["MaxVoltageStartTime"], int + ): + self.battery.max_voltage_start_time = int( + value["MaxVoltageStartTime"] ) - else: - logger.info("Soc_calc not found in dbus") - if "SocResetLastReached" in value and isinstance( - value["SocResetLastReached"], int - ): - self.battery.soc_reset_last_reached = int( - value["SocResetLastReached"] - ) + # load SOC from dbus only if SOC_CALCULATION is enabled + if utils.SOC_CALCULATION: + if "SocCalc" in value: + self.battery.soc_calc = float(value["SocCalc"]) + logger.info( + f"Soc_calc read from dbus: {self.battery.soc_calc}" + ) + else: + logger.info("Soc_calc not found in dbus") + + if "SocResetLastReached" in value and isinstance( + value["SocResetLastReached"], int + ): + self.battery.soc_reset_last_reached = int( + value["SocResetLastReached"] + ) - # check the last seen time and remove the battery it it was not seen for 30 days - elif "LastSeen" in value and int(value["LastSeen"]) < int(time()) - ( - 60 * 60 * 24 * 30 - ): - # remove entry - del_return = self.removeSetting( - get_bus(), - "com.victronenergy.settings", - "/Settings/Devices/" + key, - [ - "AllowMaxVoltage", - "ClassAndVrmInstance", - "CustomName", - "LastSeen", - "MaxVoltageStartTime", - "SocCalc", - "SocResetLastReached", - "UniqueIdentifier", - ], - ) - logger.info( - f"Remove /Settings/Devices/{key} from dbus. Delete result: {del_return}" - ) + # check the last seen time and remove the battery it it was not seen for 30 days + elif "LastSeen" in value and int(value["LastSeen"]) < int( + time() + ) - (60 * 60 * 24 * 30): + # remove entry + del_return = self.removeSetting( + get_bus(), + "com.victronenergy.settings", + "/Settings/Devices/" + key, + [ + "AllowMaxVoltage", + "ClassAndVrmInstance", + "CustomName", + "LastSeen", + "MaxVoltageStartTime", + "SocCalc", + "SocResetLastReached", + "UniqueIdentifier", + ], + ) + logger.info( + f"Remove /Settings/Devices/{key} from dbus. Delete result: {del_return}" + ) - # check if the battery has a last seen time, if not then it's an old entry and can be removed - elif "LastSeen" not in value: - del_return = self.removeSetting( - get_bus(), - "com.victronenergy.settings", - "/Settings/Devices/" + key, - ["ClassAndVrmInstance"], - ) - logger.info( - f"Remove /Settings/Devices/{key} from dbus. " - + f"Old entry. Delete result: {del_return}" - ) + # check if the battery has a last seen time, if not then it's an old entry and can be removed + elif "LastSeen" not in value: + del_return = self.removeSetting( + get_bus(), + "com.victronenergy.settings", + "/Settings/Devices/" + key, + ["ClassAndVrmInstance"], + ) + logger.info( + f"Remove /Settings/Devices/{key} from dbus. " + + f"Old entry. Delete result: {del_return}" + ) - if "ruuvi" in key: - # check if Ruuvi tag is enabled, if not remove entry. - if ( - "Enabled" in value - and value["Enabled"] == "0" - and "ClassAndVrmInstance" not in value - ): - del_return = self.removeSetting( - get_bus(), - "com.victronenergy.settings", - "/Settings/Devices/" + key, - ["CustomName", "Enabled", "TemperatureType"], - ) - logger.info( - f"Remove /Settings/Devices/{key} from dbus. " - + f"Ruuvi tag was disabled and had no ClassAndVrmInstance. Delete result: {del_return}" - ) + if "ruuvi" in key: + # check if Ruuvi tag is enabled, if not remove entry. + if ( + "Enabled" in value + and value["Enabled"] == "0" + and "ClassAndVrmInstance" not in value + ): + del_return = self.removeSetting( + get_bus(), + "com.victronenergy.settings", + "/Settings/Devices/" + key, + ["CustomName", "Enabled", "TemperatureType"], + ) + logger.info( + f"Remove /Settings/Devices/{key} from dbus. " + + f"Ruuvi tag was disabled and had no ClassAndVrmInstance. Delete result: {del_return}" + ) logger.debug("setup_instance(): for loop ended") diff --git a/etc/dbus-serialbattery/utils.py b/etc/dbus-serialbattery/utils.py index 97fa9e08..8526ec98 100644 --- a/etc/dbus-serialbattery/utils.py +++ b/etc/dbus-serialbattery/utils.py @@ -37,7 +37,7 @@ def _get_list_from_config( # Constants -DRIVER_VERSION = "1.1.20231224dev" +DRIVER_VERSION = "1.1.20240106dev" zero_char = chr(48) degree_sign = "\N{DEGREE SIGN}"