Skip to content

Standardize power meter EnergySensor name and entity ID across HA versions - #1112

Open
Moustachauve wants to merge 6 commits into
dvd-dev:mainfrom
Moustachauve:fix/power-meter-energy-sensor-name
Open

Moustachauve wants to merge 6 commits into
dvd-dev:mainfrom
Moustachauve:fix/power-meter-energy-sensor-name

Conversation

@Moustachauve

Copy link
Copy Markdown
Contributor

Note

This PR is stacked on top of #1107.

Previously, EnergySensor set _attr_name to the internal constant "hilo_energy_total" on Meter devices so older Home Assistant versions (<= 2026.2, Python 3.13) would derive the entity ID sensor.hilo_energy_total required by UtilityManager, HiloCostTotalSensor, and check_tarif. Starting in Home Assistant 2026.3+ (Python 3.14), the entity registry always prepends the device name when _attr_name does not already begin with the device name, causing the meter energy sensor to be created with a device-prefixed entity ID (such as sensor.smartenergymeter_hilo_energy_total) and divergent friendly_name values across Python 3.13 and 3.14. In addition, the migration in __init__.py ran after platform setup and only checked the hardcoded "sensor.meter00_hilo_energy_total" entity ID, leaving meters with any other device name unmigrated.

Use f"{device.name} Hilo Energy" as the entity name for all EnergySensor instances so the display name starts with the device name and remains identical across Home Assistant versions, while setting self.entity_id = f"sensor.{HILO_ENERGY_TOTAL}" on Meter devices so new installs register sensor.hilo_energy_total directly during platform setup. Expand energy_entity_renames in async_setup_entry to dynamically include all Meter devices and run it before forwarding platform setups so existing installations on Home Assistant 2026.3+ migrate to sensor.hilo_energy_total before UtilityManager and check_tarif run.

EnergySensor previously subclassed Home Assistant Core's private
IntegrationSensor class directly to estimate cumulative energy from
device power readings. Because IntegrationSensor is an internal helper
entity rather than a public base class, upstream constructor changes in
Home Assistant 2026.2 and 2026.11 repeatedly broke Hilo setups and
required fragile version-checking branches, entity registry workarounds
to resolve the source power entity_id, and state machine listeners.

Have EnergySensor inherit directly from HiloEntity and RestoreSensor
instead, computing the Left-Riemann sum in-process whenever the
underlying HiloDevice updates (via SignalR or coordinator polling) as
well as on a 5-minute fallback timer for constant-power periods.
Accumulating directly from the in-memory device power attribute removes
all coupling to homeassistant.components.integration, preserves existing
entity unique_ids and restored states (including seamless fallback for
states persisted by IntegrationSensor), and retains full Decimal
precision across intervals while rounding native_value to 2 decimal
places.
tests/test_sensor.py previously constructed mock Hilo and HiloDevice
instances manually in each test and asserted individual entity metadata
properties in Python code.

Use setup_with_selected_platforms with Platform.SENSOR and Syrupy
snapshots (snapshot_platform) instead, matching the testing pattern used
across the switch and light platform tests while exercising the full
sensor platform setup and retaining 100% coverage of EnergySensor.
Home Assistant 2026.3+ (Python 3.14) prepends the device name to
original_name and friendly_name on entities whose name does not already
start with the device name (such as Gateway sensors), causing full-
platform snapshots to diverge from Home Assistant 2026.2 (Python 3.13).
Additionally, leaving invalid power values on the shared Smart Plug
device at test teardown caused PowerSensor.state to raise during the
Python 3.13 teardown coordinator refresh.

Scope test_energy_sensor_entities snapshots to the EnergySensor entities
and restore valid device power before teardown so the test suite passes
on both Python 3.13 and Python 3.14.
…sions

Previously, EnergySensor set _attr_name to the internal constant
"hilo_energy_total" on Meter devices so older Home Assistant versions
(<= 2026.2, Python 3.13) would derive the entity ID
sensor.hilo_energy_total required by UtilityManager,
HiloCostTotalSensor, and check_tarif. Starting in Home Assistant
2026.3+ (Python 3.14), the entity registry always prepends the device
name when _attr_name does not already begin with the device name,
causing the meter energy sensor to be created with a device-prefixed
entity ID (such as sensor.smartenergymeter_hilo_energy_total) and
divergent friendly_name values across Python 3.13 and 3.14. In
addition, the migration in __init__.py ran after platform setup and
only checked the hardcoded "sensor.meter00_hilo_energy_total" entity
ID, leaving meters with any other device name unmigrated.

Use f"{device.name} Hilo Energy" as the entity name for all
EnergySensor instances so the display name starts with the device name
and remains identical across Home Assistant versions, while setting
self.entity_id = f"sensor.{HILO_ENERGY_TOTAL}" on Meter devices so new
installs register sensor.hilo_energy_total directly during platform
setup. Expand energy_entity_renames in async_setup_entry to
dynamically include all Meter devices and run it before forwarding
platform setups so existing installations on Home Assistant 2026.3+
migrate to sensor.hilo_energy_total before UtilityManager and
check_tarif run.
@Moustachauve
Moustachauve force-pushed the fix/power-meter-energy-sensor-name branch from 3cf6eaa to 9e51433 Compare October 30, 2026 01:49
@Moustachauve
Moustachauve marked this pull request as ready for review October 30, 2026 01:51
ic-dev21 and others added 2 commits November 8, 2026 19:29
Commit 8803d86 (PR dvd-dev#1001) added "Disconnected": false to the
Thermostat 1 fixture entry in tests/fixtures/all_devices.json so the
baseboard thermostat starts online during test setup. As a result,
sensor.thermostat_1_hilo_energy initializes to "0.00" rather than
"unavailable".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants