FUE-T05: anchor energy-cost HA device identity to active meter (uuid id, label name, empty when none)
This commit is contained in:
+61
-13
@@ -373,12 +373,36 @@ register_provider(_modbus_provider)
|
||||
def _energy_cost_provider(session: Session) -> list[ExposableEntity]:
|
||||
"""Enumerate ExposableEntity objects for the energy cost subsystem.
|
||||
|
||||
Produces 4 sensor entities grouped under a single HA device "Energy Cost":
|
||||
Produces 6 sensor entities grouped under a single HA device whose identity
|
||||
is anchored to the **current active electricity meter**:
|
||||
|
||||
- ``buy_price_now`` — current effective buy price (EUR/kWh or local currency).
|
||||
- ``sell_price_now`` — current effective sell price (EUR/kWh or local currency).
|
||||
- ``import_cost_total`` — cumulative import cost (total_increasing, monetary).
|
||||
- ``export_revenue_total`` — cumulative export revenue (total_increasing, monetary).
|
||||
- ``import_cost_total`` — cumulative import cost (total, monetary).
|
||||
- ``export_revenue_total`` — cumulative export revenue (total, monetary).
|
||||
- ``import_cost_today`` — today's import cost (total_increasing, monetary).
|
||||
- ``export_revenue_today`` — today's export revenue (total_increasing, monetary).
|
||||
|
||||
Active meter requirement
|
||||
------------------------
|
||||
**If no active electricity meter exists, the provider returns ``[]``.**
|
||||
No energy-cost entities are exposed to HA until a meter has been declared.
|
||||
This prevents spurious sensor creation with an undefined device identity.
|
||||
|
||||
HA device identity (换表 → 新 sensor)
|
||||
--------------------------------------
|
||||
``identifiers[1]`` is set to the active meter's **uuid** (not the fixed
|
||||
string ``"energy-cost"``). ``ha_discovery.py`` uses ``identifiers[1]`` as
|
||||
the MQTT node_id and as part of the ``unique_id`` for every entity.
|
||||
Declaring a new active electricity meter produces a new uuid → new node_id /
|
||||
unique_id → HA creates a brand-new sensor, cleanly isolating post-swap data.
|
||||
|
||||
Entity key stability
|
||||
--------------------
|
||||
Entity keys remain the fixed stable strings (``"energy.buy_price_now"`` etc.),
|
||||
**not** derived from the meter uuid. The ``exposed_entity_toggle`` table uses
|
||||
keys as its primary handle; keeping them stable means toggled-on entities stay
|
||||
enabled after a meter swap without requiring the user to re-tick them.
|
||||
|
||||
Current-price algorithm (source-agnostic, with fallback)
|
||||
---------------------------------------------------------
|
||||
@@ -399,8 +423,9 @@ def _energy_cost_provider(session: Session) -> list[ExposableEntity]:
|
||||
Cumulative totals
|
||||
-----------------
|
||||
``SUM(import_cost)`` and ``SUM(export_revenue)`` over **all non-degraded**
|
||||
``energy_cost_period`` rows. Degraded rows carry 0 costs and are excluded
|
||||
to avoid double-counting when they are later overwritten by real values.
|
||||
``energy_cost_period`` rows within the current meter's window. Degraded rows
|
||||
carry 0 costs and are excluded to avoid double-counting when they are later
|
||||
overwritten by real values.
|
||||
|
||||
Currency
|
||||
--------
|
||||
@@ -414,16 +439,37 @@ def _energy_cost_provider(session: Session) -> list[ExposableEntity]:
|
||||
- ``"energy.sell_price_now"``
|
||||
- ``"energy.import_cost_total"``
|
||||
- ``"energy.export_revenue_total"``
|
||||
- ``"energy.import_cost_today"``
|
||||
- ``"energy.export_revenue_today"``
|
||||
|
||||
DeviceInfo identifiers
|
||||
----------------------
|
||||
**Two-element tuple** ``("energy-cost", "energy-cost")`` so that
|
||||
``ha_discovery.py``'s ``entity.device.identifiers[1]`` is always valid
|
||||
(the service uses index [1] as the node_id throughout).
|
||||
**Two-element tuple** ``("energy-cost", meter.uuid)`` so that
|
||||
``ha_discovery.py``'s ``entity.device.identifiers[1]`` resolves to the
|
||||
meter uuid (used as the MQTT node_id and unique_id seed throughout).
|
||||
"""
|
||||
from app.models.energy import EnergyCostPeriod # local import to avoid circular
|
||||
from app.models.energy import EnergyCostPeriod, Meter # local import to avoid circular
|
||||
from sqlalchemy import select
|
||||
|
||||
# --- Determine currency and representative pricing from the latest non-degraded row ---
|
||||
# --- Require an active electricity meter; return [] if none exists ---
|
||||
# Using an inline query (ended_at IS NULL) rather than a service-layer helper
|
||||
# to avoid a new public dependency and remain consistent with the value_getter
|
||||
# implementations below (which use the same inline pattern).
|
||||
active_meter: Meter | None = session.execute(
|
||||
select(Meter)
|
||||
.where(
|
||||
Meter.commodity == "electricity",
|
||||
Meter.ended_at.is_(None),
|
||||
)
|
||||
.limit(1)
|
||||
).scalar_one_or_none()
|
||||
|
||||
if active_meter is None:
|
||||
# No active electricity meter → do not expose any energy-cost entities.
|
||||
# HA will not see these sensors until a meter is declared.
|
||||
return []
|
||||
|
||||
# --- Determine currency from the latest non-degraded row ---
|
||||
|
||||
latest_period: EnergyCostPeriod | None = (
|
||||
session.query(EnergyCostPeriod)
|
||||
@@ -436,13 +482,15 @@ def _energy_cost_provider(session: Session) -> list[ExposableEntity]:
|
||||
if latest_period is not None and latest_period.currency:
|
||||
currency = latest_period.currency
|
||||
|
||||
# --- Shared DeviceInfo (2-element identifiers — required by ha_discovery.py [1] access) ---
|
||||
# --- Shared DeviceInfo anchored to the active meter's uuid ---
|
||||
# identifiers[1] = meter.uuid drives the MQTT node_id and unique_id in
|
||||
# ha_discovery.py. Swapping the meter produces a new uuid → new HA sensor.
|
||||
# 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",
|
||||
identifiers=("energy-cost", active_meter.uuid),
|
||||
name=f"Energy Cost ({active_meter.label})",
|
||||
provides_availability=False,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user