Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. class DeviceController(object):
  2. # Setup stuff
  3. # ....
  4. def subscribe(self, obj, handler):
  5. """Subscribes to status updates of the requested object."""
  6. self._subscribers[obj] = handler
  7.  
  8. def _recv(self, obj, args):
  9. # First let the object update itself
  10. handled = obj.handle_update(args) #<-- Internally, args are passed.
  11.  
  12. # Now notify anyone who cares that device may have changed
  13. if handled and obj in self._subscribers:
  14. # Current code. To subscriber, library doesn't pass args:
  15. self._subscribers[obj](obj)
  16. # Ideal state -- pass args to subscribers. Will error for existing subscribers.
  17. self._subscribers[obj](obj, args)
  18.  
  19.  
  20. class TestDeviceController(object):
  21. # Setup stuff
  22. # ....
  23. def handler(self, obj):
  24. """Existing device handler signature
  25.  
  26. Don't get the most useful part (e.g., what actually changed)
  27. However, if we update self._subscribers[obj](obj) above to send args,
  28. any existing subscribers will break (i think...?)
  29. """
  30. _LOGGER.info("Handler Executed for: {}".format(obj))
  31.  
  32. def register(self):
  33. controller = DeviceController('IP Address', 'Username', 'Password')
  34.  
  35. for integration_id, keypad in controller._ids['DEVICE'].items():
  36. controller.subscribe(keypad, self.handler)
  37.  
  38. controller.connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement