-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure CustomChargingHook has an initial value.
- Loading branch information
Mark Hale
committed
Sep 28, 2024
1 parent
fe358d6
commit abdbeda
Showing
5 changed files
with
125 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from typing import Dict, List | ||
|
||
class DataMerger: | ||
def __init__(self, config, default_config_paths: List[str], service_name_resolver): | ||
if isinstance(config, list): | ||
# convert short-hand format | ||
expanded_config = {service_name_resolver.resolve_service_name(serviceName): default_config_paths for serviceName in config} | ||
elif isinstance(config, dict): | ||
expanded_config = {} | ||
for k, v in config.items(): | ||
if not v: | ||
v = default_config_paths | ||
expanded_config[service_name_resolver.resolve_service_name(k)] = v | ||
elif config is None: | ||
expanded_config = {} | ||
else: | ||
raise ValueError(f"Unsupported config object: {type(config)}") | ||
|
||
self.service_names: List[str] = list(expanded_config) | ||
|
||
self.data_by_path: Dict[str, Dict[str, str]] = {} | ||
for service_name, path_list in expanded_config.items(): | ||
for p in path_list: | ||
path_values = self.data_by_path.get(p) | ||
if path_values is None: | ||
path_values = {} | ||
self.data_by_path[p] = path_values | ||
path_values[service_name] = None | ||
|
||
def init_values(self, service_name: str, api): | ||
paths_changed = [] | ||
for p, path_values in self.data_by_path.items(): | ||
if service_name in path_values: | ||
path_values[service_name] = api.get_value(service_name, p) | ||
paths_changed.append(p) | ||
return paths_changed | ||
|
||
def clear_values(self, service_name: str): | ||
paths_changed = [] | ||
for p, path_values in self.data_by_path.items(): | ||
if service_name in path_values: | ||
path_values[service_name] = None | ||
paths_changed.append(p) | ||
return paths_changed | ||
|
||
def update_service_value(self, service_name: str, path: str, value): | ||
path_values = self.data_by_path.get(path) | ||
if path_values: | ||
if service_name in path_values: | ||
path_values[service_name] = value | ||
|
||
def get_value(self, path: str): | ||
path_values = self.data_by_path.get(path) | ||
if path_values: | ||
for service_name in self.service_names: | ||
v = path_values.get(service_name) | ||
if v is not None: | ||
return v | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from data_merger import DataMerger | ||
import hooks | ||
import unittest | ||
|
||
|
||
class SimpleServiceNameResolver: | ||
def __init(self): | ||
pass | ||
|
||
def resolve_service_name(self, name): | ||
return name | ||
|
||
|
||
class HooksTest(unittest.TestCase): | ||
def test_custom_charging_hook(self): | ||
battery_name = "com.victronenergy.battery.test" | ||
merger = DataMerger(["class:hooks.CustomChargingHook", battery_name], ["/Dc/0/Voltage", "/Info/MaxChargeCurrent"], SimpleServiceNameResolver()) | ||
config = { | ||
"ccls": { | ||
"13": 15, | ||
"12": 10 | ||
} | ||
} | ||
hook = hooks.CustomChargingHook("class:hooks.CustomChargingHook", merger, **config) | ||
merger.update_service_value(battery_name, "/Info/MaxChargeCurrent", 20) | ||
merger.update_service_value(battery_name, "/Dc/0/Voltage", 12.6) | ||
paths_changed = hook.update_service_value(battery_name, "/Dc/0/Voltage", 12.6) | ||
self.assertListEqual(paths_changed, ["/Info/MaxChargeCurrent"]) | ||
self.assertEqual(merger.get_value("/Info/MaxChargeCurrent"), 10) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v3.6 | ||
v3.7 |