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

def async_added_to_hass(self):

    @callback
    def message_received(topic, payload, qos):
        'Handle    a    new    received    MQTT    message.'
        if (self._template is not None):
            payload = self._template.async_render_with_possible_json_value(payload)
        if (payload == self._payload_on):
            self._state = True
        elif (payload == self._payload_off):
            self._state = False
        self.hass.async_add_job(self.async_update_ha_state())
    return mqtt.async_subscribe(self.hass, self._state_topic, message_received, self._qos)
