Esempio n. 1
0
    def _handle_push_notification(self, namespace, payload, from_myself=False):
        def fire_switch_state_change(dev, channel_id, o_state, n_state,
                                     f_myself):
            if o_state != n_state:
                evt = DeviceSwitchStatusEvent(dev=dev,
                                              channel_id=channel_id,
                                              switch_state=n_state,
                                              generated_by_myself=f_myself)
                self.fire_event(evt)

        with self._state_lock:
            if namespace == TOGGLE:
                # Update the local state and fire the event only if the state actually changed
                channel_index = 0
                old_switch_state = self._state.get(channel_index)
                switch_state = payload['toggle']['onoff'] == 1
                self._state[channel_index] = switch_state
                fire_switch_state_change(self, channel_index, old_switch_state,
                                         switch_state, from_myself)
                return True

            elif namespace == TOGGLEX:
                if isinstance(payload['togglex'], list):
                    for c in payload['togglex']:
                        # Update the local state and fire the event only if the state actually changed
                        channel_index = c['channel']
                        old_switch_state = self._state.get(channel_index)
                        switch_state = c['onoff'] == 1
                        self._state[channel_index] = switch_state
                        fire_switch_state_change(self, channel_index,
                                                 old_switch_state,
                                                 switch_state, from_myself)
                        return True

                elif isinstance(payload['togglex'], dict):
                    # Update the local state and fire the event only if the state actually changed
                    channel_index = payload['togglex']['channel']
                    old_switch_state = self._state.get(channel_index)
                    switch_state = payload['togglex']['onoff'] == 1
                    self._state[channel_index] = switch_state
                    fire_switch_state_change(self, channel_index,
                                             old_switch_state, switch_state,
                                             from_myself)
                    return True

            elif namespace == REPORT:
                # Ignoring REPORT
                l.info("Report event is currently unhandled.")
                return False

            elif namespace == CONSUMPTIONX:
                # Ignoring
                l.info("ConsumptionX push event is currently ignored")
                return False

            else:
                l.error("Unknown/Unsupported namespace/command: %s" %
                        namespace)
                l.debug("Namespace: %s, Data: %s" % (namespace, payload))
                return False
Esempio n. 2
0
    def _handle_push_notification(self, namespace, payload, from_myself=False):
        def fire_garage_door_state_change(dev, channel_id, o_state, n_state,
                                          f_myself):
            if o_state != n_state:
                evt = DeviceDoorStatusEvent(dev=dev,
                                            channel_id=channel_id,
                                            door_state=n_state,
                                            generated_by_myself=f_myself)
                self.fire_event(evt)

        with self._state_lock:
            if namespace == GARAGE_DOOR_STATE:
                for door in payload['state']:
                    channel_index = door['channel']
                    state = door['open'] == 1
                    old_state = None
                    if self._door_state is not None:
                        old_state = self._door_state[channel_index]
                        self._door_state[channel_index] = state

                    fire_garage_door_state_change(self, channel_index,
                                                  old_state, state,
                                                  from_myself)
                    return True

            elif namespace == REPORT:
                # For now, we simply ignore push notification of these kind.
                # In the future, we might think of handling such notification by caching them
                # and avoid the network round-trip when asking for power consumption (if the latest report is
                # recent enough)
                l.warning("Report command is currently not handled.")
                return False

            else:
                l.error("Unknown/Unsupported namespace/command: %s" %
                        namespace)
                l.debug("Namespace: %s, Data: %s" % (namespace, payload))
                return False