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:
2026-06-24 11:54:49 +02:00
parent e8521351d7
commit 7e61b0a938
3 changed files with 51 additions and 2 deletions
+23
View File
@@ -648,6 +648,29 @@ def test_build_discovery_payload_no_index_error_for_energy_entities(energy_db) -
assert "energy-cost" in config["device"]["identifiers"]
def test_energy_cost_entities_omit_availability_so_ha_shows_them(energy_db) -> None:
"""The energy-cost device has no online/offline heartbeat, so its discovery
config must NOT declare an availability topic — otherwise HA marks the
entities ``unavailable`` forever even though state is being published.
"""
from app.integrations.expose import build_catalog
from app.services.ha_discovery import build_discovery_payload
with Session(energy_db) as session:
catalog = build_catalog(session)
energy_entries = [e for e in catalog if e.entity.key.startswith("energy.")]
assert len(energy_entries) == 4
for entry in energy_entries:
assert entry.entity.device.provides_availability is False
_, config = build_discovery_payload(entry.entity, "homeassistant")
assert "availability" not in config, (
f"energy entity {entry.entity.key} must omit availability (always-available)"
)
assert "availability_mode" not in config
def test_energy_entity_discovery_topics_contain_correct_node_id() -> None:
"""Discovery topic node_id for energy entities must be 'energy-cost' (hyphens → underscores)."""
from app.integrations.expose import DeviceInfo, ExposableEntity