示例#1
0
 def test_as_number_coercion(self):
     """Test state_as_number with number."""
     for _state in ('0', '0.0', 0, 0.0):
         self.assertEqual(
             0.0, state.state_as_number(
                 ha.State('domain.test', _state, {})))
     for _state in ('1', '1.0', 1, 1.0):
         self.assertEqual(
             1.0, state.state_as_number(
                 ha.State('domain.test', _state, {})))
示例#2
0
 def test_as_number_states(self):
     """Test state_as_number with states."""
     zero_states = (STATE_OFF, STATE_CLOSED, STATE_UNLOCKED,
                    STATE_BELOW_HORIZON)
     one_states = (STATE_ON, STATE_OPEN, STATE_LOCKED, STATE_ABOVE_HORIZON)
     for _state in zero_states:
         self.assertEqual(0, state.state_as_number(
             ha.State('domain.test', _state, {})))
     for _state in one_states:
         self.assertEqual(1, state.state_as_number(
             ha.State('domain.test', _state, {})))
示例#3
0
    def test_repr(self):
        """Test state.repr."""
        self.assertEqual(
            "<state happy.happy=on @ 1984-12-08T12:00:00+00:00>",
            str(
                ha.State("happy.happy",
                         "on",
                         last_changed=datetime(1984, 12, 8, 12, 0, 0))))

        self.assertEqual(
            "<state happy.happy=on; brightness=144 @ "
            "1984-12-08T12:00:00+00:00>",
            str(
                ha.State("happy.happy", "on", {"brightness": 144},
                         datetime(1984, 12, 8, 12, 0, 0))))
示例#4
0
 def test_as_number_invalid_cases(self):
     """Test state_as_number with invalid cases."""
     for _state in ('', 'foo', 'foo.bar', None, False, True, object,
                    object()):
         self.assertRaises(ValueError,
                           state.state_as_number,
                           ha.State('domain.test', _state, {}))
示例#5
0
    def test_event_listener(self, mock_gauge, mock_connection):
        """Test event listener."""
        config = {
            'statsd': {
                'host': 'host',
            }
        }
        hass = mock.MagicMock()
        statsd.setup(hass, config)
        self.assertTrue(hass.bus.listen.called)
        handler_method = hass.bus.listen.call_args_list[0][0][1]

        valid = {'1': 1,
                 '1.0': 1.0,
                 STATE_ON: 1,
                 STATE_OFF: 0}
        for in_, out in valid.items():
            state = mock.MagicMock(state=in_)
            handler_method(mock.MagicMock(data={'new_state': state}))
            mock_gauge.return_value.send.assert_called_once_with(
                state.entity_id, out)
            mock_gauge.return_value.send.reset_mock()

        for invalid in ('foo', '', object):
            handler_method(mock.MagicMock(data={
                'new_state': ha.State('domain.test', invalid, {})}))
            self.assertFalse(mock_gauge.return_value.send.called)
示例#6
0
 def test_send_to_graphite_errors(self, mock_time):
     """Test the sending with errors."""
     mock_time.return_value = 12345
     state = ha.State('domain.entity', STATE_ON, {'foo': 1.0})
     with mock.patch.object(self.gf, '_send_to_graphite') as mock_send:
         mock_send.side_effect = socket.error
         self.gf._report_attributes('entity', state)
         mock_send.side_effect = socket.gaierror
         self.gf._report_attributes('entity', state)
示例#7
0
    def test_reproduce_with_no_entity(self):
        """Test reproduce_state with no entity."""
        calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)

        state.reproduce_state(self.hass, ha.State('light.test', 'on'))

        self.hass.pool.block_till_done()

        self.assertTrue(len(calls) == 0)
        self.assertEqual(None, self.hass.states.get('light.test'))
