예제 #1
0
    def test_repr(self):
        """ Test state.repr """
        self.assertEqual(
            "<state on @ 12:00:00 08-12-1984>",
            str(
                ha.State("happy.happy",
                         "on",
                         last_changed=datetime(1984, 12, 8, 12, 0, 0))))

        self.assertEqual(
            "<state on:brightness=144 @ 12:00:00 08-12-1984>",
            str(
                ha.State("happy.happy", "on", {"brightness": 144},
                         datetime(1984, 12, 8, 12, 0, 0))))
    def test_get_states(self):
        """ Test getting states at a specific point in time. """
        self.init_recorder()
        states = []

        # Create 10 states for 5 different entities
        # After the first 5, sleep a second and save the time
        # history.get_states takes the latest states BEFORE point X

        for i in range(10):
            state = ha.State('test.point_in_time_{}'.format(i % 5),
                             "State {}".format(i), {'attribute_test': i})

            mock_state_change_event(self.hass, state)
            self.hass.pool.block_till_done()
            recorder._INSTANCE.block_till_done()

            if i < 5:
                states.append(state)

                if i == 4:
                    time.sleep(1)
                    point = dt_util.utcnow()

        self.assertEqual(
            states,
            sorted(history.get_states(point),
                   key=lambda state: state.entity_id))

        # Test get_state here because we have a DB setup
        self.assertEqual(states[0],
                         history.get_state(point, states[0].entity_id))
    def create_state_changed_event(self, event_time_fired, entity_id, state):
        """ Create state changed event. """

        # Logbook only cares about state change events that
        # contain an old state but will not actually act on it.
        state = ha.State(entity_id, state).as_dict()

        return ha.Event(EVENT_STATE_CHANGED, {
            'entity_id': entity_id,
            'old_state': state,
            'new_state': state,
        },
                        time_fired=event_time_fired)
예제 #4
0
 def test_dict_conversion(self):
     state = ha.State('domain.hello', 'world', {'some': 'attr'})
     self.assertEqual(state, ha.State.from_dict(state.as_dict()))
예제 #5
0
 def test_copy(self):
     state = ha.State('domain.hello', 'world', {'some': 'attr'})
     self.assertEqual(state, state.copy())
예제 #6
0
 def test_name_if_friendly_name_attr(self):
     name = 'Some Unique Name'
     state = ha.State('domain.hello_world', 'world',
                      {ATTR_FRIENDLY_NAME: name})
     self.assertEqual(name, state.name)
예제 #7
0
 def test_name_if_no_friendly_name_attr(self):
     state = ha.State('domain.hello_world', 'world')
     self.assertEqual('hello world', state.name)
예제 #8
0
 def test_object_id(self):
     state = ha.State('domain.hello', 'world')
     self.assertEqual('hello', state.object_id)
예제 #9
0
 def test_domain(self):
     state = ha.State('some_domain.hello', 'world')
     self.assertEqual('some_domain', state.domain)