def setup(hass, config):
    'Set up the StatsD component.'
    conf = config[DOMAIN]
    host = conf.get(CONF_HOST)
    port = conf.get(CONF_PORT)
    sample_rate = conf.get(CONF_RATE)
    prefix = conf.get(CONF_PREFIX)
    value_mapping = conf.get(CONF_VALUE_MAP)
    show_attribute_flag = conf.get(CONF_ATTR)
    statsd_client = statsd.StatsClient(host=host, port=port, prefix=prefix)

    def statsd_event_listener(event):
        'Listen for new messages on the bus and sends them to StatsD.'
        state = event.data.get('new_state')
        if (state is None):
            return
        try:
            if (value_mapping and (state.state in value_mapping)):
                _state = float(value_mapping[state.state])
            else:
                _state = state_helper.state_as_number(state)
        except ValueError:
            _state = None
        states = dict(state.attributes)
        _LOGGER.debug('Sending %s', state.entity_id)
        if (show_attribute_flag is True):
            if isinstance(_state, (float, int)):
                statsd_client.gauge(('%s.state' % state.entity_id), _state, sample_rate)
            for (key, value) in states.items():
                if isinstance(value, (float, int)):
                    stat = ('%s.%s' % (state.entity_id, key.replace(' ', '_')))
                    statsd_client.gauge(stat, value, sample_rate)
        elif isinstance(_state, (float, int)):
            statsd_client.gauge(state.entity_id, _state, sample_rate)
        statsd_client.incr(state.entity_id, rate=sample_rate)
    hass.bus.listen(EVENT_STATE_CHANGED, statsd_event_listener)
    return True
Set up the StatsD component.
def setup(hass, config):
    
    conf = config[DOMAIN]
    host = conf.get(CONF_HOST)
    port = conf.get(CONF_PORT)
    sample_rate = conf.get(CONF_RATE)
    prefix = conf.get(CONF_PREFIX)
    value_mapping = conf.get(CONF_VALUE_MAP)
    show_attribute_flag = conf.get(CONF_ATTR)
    statsd_client = statsd.StatsClient(host=host, port=port, prefix=prefix)

    def statsd_event_listener(event):
        'Listen for new messages on the bus and sends them to StatsD.'
        state = event.data.get('new_state')
        if (state is None):
            return
        try:
            if (value_mapping and (state.state in value_mapping)):
                _state = float(value_mapping[state.state])
            else:
                _state = state_helper.state_as_number(state)
        except ValueError:
            _state = None
        states = dict(state.attributes)
        _LOGGER.debug('Sending %s', state.entity_id)
        if (show_attribute_flag is True):
            if isinstance(_state, (float, int)):
                statsd_client.gauge(('%s.state' % state.entity_id), _state, sample_rate)
            for (key, value) in states.items():
                if isinstance(value, (float, int)):
                    stat = ('%s.%s' % (state.entity_id, key.replace(' ', '_')))
                    statsd_client.gauge(stat, value, sample_rate)
        elif isinstance(_state, (float, int)):
            statsd_client.gauge(state.entity_id, _state, sample_rate)
        statsd_client.incr(state.entity_id, rate=sample_rate)
    hass.bus.listen(EVENT_STATE_CHANGED, statsd_event_listener)
    return True