示例#8
0
    def test_reproduce_group_same_data(self):
        """Test reproduce_state with group with same domain and data."""
        light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)

        self.hass.states.set('light.test1', 'off')
        self.hass.states.set('light.test2', 'off')

        state.reproduce_state(self.hass, [
            ha.State('light.test1', 'on', {'brightness': 95}),
            ha.State('light.test2', 'on', {'brightness': 95})])

        self.hass.pool.block_till_done()

        self.assertEqual(1, len(light_calls))
        last_call = light_calls[-1]
        self.assertEqual('light', last_call.domain)
        self.assertEqual(SERVICE_TURN_ON, last_call.service)
        self.assertEqual(['light.test1', 'light.test2'],
                         last_call.data.get('entity_id'))
        self.assertEqual(95, last_call.data.get('brightness'))
示例#9
0
    def test_reproduce_bad_state(self):
        """Test reproduce_state with bad state."""
        calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)

        self.hass.states.set('light.test', 'off')

        state.reproduce_state(self.hass, ha.State('light.test', 'bad'))

        self.hass.pool.block_till_done()

        self.assertTrue(len(calls) == 0)
        self.assertEqual('off', self.hass.states.get('light.test').state)
示例#10
0
    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)
示例#11
0
    def test_get_states(self):
        """Test getting states at a specific point in time."""
        self.init_recorder()
        states = []

        now = dt_util.utcnow()
        with patch('blumate.components.recorder.dt_util.utcnow',
                   return_value=now):
            for i in range(5):
                state = ha.State('test.point_in_time_{}'.format(i % 5),
                                 "State {}".format(i), {'attribute_test': i})

                mock_state_change_event(self.hass, state)

                states.append(state)

            self.wait_recording_done()

        future = now + timedelta(seconds=1)
        with patch('blumate.components.recorder.dt_util.utcnow',
                   return_value=future):
            for i in range(5):
                state = ha.State('test.point_in_time_{}'.format(i % 5),
                                 "State {}".format(i), {'attribute_test': i})

                mock_state_change_event(self.hass, state)

            self.wait_recording_done()

        # Get states returns everything before POINT
        self.assertEqual(
            states,
            sorted(history.get_states(future),
                   key=lambda state: state.entity_id))

        # Test get_state here because we have a DB setup
        self.assertEqual(states[0],
                         history.get_state(future, states[0].entity_id))
示例#12
0
    def test_reproduce_turn_on(self):
        """Test reproduce_state with SERVICE_TURN_ON."""
        calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)

        self.hass.states.set('light.test', 'off')

        state.reproduce_state(self.hass, ha.State('light.test', 'on'))

        self.hass.pool.block_till_done()

        self.assertTrue(len(calls) > 0)
        last_call = calls[-1]
        self.assertEqual('light', last_call.domain)
        self.assertEqual(SERVICE_TURN_ON, last_call.service)
        self.assertEqual(['light.test'], last_call.data.get('entity_id'))
示例#13
0
    def test_reproduce_media_pause(self):
        """Test reproduce_state with SERVICE_MEDIA_PAUSE."""
        calls = mock_service(self.hass, 'media_player', SERVICE_MEDIA_PAUSE)

        self.hass.states.set('media_player.test', 'playing')

        state.reproduce_state(
            self.hass, ha.State('media_player.test', 'paused'))

        self.hass.pool.block_till_done()

        self.assertTrue(len(calls) > 0)
        last_call = calls[-1]
        self.assertEqual('media_player', last_call.domain)
        self.assertEqual(SERVICE_MEDIA_PAUSE, last_call.service)
        self.assertEqual(['media_player.test'],
                         last_call.data.get('entity_id'))
示例#14
0
    def test_reproduce_group(self):
        """Test reproduce_state with group."""
        light_calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)

        self.hass.states.set('group.test', 'off', {
            'entity_id': ['light.test1', 'light.test2']})

        state.reproduce_state(self.hass, ha.State('group.test', 'on'))

        self.hass.pool.block_till_done()

        self.assertEqual(1, len(light_calls))
        last_call = light_calls[-1]
        self.assertEqual('light', last_call.domain)
        self.assertEqual(SERVICE_TURN_ON, last_call.service)
        self.assertEqual(['light.test1', 'light.test2'],
                         last_call.data.get('entity_id'))
