Skip to content

Commit

Permalink
Merge 4839794 into e7bc941
Browse files Browse the repository at this point in the history
  • Loading branch information
henfri committed Jan 5, 2019
2 parents e7bc941 + 4839794 commit 556cdc9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pyblnet/blnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,55 @@ def _turn(self, digital_id, value, can_node=None):
else:
raise EnvironmentError('Can\'t set values with blnet web disabled')

def get_value(self, name=None, id=None, type='digital', ret='value', cached=None):
"""
higher level interface to get a value or mode by name or id
ret: can be 'value', 'mode'
returns the value if ret='value' or the mode if ret='mode' as first return value
and the dictionary containing name, value, id and mode as a second return value
cached: in order to prevent polling data from BLNet with every call,
the data can be fetched once and stored and passed to this function
"""
val = None
dic = None
if name is None and id is None: return val
if cached is None:
cached = self.fetch()
for key, v in cached[type].items():
if str(v['name']) == str(name) or str(v['id']) == str(id):
val = v[ret]
dic = v

return val, dic

def get_digital_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='digital', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_digital_mode(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='digital', ret='mode', name=name, id=id, cached=cached)
return val, dic

def get_analog_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='analog', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_energy_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='energy', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_speed_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='speed', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_power_value(self, name=None, id=None, cached=None):
val, dic=self.get_value(type='power', ret='value', name=name, id=id, cached=cached)
return val, dic



@staticmethod
def _convert_web(values):
"""
Expand Down
13 changes: 13 additions & 0 deletions pyblnet/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ def test_blnet_fetch(self):
STATE
)

def test_blnet_fetch_fine_grained(self):
""" Test fetching data in higher level class """
fetched = STATE

def scratch_tuple(type, id, ret):
return fetched[type][id][ret], fetched[type][id]

blnet = BLNET(ADDRESS, password=PASSWORD, timeout=10, use_ta=False, web_port=self.port)
self.assertEqual(blnet.get_analog_value(id=5, cached=fetched), scratch_tuple('analog', 5, 'value'))
self.assertEqual(blnet.get_analog_value(name='TSP.oben', cached=fetched), scratch_tuple('analog', 2, 'value'))
self.assertEqual(blnet.get_digital_value(id=1, cached=fetched), scratch_tuple('digital', 1, 'value'))
self.assertEqual(blnet.get_digital_mode(id=1, cached=fetched), scratch_tuple('digital', 1, 'mode'))

def test_blnet_web_analog(self):
""" Test reading analog values """
self.assertEqual(
Expand Down

0 comments on commit 556cdc9

Please sign in to comment.