Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unknown as the "missing" HASS MQTT Discovery state payload #294

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions ecowitt2mqtt/helpers/publisher/hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,24 @@ class HassDiscoveryPayload:
DATA_POINT_YRAIN_PIEZO: StateClass.TOTAL,
}

STATE_UNKNOWN = "unknown"


def get_availability_payload(data_point: CalculatedDataPoint) -> str:
"""Get the availability payload for a data point."""
if data_point.value is None:
return AVAILABILITY_OFFLINE
"""Get the availability payload for a data point.

Right now, this is hardcoded to always available.
"""
return AVAILABILITY_ONLINE


def get_state_payload(data_point: CalculatedDataPoint) -> DataValueType:
"""Get the state payload for a data point."""
if not data_point.value:
return STATE_UNKNOWN
return data_point.value


class HomeAssistantDiscoveryPublisher(MqttPublisher):
"""Define an MQTT publisher for the MQTT Discovery standard."""

Expand Down Expand Up @@ -490,7 +500,10 @@ async def async_publish(self, data: dict[str, DataValueType]) -> None:
discovery_payload.payload["json_attributes_topic"],
data_point.attributes,
),
(discovery_payload.payload["state_topic"], data_point.value),
(
discovery_payload.payload["state_topic"],
get_state_payload(data_point),
),
):
tasks.append(
asyncio.create_task(
Expand Down
Loading