Skip to content

Commit

Permalink
Changed: Fix issue loading settings from dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Jan 6, 2024
1 parent 9779b2e commit c80d8c5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
222 changes: 113 additions & 109 deletions etc/dbus-serialbattery/dbushelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion etc/dbus-serialbattery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down

0 comments on commit c80d8c5

Please sign in to comment.