Skip to content

Commit

Permalink
Add parameter onlyUpdateIfChanged to Device.setState()
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeprag committed Nov 5, 2018
1 parent b6b5c6e commit fc697c9
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions telldus/src/telldus/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,29 @@ def setSensorValues(self, values):
self._manager.sensorValuesUpdated(self, values)
self._manager.save()

def setState(self, state, stateValue=None, ack=None, origin=None):
def setState(self, state, stateValue=None, ack=None, origin=None, onlyUpdateIfChanged=False):
"""
Update the state of the device. Use this method if the state should be updated from an
external source and not by an command. Examples if the state was updated on the device
itself.
:param state: The new state
:param stateValue: State value for the state if needed. Not all states has values.
:param ack: Internal, do not use
:param origin: The origin how the state was updated. If not set this will be "Incoming signal"
:param onlyUpdateIfChanged: Skip the update if the state is changed or not. If this is `False`
the new state will always trigger and update. This parameter was added in version 1.2
"""
if stateValue is None:
stateValue = ''
if self._state == state \
and self._stateValue == stateValue \
and self.lastUpdated \
and self.lastUpdated > int(time.time() - 1):
# Same state/statevalue and less than one second ago, most probably
# just the same value being resent, ignore
return
if self._state == state and self._stateValue == stateValue:
if self.lastUpdated and self.lastUpdated > int(time.time() - 1):
# Same state/statevalue and less than one second ago, most probably
# just the same value being resent, ignore
return
if onlyUpdateIfChanged:
# No need to update
return
self.lastUpdated = time.time()
self._state = state
self._stateValue = stateValue
Expand Down

0 comments on commit fc697c9

Please sign in to comment.