Ejemplo n.º 1
0
    def test_track_point_in_time(self):
        """Test track point in time."""
        before_birthday = datetime(1985, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
        birthday_paulus = datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
        after_birthday = datetime(1987, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)

        runs = []

        track_point_in_utc_time(self.hass, lambda x: runs.append(1),
                                birthday_paulus)

        self._send_time_changed(before_birthday)
        self.hass.pool.block_till_done()
        self.assertEqual(0, len(runs))

        self._send_time_changed(birthday_paulus)
        self.hass.pool.block_till_done()
        self.assertEqual(1, len(runs))

        # A point in time tracker will only fire once, this should do nothing
        self._send_time_changed(birthday_paulus)
        self.hass.pool.block_till_done()
        self.assertEqual(1, len(runs))

        track_point_in_time(self.hass, lambda x: runs.append(1),
                            birthday_paulus)

        self._send_time_changed(after_birthday)
        self.hass.pool.block_till_done()
        self.assertEqual(2, len(runs))
Ejemplo n.º 2
0
    def test_track_point_in_time(self):
        """Test track point in time."""
        before_birthday = datetime(1985, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
        birthday_paulus = datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
        after_birthday = datetime(1987, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)

        runs = []

        track_point_in_utc_time(
            self.hass, lambda x: runs.append(1), birthday_paulus)

        self._send_time_changed(before_birthday)
        self.hass.pool.block_till_done()
        self.assertEqual(0, len(runs))

        self._send_time_changed(birthday_paulus)
        self.hass.pool.block_till_done()
        self.assertEqual(1, len(runs))

        # A point in time tracker will only fire once, this should do nothing
        self._send_time_changed(birthday_paulus)
        self.hass.pool.block_till_done()
        self.assertEqual(1, len(runs))

        track_point_in_time(
            self.hass, lambda x: runs.append(1), birthday_paulus)

        self._send_time_changed(after_birthday)
        self.hass.pool.block_till_done()
        self.assertEqual(2, len(runs))
Ejemplo n.º 3
0
 def value_changed(self, value):
     """Called when a value has changed on the network."""
     if self._value.value_id == value.value_id:
         self.update_ha_state()
         if value.data:
             # only allow this value to be true for re_arm secs
             self.invalidate_after = dt_util.utcnow() + datetime.timedelta(
                 seconds=self.re_arm_sec)
             track_point_in_time(
                 self._hass, self.update_ha_state,
                 self.invalidate_after)
Ejemplo n.º 4
0
    def alarm_arm_home(self, code=None):
        """Send arm home command."""
        if not self._validate_code(code, STATE_ALARM_ARMED_HOME):
            return

        self._state = STATE_ALARM_ARMED_HOME
        self._state_ts = dt_util.utcnow()
        self.update_ha_state()

        if self._pending_time:
            track_point_in_time(self._bmss, self.update_ha_state,
                                self._state_ts + self._pending_time)
Ejemplo n.º 5
0
 def __init__(self, sensor_value, sensor_class, hass, re_arm_sec=60):
     """Initialize the sensor."""
     super(ZWaveTriggerSensor, self).__init__(sensor_value, sensor_class)
     self._hass = hass
     self.re_arm_sec = re_arm_sec
     self.invalidate_after = dt_util.utcnow() + datetime.timedelta(
         seconds=self.re_arm_sec)
     # If it's active make sure that we set the timeout tracker
     if sensor_value.data:
         track_point_in_time(
             self._hass, self.update_ha_state,
             self.invalidate_after)
Ejemplo n.º 6
0
    def alarm_trigger(self, code=None):
        """Send alarm trigger command. No code needed."""
        self._state = STATE_ALARM_TRIGGERED
        self._state_ts = dt_util.utcnow()
        self.update_ha_state()

        if self._trigger_time:
            track_point_in_time(self._bmss, self.update_ha_state,
                                self._state_ts + self._pending_time)

            track_point_in_time(
                self._bmss, self.update_ha_state,
                self._state_ts + self._pending_time + self._trigger_time)
Ejemplo n.º 7
0
    def alarm_arm_home(self, code=None):
        """Send arm home command."""
        if not self._validate_code(code, STATE_ALARM_ARMED_HOME):
            return

        self._state = STATE_ALARM_ARMED_HOME
        self._state_ts = dt_util.utcnow()
        self.update_ha_state()

        if self._pending_time:
            track_point_in_time(
                self._bmss, self.update_ha_state,
                self._state_ts + self._pending_time)
Ejemplo n.º 8
0
    def alarm_trigger(self, code=None):
        """Send alarm trigger command. No code needed."""
        self._state = STATE_ALARM_TRIGGERED
        self._state_ts = dt_util.utcnow()
        self.update_ha_state()

        if self._trigger_time:
            track_point_in_time(
                self._bmss, self.update_ha_state,
                self._state_ts + self._pending_time)

            track_point_in_time(
                self._bmss, self.update_ha_state,
                self._state_ts + self._pending_time + self._trigger_time)
Ejemplo n.º 9
0
    def schedule_lights_at_sun_set(hass, entity, old_state, new_state):
        """The moment sun sets we want to have all the lights on.

        We will schedule to have each light start after one another
        and slowly transition in.
        """
        start_point = calc_time_for_light_when_sunset()
        if not start_point:
            return

        def turn_on(light_id):
            """Lambda can keep track of function parameters.

            No local parameters. If we put the lambda directly in the below
            statement only the last light will be turned on.
            """
            return lambda now: turn_light_on_before_sunset(light_id)

        for index, light_id in enumerate(light_ids):
            track_point_in_time(hass, turn_on(light_id),
                                start_point + index * LIGHT_TRANSITION_TIME)
Ejemplo n.º 10
0
    def schedule_lights_at_sun_set(hass, entity, old_state, new_state):
        """The moment sun sets we want to have all the lights on.

        We will schedule to have each light start after one another
        and slowly transition in.
        """
        start_point = calc_time_for_light_when_sunset()
        if not start_point:
            return

        def turn_on(light_id):
            """Lambda can keep track of function parameters.

            No local parameters. If we put the lambda directly in the below
            statement only the last light will be turned on.
            """
            return lambda now: turn_light_on_before_sunset(light_id)

        for index, light_id in enumerate(light_ids):
            track_point_in_time(hass, turn_on(light_id),
                                start_point + index * LIGHT_TRANSITION_TIME)
Ejemplo n.º 11
0
    def state_automation_listener(entity, from_s, to_s):
        """Listen for state changes and calls action."""
        def call_action():
            """Call action with right context."""
            action({
                'trigger': {
                    'platform': 'state',
                    'entity_id': entity,
                    'from_state': from_s,
                    'to_state': to_s,
                    'for': time_delta,
                }
            })

        if time_delta is None:
            call_action()
            return

        def state_for_listener(now):
            """Fire on state changes after a delay and calls action."""
            hass.bus.remove_listener(EVENT_STATE_CHANGED,
                                     attached_state_for_cancel)
            call_action()

        def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
            """Fire on changes and cancel for listener if changed."""
            if inner_to_s.state == to_s.state:
                return
            hass.bus.remove_listener(EVENT_TIME_CHANGED,
                                     attached_state_for_listener)
            hass.bus.remove_listener(EVENT_STATE_CHANGED,
                                     attached_state_for_cancel)

        attached_state_for_listener = track_point_in_time(
            hass, state_for_listener,
            dt_util.utcnow() + time_delta)

        attached_state_for_cancel = track_state_change(
            hass, entity, state_for_cancel_listener)
Ejemplo n.º 12
0
    def state_automation_listener(entity, from_s, to_s):
        """Listen for state changes and calls action."""
        def call_action():
            """Call action with right context."""
            action({
                'trigger': {
                    'platform': 'state',
                    'entity_id': entity,
                    'from_state': from_s,
                    'to_state': to_s,
                    'for': time_delta,
                }
            })

        if time_delta is None:
            call_action()
            return

        def state_for_listener(now):
            """Fire on state changes after a delay and calls action."""
            hass.bus.remove_listener(
                EVENT_STATE_CHANGED, attached_state_for_cancel)
            call_action()

        def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
            """Fire on changes and cancel for listener if changed."""
            if inner_to_s.state == to_s.state:
                return
            hass.bus.remove_listener(EVENT_TIME_CHANGED,
                                     attached_state_for_listener)
            hass.bus.remove_listener(EVENT_STATE_CHANGED,
                                     attached_state_for_cancel)

        attached_state_for_listener = track_point_in_time(
            hass, state_for_listener, dt_util.utcnow() + time_delta)

        attached_state_for_cancel = track_state_change(
            hass, entity, state_for_cancel_listener)