Exemplo n.º 1
0
def create_mqtt_client(
    driver_object,
    mqtt_server,
    mqtt_port,
    mqtt_username,
    mqtt_password,
    mqtt_base_topic,
    devices_names_config,
    ha_prefix,
    log_level,
):
    """Create MQTT client object, setup callbacks and connection to server."""
    logger.debug("Connecting to %s:%s", mqtt_server, mqtt_port)
    mqttc = mqtt.Client(
        client_id="dali2mqtt",
        userdata={
            "driver": driver_object,
            "base_topic": mqtt_base_topic,
            "ha_prefix": ha_prefix,
            "devices_names_config": devices_names_config,
            "log_level": log_level,
            "all_lamps": {},
        },
    )
    mqttc.will_set(
        MQTT_DALI2MQTT_STATUS.format(mqtt_base_topic), MQTT_NOT_AVAILABLE, retain=True
    )
    mqttc.on_connect = lambda a, b, c, d: on_connect(a, b, c, d, ha_prefix)

    # Add message callbacks that will only trigger on a specific subscription match.
    mqttc.message_callback_add(
        MQTT_COMMAND_TOPIC.format(mqtt_base_topic, "+"), on_message_cmd
    )
    mqttc.message_callback_add(
        MQTT_BRIGHTNESS_COMMAND_TOPIC.format(mqtt_base_topic, "+"),
        on_message_brightness_cmd,
    )
    mqttc.message_callback_add(
        MQTT_SCAN_LAMPS_COMMAND_TOPIC.format(mqtt_base_topic),
        on_message_reinitialize_lamps_cmd,
    )
    mqttc.on_message = on_message
    if mqtt_username:
        mqttc.username_pw_set(mqtt_username, mqtt_password)
    mqttc.connect(mqtt_server, mqtt_port, 60)
    return mqttc
Exemplo n.º 2
0
def on_connect(
    client,
    data_object,
    flags,
    result,
    ha_prefix=DEFAULT_HA_DISCOVERY_PREFIX,
):  # pylint: disable=W0613,R0913
    """Callback on connection to MQTT server."""
    mqtt_base_topic = data_object["base_topic"]
    driver_object = data_object["driver"]
    client.subscribe([
        (MQTT_COMMAND_TOPIC.format(mqtt_base_topic, "+"), 0),
        (MQTT_BRIGHTNESS_COMMAND_TOPIC.format(mqtt_base_topic, "+"), 0),
        (MQTT_SCAN_LAMPS_COMMAND_TOPIC.format(mqtt_base_topic), 0),
    ])
    client.publish(MQTT_DALI2MQTT_STATUS.format(mqtt_base_topic),
                   MQTT_AVAILABLE,
                   retain=True)
    initialize_lamps(data_object, client)
Exemplo n.º 3
0
def on_message_cmd(mqtt_client, data_object, msg):
    """Callback on MQTT command message."""
    logger.debug("Command on %s: %s", msg.topic, msg.payload)
    light = re.search(
        MQTT_COMMAND_TOPIC.format(data_object["base_topic"], "(.+?)"), msg.topic
    ).group(1)
    if msg.payload == MQTT_PAYLOAD_OFF:
        try:
            lamp_object = data_object["all_lamps"][light]
            logger.debug("Set light <%s> to %s", light, msg.payload)
            data_object["driver"].send(gear.Off(lamp_object.short_address))
            mqtt_client.publish(
                MQTT_STATE_TOPIC.format(data_object["base_topic"], light),
                MQTT_PAYLOAD_OFF,
                retain=True,
            )
        except DALIError as err:
            logger.error("Failed to set light <%s> to %s: %s", light, "OFF", err)
        except KeyError:
            logger.error("Lamp %s doesn't exists", light)
Exemplo n.º 4
0
 def gen_ha_config(self, mqtt_base_topic):
     """Generate a automatic configuration for Home Assistant."""
     json_config = {
         "name":
         self.friendly_name,
         "unique_id":
         "DALI2MQTT_LIGHT_{}".format(self.device_name),
         "state_topic":
         MQTT_STATE_TOPIC.format(mqtt_base_topic, self.device_name),
         "command_topic":
         MQTT_COMMAND_TOPIC.format(mqtt_base_topic, self.device_name),
         "payload_off":
         MQTT_PAYLOAD_OFF.decode("utf-8"),
         "brightness_state_topic":
         MQTT_BRIGHTNESS_STATE_TOPIC.format(mqtt_base_topic,
                                            self.device_name),
         "brightness_command_topic":
         MQTT_BRIGHTNESS_COMMAND_TOPIC.format(mqtt_base_topic,
                                              self.device_name),
         "brightness_scale":
         self.max_level,
         "on_command_type":
         "brightness",
         "availability_topic":
         MQTT_DALI2MQTT_STATUS.format(mqtt_base_topic),
         "payload_available":
         MQTT_AVAILABLE,
         "payload_not_available":
         MQTT_NOT_AVAILABLE,
         "device": {
             "identifiers": "dali2mqtt",
             "name": "DALI Lights",
             "sw_version": f"dali2mqtt {__version__}",
             "model": "dali2mqtt",
             "manufacturer": f"{__author__} <{__email__}>",
         },
     }
     return json.dumps(json_config)