Esempio n. 1
0
    def test_get_last_state_changes(self):
        """Test number of state changes."""
        self.init_recorder()
        entity_id = "sensor.test"

        def set_state(state):
            """Set the state."""
            self.hass.states.set(entity_id, state)
            wait_recording_done(self.hass)
            return self.hass.states.get(entity_id)

        start = dt_util.utcnow() - timedelta(minutes=2)
        point = start + timedelta(minutes=1)
        point2 = point + timedelta(minutes=1)

        with patch("homeassistant.components.recorder.dt_util.utcnow",
                   return_value=start):
            set_state("1")

        states = []
        with patch("homeassistant.components.recorder.dt_util.utcnow",
                   return_value=point):
            states.append(set_state("2"))

        with patch("homeassistant.components.recorder.dt_util.utcnow",
                   return_value=point2):
            states.append(set_state("3"))

        hist = history.get_last_state_changes(self.hass, 2, entity_id)

        assert states == hist[entity_id]
Esempio n. 2
0
    def test_get_last_state_changes(self):
        """Test number of state changes."""
        self.init_recorder()
        entity_id = 'sensor.test'

        def set_state(state):
            """Set the state."""
            self.hass.states.set(entity_id, state)
            self.wait_recording_done()
            return self.hass.states.get(entity_id)

        start = dt_util.utcnow() - timedelta(minutes=2)
        point = start + timedelta(minutes=1)
        point2 = point + timedelta(minutes=1)

        with patch('homeassistant.components.recorder.dt_util.utcnow',
                   return_value=start):
            set_state('1')

        states = []
        with patch('homeassistant.components.recorder.dt_util.utcnow',
                   return_value=point):
            states.append(set_state('2'))

        with patch('homeassistant.components.recorder.dt_util.utcnow',
                   return_value=point2):
            states.append(set_state('3'))

        hist = history.get_last_state_changes(
            self.hass, 2, entity_id)

        self.assertEqual(states, hist[entity_id])
Esempio n. 3
0
    def test_get_last_state_changes(self):
        """Test number of state changes."""
        self.init_recorder()
        entity_id = 'sensor.test'

        def set_state(state):
            """Set the state."""
            self.hass.states.set(entity_id, state)
            self.wait_recording_done()
            return self.hass.states.get(entity_id)

        start = dt_util.utcnow() - timedelta(minutes=2)
        point = start + timedelta(minutes=1)
        point2 = point + timedelta(minutes=1)

        with patch('homeassistant.components.recorder.dt_util.utcnow',
                   return_value=start):
            set_state('1')

        states = []
        with patch('homeassistant.components.recorder.dt_util.utcnow',
                   return_value=point):
            states.append(set_state('2'))

        with patch('homeassistant.components.recorder.dt_util.utcnow',
                   return_value=point2):
            states.append(set_state('3'))

        hist = history.get_last_state_changes(self.hass, 2, entity_id)

        self.assertEqual(states, hist[entity_id])
Esempio n. 4
0
    def test_ensure_state_can_be_copied(self):
        """Ensure a state can pass though copy().

        The filter integration uses copy() on states
        from history.
        """
        self.test_setup()
        entity_id = "sensor.test"

        def set_state(state):
            """Set the state."""
            self.hass.states.set(entity_id, state)
            wait_recording_done(self.hass)
            return self.hass.states.get(entity_id)

        start = dt_util.utcnow() - timedelta(minutes=2)
        point = start + timedelta(minutes=1)

        with patch(
            "homeassistant.components.recorder.dt_util.utcnow", return_value=start
        ):
            set_state("1")

        with patch(
            "homeassistant.components.recorder.dt_util.utcnow", return_value=point
        ):
            set_state("2")

        hist = history.get_last_state_changes(self.hass, 2, entity_id)

        assert copy(hist[entity_id][0]) == hist[entity_id][0]
        assert copy(hist[entity_id][1]) == hist[entity_id][1]
Esempio n. 5
0
 async def async_added_to_hass(self):
     """Setup states now that device is in hass"""
     states = history.get_last_state_changes(self.hass, 1, self.entity_id)
     if states:
         states = states[self.entity_id]
         if states != None and len(states) > 0:
             state = states[0]
             if state != None and state.state == 'on':
                 self.turn_on()