Ejemplo n.º 1
0
async def _async_publish_to_hass_discovery(
        client: Client, data: dict, discovery_manager: HassDiscovery) -> None:
    """Publish data to appropriate topics for Home Assistant Discovery."""
    LOGGER.debug(
        "Publishing according to Home Assistant MQTT Discovery standard")

    try:
        async with client:
            tasks = []
            for key, value in data.items():
                config_payload = discovery_manager.get_config_payload(key)
                config_topic = discovery_manager.get_config_topic(key)

                tasks.append(
                    client.publish(config_topic,
                                   _generate_payload(config_payload)))
                tasks.append(
                    client.publish(
                        config_payload["availability_topic"],
                        _generate_payload("online"),
                    ))
                tasks.append(
                    client.publish(config_payload["state_topic"],
                                   _generate_payload(value)))

            await asyncio.gather(*tasks)
    except MqttError as err:
        LOGGER.error("Error while publishing to HASS Discovery: %s", err)
        return

    LOGGER.info("Published to HASS discovery: %s", data)
Ejemplo n.º 2
0
    async def async_disconnect(self) -> None:
        """Disconnect from the MQTT broker."""
        try:
            await self._client.disconnect()
        except MqttError as err:
            LOGGER.error("Error while disconnecting from MQTT broker: %s", err)

        LOGGER.debug("Disconnected from MQTT broker")
Ejemplo n.º 3
0
    async def async_connect(self) -> None:
        """Connect to the MQTT broker."""
        try:
            await self._client.connect()
        except MqttError as err:
            LOGGER.error("Error while connecting to MQTT broker: %s", err)

        LOGGER.debug("Connected to MQTT broker")
Ejemplo n.º 4
0
async def _async_publish_to_topic(
    client: Client, data: Dict[str, Any], topic: str
) -> None:
    """Publish data to a single MQTT topic."""
    LOGGER.debug("Publishing entire device payload to single topic: %s", topic)

    try:
        async with client:
            await client.publish(topic, _generate_payload(data))
    except MqttError as err:
        LOGGER.error("Error while publishing to %s: %s", topic, err)
        return

    LOGGER.info("Published to %s: %s", topic, data)
Ejemplo n.º 5
0
    async def async_publish(self, topic: str, data: Union[dict, float,
                                                          str]) -> None:
        """Publish data to an MQTT topic."""
        if isinstance(data, dict):
            payload = json.dumps(data).encode("utf-8")
        elif isinstance(data, str):
            payload = data.encode("utf-8")
        else:
            payload = str(data).encode("utf-8")

        try:
            await self._client.publish(topic, payload)
            LOGGER.info("Data published to topic %s: %s", topic, data)
        except MqttError as err:
            LOGGER.error("Error while publishing data to MQTT: %s", err)