'Subscribe to MQTT events.
This method must be run in the event loop and returns a coroutine.'

def async_added_to_hass(self):

    @callback
    def update_state(device_id, room, distance):
        'Update    the    sensor    state.'
        self._state = room
        self._distance = distance
        self._updated = dt.utcnow()
        self.hass.async_add_job(self.async_update_ha_state())
    @callback
    def message_received(topic, payload, qos):
        'Handle    new    MQTT    messages.'
        try:
            data = MQTT_PAYLOAD(payload)
        except vol.MultipleInvalid as error:
            _LOGGER.debug('Skipping    update    because    of    malformatted    data:    %s', error)
            return
        device = _parse_update_data(topic, data)
        if (device.get(CONF_DEVICE_ID) == self._device_id):
            if ((self._distance is None) or (self._updated is None)):
                update_state(**device)
            else:
                timediff = (dt.utcnow() - self._updated)
                if ((device.get(ATTR_ROOM) == self._state) or (device.get(ATTR_DISTANCE) < self._distance) or (timediff.seconds >= self._timeout)):
                    update_state(**device)
    return mqtt.async_subscribe(self.hass, self._state_topic, message_received, 1)
