Skip to content

Commit

Permalink
Add support for variable precision (#138)
Browse files Browse the repository at this point in the history
* Add support for variable precision (fixes #111)

* Fix typo
  • Loading branch information
shbatm authored Oct 18, 2020
1 parent a490d43 commit 2a63127
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pyisy/variables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
_LOGGER,
ATTR_ID,
ATTR_INIT,
ATTR_PRECISION,
ATTR_TS,
ATTR_VAL,
ATTR_VAR,
Expand Down Expand Up @@ -121,19 +122,21 @@ def parse(self, xml):
vid = int(attr_from_element(feature, ATTR_ID))
vtype = int(attr_from_element(feature, TAG_TYPE))
init = value_from_xml(feature, ATTR_INIT)
prec = int(value_from_xml(feature, ATTR_PRECISION, 0))
val = value_from_xml(feature, ATTR_VAL)
ts_raw = value_from_xml(feature, ATTR_TS)
t_s = parser.parse(ts_raw)
vname = self.vnames[vtype].get(vid, "")

vobj = self.vobjs[vtype].get(vid)
if vobj is None:
vobj = Variable(self, vid, vtype, vname, init, val, t_s)
vobj = Variable(self, vid, vtype, vname, init, val, t_s, prec)
self.vids[vtype].append(vid)
self.vobjs[vtype][vid] = vobj
else:
vobj.init = init
vobj.status = val
vobj.prec = prec
vobj.last_edited = t_s

_LOGGER.info("ISY Loaded Variables")
Expand Down Expand Up @@ -166,6 +169,7 @@ def update_received(self, xmldoc):
vobj.init = int(value_from_xml(xmldoc, ATTR_INIT))
else:
vobj.status = int(value_from_xml(xmldoc, ATTR_VAL))
vobj.prec = int(value_from_xml(xmldoc, ATTR_PRECISION, 0))
vobj.last_edited = parser.parse(value_from_xml(xmldoc, ATTR_TS))

_LOGGER.debug("ISY Updated Variable: %s.%s", str(vtype), str(vid))
Expand Down
19 changes: 18 additions & 1 deletion pyisy/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ATTR_INIT,
ATTR_LAST_CHANGED,
ATTR_LAST_UPDATE,
ATTR_PRECISION,
ATTR_SET,
ATTR_STATUS,
ATTR_TS,
Expand Down Expand Up @@ -35,7 +36,7 @@ class Variable:
:ivar val: Watched property that represents the value of the variable.
"""

def __init__(self, variables, vid, vtype, vname, init, status, ts):
def __init__(self, variables, vid, vtype, vname, init, status, ts, prec):
"""Initialize a Variable class."""
super().__init__()
self._id = vid
Expand All @@ -44,6 +45,7 @@ def __init__(self, variables, vid, vtype, vname, init, status, ts):
self._last_update = now()
self._last_changed = now()
self._name = vname
self._prec = prec
self._status = status
self._type = vtype
self._variables = variables
Expand Down Expand Up @@ -116,6 +118,20 @@ def name(self):
"""Return the Variable Name."""
return self._name

@property
def prec(self):
"""Return the Variable Precision."""
return self._prec

@prec.setter
def prec(self, value):
"""Set the current node state and notify listeners."""
if self._prec != value:
self._prec = value
self._last_changed = now()
self.status_events.notify(self.status_feedback)
return self._prec

@property
def status(self):
"""Return the current node state."""
Expand All @@ -137,6 +153,7 @@ def status_feedback(self):
TAG_ADDRESS: self.address,
ATTR_STATUS: self._status,
ATTR_INIT: self._init,
ATTR_PRECISION: self._prec,
ATTR_TS: self._last_edited,
ATTR_LAST_CHANGED: self._last_changed,
ATTR_LAST_UPDATE: self._last_update,
Expand Down

0 comments on commit 2a63127

Please sign in to comment.