示例#15
0
    def test_reproduce_complex_data(self):
        """Test reproduce_state with complex service data."""
        calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)

        self.hass.states.set('light.test', 'off')

        complex_data = ['hello', {'11': '22'}]

        state.reproduce_state(self.hass, ha.State('light.test', 'on', {
            'complex': complex_data
        }))

        self.hass.pool.block_till_done()

        self.assertTrue(len(calls) > 0)
        last_call = calls[-1]
        self.assertEqual('light', last_call.domain)
        self.assertEqual(SERVICE_TURN_ON, last_call.service)
        self.assertEqual(complex_data, last_call.data.get('complex'))
示例#16
0
    def test_reproduce_media_data(self):
        """Test reproduce_state with SERVICE_PLAY_MEDIA."""
        calls = mock_service(self.hass, 'media_player', SERVICE_PLAY_MEDIA)

        self.hass.states.set('media_player.test', 'off')

        media_attributes = {'media_content_type': 'movie',
                            'media_content_id': 'batman'}

        state.reproduce_state(self.hass, ha.State('media_player.test', 'None',
                                                  media_attributes))

        self.hass.pool.block_till_done()

        self.assertTrue(len(calls) > 0)
        last_call = calls[-1]
        self.assertEqual('media_player', last_call.domain)
        self.assertEqual(SERVICE_PLAY_MEDIA, last_call.service)
        self.assertEqual('movie', last_call.data.get('media_content_type'))
        self.assertEqual('batman', last_call.data.get('media_content_id'))
示例#17
0
    def test_report_with_binary_state(self, mock_time):
        """Test the reporting with binary state."""
        mock_time.return_value = 12345
        state = ha.State('domain.entity', STATE_ON, {'foo': 1.0})
        with mock.patch.object(self.gf, '_send_to_graphite') as mock_send:
            self.gf._report_attributes('entity', state)
            expected = [
                'bm.entity.foo 1.000000 12345',
                'bm.entity.state 1.000000 12345'
            ]
            actual = mock_send.call_args_list[0][0][0].split('\n')
            self.assertEqual(sorted(expected), sorted(actual))

        state.state = STATE_OFF
        with mock.patch.object(self.gf, '_send_to_graphite') as mock_send:
            self.gf._report_attributes('entity', state)
            expected = [
                'bm.entity.foo 1.000000 12345',
                'bm.entity.state 0.000000 12345'
            ]
            actual = mock_send.call_args_list[0][0][0].split('\n')
            self.assertEqual(sorted(expected), sorted(actual))
示例#18
0
 def test_dict_conversion(self):
     """Test conversion of dict."""
     state = ha.State('domain.hello', 'world', {'some': 'attr'})
     self.assertEqual(state, ha.State.from_dict(state.as_dict()))
示例#19
0
 def test_name_if_friendly_name_attr(self):
     """Test if there is a friendly name."""
     name = 'Some Unique Name'
     state = ha.State('domain.hello_world', 'world',
                      {ATTR_FRIENDLY_NAME: name})
     self.assertEqual(name, state.name)
示例#20
0
 def test_name_if_no_friendly_name_attr(self):
     """Test if there is no friendly name."""
     state = ha.State('domain.hello_world', 'world')
     self.assertEqual('hello world', state.name)
示例#21
0
 def test_object_id(self):
     """Test object ID."""
     state = ha.State('domain.hello', 'world')
     self.assertEqual('hello', state.object_id)
示例#22
0
 def test_domain(self):
     """Test domain."""
     state = ha.State('some_domain.hello', 'world')
     self.assertEqual('some_domain', state.domain)