From 2a63127989ddc82a8274ec4be28deba3fa78a87a Mon Sep 17 00:00:00 2001 From: shbatm Date: Sat, 17 Oct 2020 20:07:39 -0500 Subject: [PATCH] Add support for variable precision (#138) * Add support for variable precision (fixes #111) * Fix typo --- pyisy/variables/__init__.py | 6 +++++- pyisy/variables/variable.py | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pyisy/variables/__init__.py b/pyisy/variables/__init__.py index 2ba37401..20c4227f 100644 --- a/pyisy/variables/__init__.py +++ b/pyisy/variables/__init__.py @@ -8,6 +8,7 @@ _LOGGER, ATTR_ID, ATTR_INIT, + ATTR_PRECISION, ATTR_TS, ATTR_VAL, ATTR_VAR, @@ -121,6 +122,7 @@ 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) @@ -128,12 +130,13 @@ def parse(self, xml): 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") @@ -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)) diff --git a/pyisy/variables/variable.py b/pyisy/variables/variable.py index 0eedf7e6..9dcd77d3 100644 --- a/pyisy/variables/variable.py +++ b/pyisy/variables/variable.py @@ -4,6 +4,7 @@ ATTR_INIT, ATTR_LAST_CHANGED, ATTR_LAST_UPDATE, + ATTR_PRECISION, ATTR_SET, ATTR_STATUS, ATTR_TS, @@ -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 @@ -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 @@ -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.""" @@ -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,