HA Discovery fix: energy-cost sensors are always-available
The energy-cost device has only sensors (no online/offline heartbeat), but the discovery config still declared an availability topic that nothing ever publishes 'online' to — so Home Assistant marked every energy-cost entity 'unavailable' even though the state topic was being published (visible in MQTT Explorer). - DeviceInfo gains provides_availability (default True, keeps Modbus behavior). - _energy_cost_provider sets provides_availability=False. - build_discovery_payload only emits availability/availability_mode for devices that publish a heartbeat; HA treats the rest as always-available. - Test asserts energy-cost configs omit availability.
This commit is contained in:
@@ -58,6 +58,21 @@ class DeviceInfo:
|
||||
name: str
|
||||
"""Human-readable device name (may change; triggers HA discovery re-publish)."""
|
||||
|
||||
provides_availability: bool = True
|
||||
"""Whether this device publishes an availability ("online"/"offline") heartbeat.
|
||||
|
||||
When True (the default — e.g. Modbus devices, which have an ``online``
|
||||
binary_sensor driving availability), the HA Discovery config for each entity
|
||||
declares an ``availability`` topic and HA marks the entity unavailable until
|
||||
"online" is published.
|
||||
|
||||
When False (e.g. the energy-cost device, which has only sensors and no
|
||||
heartbeat source), the config omits ``availability`` so HA treats the entity
|
||||
as *always available* and shows its state as soon as one arrives. Without
|
||||
this, such entities would stay perpetually ``unavailable`` in HA even though
|
||||
their state is being published.
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExposableEntity:
|
||||
@@ -422,9 +437,13 @@ def _energy_cost_provider(session: Session) -> list[ExposableEntity]:
|
||||
currency = latest_period.currency
|
||||
|
||||
# --- Shared DeviceInfo (2-element identifiers — required by ha_discovery.py [1] access) ---
|
||||
# provides_availability=False: the energy-cost device has only sensors and no
|
||||
# online/offline heartbeat, so its entities must be "always available" in HA.
|
||||
# (Otherwise HA shows them unavailable despite state being published.)
|
||||
device_info = DeviceInfo(
|
||||
identifiers=("energy-cost", "energy-cost"),
|
||||
name="Energy Cost",
|
||||
provides_availability=False,
|
||||
)
|
||||
|
||||
# --- value_getter: current buy price ---
|
||||
|
||||
@@ -133,14 +133,21 @@ def build_discovery_payload(
|
||||
"unique_id": _unique_id(entity),
|
||||
"name": entity.name,
|
||||
"state_topic": state_t,
|
||||
"availability": [{"topic": avail_topic}],
|
||||
"availability_mode": "all",
|
||||
"device": {
|
||||
"identifiers": list(entity.device.identifiers),
|
||||
"name": entity.device.name,
|
||||
},
|
||||
}
|
||||
|
||||
# Only declare an availability topic for devices that actually publish an
|
||||
# online/offline heartbeat (e.g. Modbus, via its "online" binary_sensor).
|
||||
# Devices without a heartbeat (e.g. energy-cost) omit availability so HA
|
||||
# treats their entities as always-available — otherwise HA would mark them
|
||||
# ``unavailable`` forever even though their state is being published.
|
||||
if entity.device.provides_availability:
|
||||
config["availability"] = [{"topic": avail_topic}]
|
||||
config["availability_mode"] = "all"
|
||||
|
||||
# Component-specific fields
|
||||
if entity.component == "binary_sensor":
|
||||
# "online" binary sensor: payload ON/OFF
|
||||
|
||||
Reference in New Issue
Block a user