♻️ Use has_entity_name + suffix-only entity names #46

Merged
mat merged 1 commit from refactor/has-entity-name into main 2026-07-10 15:07:12 +00:00
Owner

Why

All Comwatt entities built their display name by hand: _attr_name = f"{device['name']} Power". The modern HA convention is has_entity_name = True + a suffix-only name, letting HA prepend the device name for both the display name and the entity_id slug.

Bug fixed along the way

ComwattSwitch already had _attr_has_entity_name = True but kept _attr_name = f"{device['name']} Switch". So HA prefixed the device name a second time:

before after
switch entity_id switch.relay_relay_switch switch.relay_switch

The switch tests looked the entity up dynamically, so the doubled name went unnoticed.

Changes

  • ComwattSensor base: _attr_has_entity_name = True (covers the three sensor subclasses); each _attr_name drops the device/site prefix → "Power", "Total Energy", description.friendly_suffix.
  • ComwattSwitch: _attr_name = "Switch" (already had the flag).

No registry churn

Sensor entity_ids are unchanged — the device name still enters the slug via HA's has_entity_name path:

entity before after
sensor.relay_power Relay Power (flag=False) Power (flag=True) → same id
sensor.home_auto_production_rate Home Auto Production Rate Auto Production Rate → same id

Verified empirically against HA 2026.2.3. The switch entity_id corrects from the doubled slug to the clean one for new installs; existing registry entries keep their persisted entity_id.

Test plan

  • pytest — 32 passed
  • ruff + mypy clean
  • Added regression assertion locking switch.relay_switch + original_name == "Switch"
## Why All Comwatt entities built their display name by hand: `_attr_name = f"{device['name']} Power"`. The modern HA convention is `has_entity_name = True` + a suffix-only name, letting HA prepend the device name for both the **display name** and the **entity_id slug**. ## Bug fixed along the way `ComwattSwitch` already had `_attr_has_entity_name = True` **but** kept `_attr_name = f"{device['name']} Switch"`. So HA prefixed the device name a second time: | | before | after | |---|---|---| | switch entity_id | `switch.relay_relay_switch` | `switch.relay_switch` | The switch tests looked the entity up dynamically, so the doubled name went unnoticed. ## Changes - `ComwattSensor` base: `_attr_has_entity_name = True` (covers the three sensor subclasses); each `_attr_name` drops the device/site prefix → `"Power"`, `"Total Energy"`, `description.friendly_suffix`. - `ComwattSwitch`: `_attr_name = "Switch"` (already had the flag). ## No registry churn Sensor entity_ids are unchanged — the device name still enters the slug via HA's `has_entity_name` path: | entity | before | after | |---|---|---| | `sensor.relay_power` | `Relay Power` (flag=False) | `Power` (flag=True) → same id | | `sensor.home_auto_production_rate` | `Home Auto Production Rate` | `Auto Production Rate` → same id | Verified empirically against HA 2026.2.3. The switch entity_id corrects from the doubled slug to the clean one **for new installs**; existing registry entries keep their persisted entity_id. ## Test plan - [x] pytest — 32 passed - [x] ruff + mypy clean - [x] Added regression assertion locking `switch.relay_switch` + `original_name == "Switch"`
♻️ Use has_entity_name + suffix-only entity names
Some checks are pending
Validate / lint-ruff (push) Waiting to run
Validate / test-pytest (push) Waiting to run
Validate / type-check-mypy (push) Waiting to run
Validate / lint-ruff (pull_request) Waiting to run
Validate / test-pytest (pull_request) Waiting to run
Validate / type-check-mypy (pull_request) Waiting to run
6685f4f918
Adopt Home Assistant's has_entity_name=True convention: the entity
stores only its own suffix ("Power", "Switch", "Auto Production Rate")
and HA prepends the device name for both the display name and the
entity_id slug.

- ComwattSensor base gets _attr_has_entity_name = True (covers the three
  sensor subclasses); each _attr_name drops the device/site prefix.
- ComwattSwitch already had the flag but kept f"{device['name']} Switch",
  so HA prefixed the device name again → switch.relay_relay_switch.
  Now "Switch" → switch.relay_switch (fixes the doubled name).

Entity ids are unchanged for sensors (device name still enters the slug
via HA), so no registry churn for existing installs. The switch entity_id
corrects from the doubled slug to the clean one for new installs; existing
registry entries keep their persisted entity_id.

Adds a regression assertion locking switch.relay_switch + original_name.
mat changed target branch from feat/40-site-metrics to main 2026-07-10 14:59:01 +00:00
mat force-pushed refactor/has-entity-name from 6685f4f918
Some checks are pending
Validate / lint-ruff (push) Waiting to run
Validate / test-pytest (push) Waiting to run
Validate / type-check-mypy (push) Waiting to run
Validate / lint-ruff (pull_request) Waiting to run
Validate / test-pytest (pull_request) Waiting to run
Validate / type-check-mypy (pull_request) Waiting to run
to d58eb9fce1
All checks were successful
Validate / lint-ruff (push) Successful in 7s
Validate / test-pytest (push) Successful in 3m0s
Validate / type-check-mypy (push) Successful in 3m7s
Validate / lint-ruff (pull_request) Successful in 7s
Validate / test-pytest (pull_request) Successful in 3m0s
Validate / type-check-mypy (pull_request) Successful in 3m12s
2026-07-10 15:03:09 +00:00
Compare
mat merged commit 610dd75533 into main 2026-07-10 15:07:12 +00:00
mat deleted branch refactor/has-entity-name 2026-07-10 15:07:12 +00:00
mat referenced this pull request from a commit 2026-07-10 18:06:30 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mat/homeassistant-comwatt!46
No description provided.