'Test event listener.'

@mock.patch('statsd.StatsClient')
def test_event_listener_attr_details(self, mock_client):

    config = {'statsd': {'host': 'host', 'log_attributes': True}}
    config['statsd'][statsd.CONF_RATE] = statsd.DEFAULT_RATE
    self.hass.bus.listen = mock.MagicMock()
    setup_component(self.hass, statsd.DOMAIN, config)
    self.assertTrue(self.hass.bus.listen.called)
    handler_method = self.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_, attributes={'attribute    key': 3.2})
        handler_method(mock.MagicMock(data={'new_state': state}))
        mock_client.return_value.gauge.assert_has_calls([mock.call(('%s.state' % state.entity_id), out, statsd.DEFAULT_RATE), mock.call(('%s.attribute_key' % state.entity_id), 3.2, statsd.DEFAULT_RATE)])
        mock_client.return_value.gauge.reset_mock()
        self.assertEqual(mock_client.return_value.incr.call_count, 1)
        self.assertEqual(mock_client.return_value.incr.call_args, mock.call(state.entity_id, rate=statsd.DEFAULT_RATE))
        mock_client.return_value.incr.reset_mock()
    for invalid in ('foo', '', object):
        handler_method(mock.MagicMock(data={'new_state': ha.State('domain.test', invalid, {})}))
        self.assertFalse(mock_client.return_value.gauge.called)
        self.assertTrue(mock_client.return_value.incr.called)
