comparison velib_python/test/mock_settings_device.py @ 8:9c0435a617db

Import velib_python
author Daniel O'Connor <darius@dons.net.au>
date Sun, 05 Dec 2021 14:35:36 +1030
parents
children
comparison
equal deleted inserted replaced
5:982eeffe9d95 8:9c0435a617db
1 PATH = 0
2 VALUE = 1
3 MINIMUM = 2
4 MAXIMUM = 3
5
6
7 class MockSettingsItem(object):
8 def __init__(self, parent, path):
9 self._parent = parent
10 self.path = path
11
12 def get_value(self):
13 setting = 'addSetting'+self.path
14 if setting in self._parent._settings:
15 return self._parent[setting]
16 return None
17
18 def set_value(self, value):
19 self._parent['addSetting'+self.path] = value
20
21 @property
22 def exists(self):
23 return 'addSetting'+self.path in self._parent._settings
24
25 # Simulates the SettingsSevice object without using the D-Bus (intended for unit tests). Values passed to
26 # __setitem__ (or the [] operator) will be stored in memory for later retrieval by __getitem__.
27 class MockSettingsDevice(object):
28 def __init__(self, supported_settings, event_callback, name='com.victronenergy.settings', timeout=0):
29 self._dbus_name = name
30 self._settings = supported_settings
31 self._event_callback = event_callback
32
33 def addSetting(self, path, value, _min, _max, silent=False, callback=None):
34 # Persist in our settings stash so the settings is available through
35 # the mock item
36 self._settings['addSetting'+path] = [path, value, _min, _max, silent]
37 return MockSettingsItem(self, path)
38
39 def get_short_name(self, path):
40 for k,v in self._settings.items():
41 if v[PATH] == path:
42 return k
43 return None
44
45 def __getitem__(self, setting):
46 return self._settings[setting][VALUE]
47
48 def __setitem__(self, setting, new_value):
49 s = self._settings.get(setting, None)
50 if s is None:
51 raise Exception('setting not found')
52 old_value = s[VALUE]
53 if old_value == new_value:
54 return
55 s[VALUE] = new_value
56 if self._event_callback is not None:
57 self._event_callback(setting, old_value, new_value)