コード例 #1
0
ファイル: hass.py プロジェクト: bachya/ecowitt2mqtt
def get_entity_description(key: str, value: Union[float, int,
                                                  str]) -> EntityDescription:
    """Get an entity description for a data key.

    1. Return a specific data point if it exists.
    2. Return a globbed data point if it exists.
    3. Return defaults if no specific or globbed data points exist.
    """
    if DATA_POINT_GLOB_BATT in key and isinstance(value, str):
        # Because Ecowitt doesn't give us a clear way to know what sort of battery
        # we're looking at (a binary on/off battery or one that reports voltage), we
        # check its value: strings are binary, floats are voltage:
        return ENTITY_DESCRIPTIONS[DATA_POINT_GLOB_BATT_BINARY]

    if key in ENTITY_DESCRIPTIONS:
        return ENTITY_DESCRIPTIONS[key]

    globbed_descriptions = [
        v for k, v in ENTITY_DESCRIPTIONS.items() if k in key
    ]
    if globbed_descriptions:
        return globbed_descriptions[0]

    LOGGER.info("No entity description found for key: %s", key)
    return EntityDescription(platform=PLATFORM_SENSOR)
コード例 #2
0
ファイル: mqtt.py プロジェクト: the-real-orca/ecowitt2mqtt
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)
コード例 #3
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)
コード例 #4
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)
コード例 #5
0
ファイル: device.py プロジェクト: bachya/ecowitt2mqtt
def get_device_from_raw_payload(payload: Dict[str, Any]) -> Device:
    """Return a device based upon a model string."""
    model = payload["model"]
    station_type = payload.get("stationtype", DEFAULT_STATION_TYPE)
    unique_id = payload.get("PASSKEY", DEFAULT_UNIQUE_ID)

    if model in DEVICE_DATA:
        manufacturer, name = DEVICE_DATA[model]
    else:
        matches = [v for k, v in DEVICE_DATA.items() if k in model]
        if matches:
            manufacturer, name = matches[0]
        else:
            LOGGER.info(
                ("Unknown device; please report it at "
                 "https://github.com/bachya/ecowitt2mqtt (payload: %s)"),
                payload,
            )
            manufacturer = DEFAULT_MANUFACTURER
            name = DEFAULT_NAME

    return Device(unique_id, manufacturer, name, station_type)