Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file.
the panel to load and draws the panel once it has loaded**, where it showed "Unable to connect to SPAN Panel" until Retry was pressed or the dashboard was
opened again.

- **An error from a breaker, circuit priority, charger or adopted-device control now names the circuit or device by the name you gave it in Home Assistant**,
where it kept the name the panel or the integration gave it.

## [2.3.3] - 11/2026

**Install 2.3.3 before your panel updates to firmware r202645**, which gives the panel's circuits new device IDs: 2.3.3 carries every circuit entity, device,
Expand Down
39 changes: 29 additions & 10 deletions custom_components/span_panel/adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def adopted_device_info(
identifiers={(DOMAIN, identifier)},
name=adopted_device_label(device),
manufacturer=device.vendor_name or "Unknown",
model=device.model or humanised(device.device_type.rsplit(".", 1)[-1]),
model=device.model or _type_name(device.device_type),
serial_number=device.serial_number,
sw_version=device.software_version,
hw_version=device.hardware_version,
Expand All @@ -373,12 +373,30 @@ def adopted_device_info(


def adopted_device_label(device: AdoptedDevice) -> str:
"""Return the name this device is known by, on its card and in its errors.

One function because the two have to agree: an error naming a device the
user cannot find in their device list is an error they cannot act on.
"""Return the name this device's card is registered under, taken from the wire.

It is what the device list shows until the user renames the device there.
Whatever names the device to a person names the card as Home Assistant
shows it instead (`util.device_display_name`): the errors its controls
raise, and the curation editor's heading, which falls back to this for a
device that has no card yet. An error naming a device the user cannot find
in their device list is an error they cannot act on.

Never empty: the device's own name where it publishes one, else the last
non-empty segment of its `$type`, else its wire id. The type comes from a
publisher this integration does not model and can be malformed. The
library skips a device declaring none, but one ending in a dot has nothing
after it, and an empty name is worse than none: Core names a new card
after the panel's entry, a device that is not the panel, and blanks it on
the next load, leaving the user nothing to find it by.
"""
return device.name or humanised(device.device_type.rsplit(".", 1)[-1])
return device.name or _type_name(device.device_type) or device.device_id


def _type_name(device_type: str) -> str | None:
"""Return the last non-empty segment of a dotted `$type`, humanised, or None where it has none."""
segment = next((part for part in reversed(device_type.split(".")) if part), None)
return None if segment is None else humanised(segment)


def humanised(wire_token: str) -> str:
Expand Down Expand Up @@ -626,7 +644,6 @@ def __init__(
)
self._node_id = declaration.node_id
self._property_id = declaration.property_id
self._device_label = adopted_device_label(device)

async def _publish(self, value: str) -> None:
"""Write one value and refresh, reporting both ways it can fail to happen.
Expand All @@ -641,8 +658,10 @@ async def _publish(self, value: str) -> None:
The messages name the property and the device rather than the circuit or
the charger the curated keys name, because that is the whole identity an
adopted device has: this integration models nothing about it beyond its
declaration, and the wire vocabulary is what the user will find in the
panel's own tree.
declaration. The property is named in the wire vocabulary, which is what
the user will find in the panel's own tree. The device is named as their
device list names it, which after a rename is not the wire's name -- see
`SpanPanelEntity._displayed_device_name`.

No `hasattr` guard, unlike the curated controls. Those ask because a
transport may not implement an optional protocol at all; this entity only
Expand All @@ -658,7 +677,7 @@ async def _publish(self, value: str) -> None:
not_delivered_key="adopted_control_not_delivered",
placeholders={
"property": f"{self._node_id}/{self._property_id}",
"device": self._device_label,
"device": self._displayed_device_name(),
},
)
await self.coordinator.async_request_refresh()
Expand Down
21 changes: 19 additions & 2 deletions custom_components/span_panel/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from .field_paths import FieldPathDeclarationMixin
from .id_builder import build_circuit_device_identifier
from .naming import circuit_object_id_suffix
from .util import circuit_device_info, snapshot_to_device_info
from .util import circuit_device_info, device_display_name, snapshot_to_device_info

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -157,7 +157,9 @@ async def _async_control(
user sees is `failed_key` / `not_delivered_key` with `placeholders`,
which is why the reason is added here rather than at each call site: it
is the one placeholder every one of these messages carries, and the one
a caller cannot know before the call.
a caller cannot know before the call. A placeholder that names the
circuit or device being commanded is `_displayed_device_name`'s answer,
so the message names it as the user's device list does.

Returns the outcome for a caller that has something to say about a
delivered command -- `no_op`, say, or the topic it went to.
Expand All @@ -183,6 +185,21 @@ async def _async_control(

return outcome

def _displayed_device_name(self) -> str:
"""Return this entity's device as Home Assistant shows it, for a message that names it.

The user's name for the device, else the one it is registered under
(`util.device_display_name`), read from the entity's `device_entry` when
the message is built, so a rename made since setup is the name it
carries. An added control always has a named device: Core links the
device before it adds the entity, and every device a control sits on is
registered under a name.
"""
device = self.device_entry
if device is None or not (name := device_display_name(device)):
raise RuntimeError(f"{self.entity_id} has no named device to report")
return name

async def async_added_to_hass(self) -> None:
"""Tell the coordinator which snapshot fields this entity reads.

Expand Down
17 changes: 8 additions & 9 deletions custom_components/span_panel/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,14 @@ def circuit_device_name(circuit: SpanCircuitSnapshot, circuit_id: str) -> str:


def construct_circuit_label(circuit: SpanCircuitSnapshot | None, circuit_id: str) -> str:
"""Name a circuit the way its owner knows it, for a message addressed to them.

The panel's own name where there is one, and the tab identifier otherwise --
which is what the entity itself was named from, so an error names the thing
the user is looking at rather than a wire UUID. A meter outside the panel
has no tabs, and takes `outside_meter_label`, as its own device does.
`None` is accepted because a circuit can drop out of a snapshot between a
command and its failure, and an error message is the worst possible place
to raise a second error.
"""Name a circuit by the panel's name for it, for a message addressed to its owner.

The panel's own name where there is one, and the tab identifier otherwise,
so a message names a circuit the user knows rather than a wire UUID. A
meter outside the panel has no tabs, and takes `outside_meter_label`, as
its own device does. `None` is accepted for a circuit the snapshot does
not hold, which `construct_shared_with_attribute` passes for a peer the
snapshot lacks.
"""
if circuit is not None and circuit.measures_outside_panel:
return outside_meter_label(circuit_id)
Expand Down
7 changes: 4 additions & 3 deletions custom_components/span_panel/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(
evse = snapshot.evse.get(evse_id, EMPTY_EVSE)
use_circuit_numbers = data_coordinator.config_entry.options.get(USE_CIRCUIT_NUMBERS, False)
display_suffix = resolve_evse_display_suffix(evse, snapshot, use_circuit_numbers)
# Named in the errors a person reads; `_evse_id` is the wire's node id.
# Named in the log line of each command; `_evse_id` is the wire's node id.
self._charger_name = evse_display_name(evse, panel_name, display_suffix)

self._attr_device_info = evse_device_info(
Expand Down Expand Up @@ -158,14 +158,15 @@ async def _async_set(self, *, locked: bool) -> None:
"""Ask the panel to move the lock, through the control path every control shares.

A refusal and an undelivered command both reach the caller; the state
stays what the charger reports until it republishes.
stays what the charger reports until it republishes. Either names the
charger as its card is shown, the user's name for it included.
"""
await self._async_control(
self._description.set_fn(self.coordinator.client, self._evse_id, locked),
command=f"a lock command for {self._charger_name}",
failed_key="evse_lock_failed",
not_delivered_key="evse_lock_not_delivered",
placeholders={"charger": self._charger_name},
placeholders={"charger": self._displayed_device_name()},
)
await self.coordinator.async_request_refresh()

Expand Down
10 changes: 6 additions & 4 deletions custom_components/span_panel/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ def __init__(
use_circuit_numbers = data_coordinator.config_entry.options.get(USE_CIRCUIT_NUMBERS, False)
display_suffix = resolve_evse_display_suffix(evse, snapshot, use_circuit_numbers)

# The charger's own name, kept because both ways this control can fail
# are reported to a person. `_evse_id` is the node id the wire uses; it
# names nothing the user can see on their dashboard.
# The charger's own name, for the log line of each command. `_evse_id` is
# the node id the wire uses; it names nothing the user can see on their
# dashboard.
self._charger_name = evse_display_name(evse, panel_name, display_suffix)

self._attr_device_info = evse_device_info(
Expand Down Expand Up @@ -301,14 +301,16 @@ async def async_set_native_value(self, value: float) -> None:
`_async_control` every control in this integration shares. A refusal
never resolved an address; a `FAILED` outcome resolved one and was never
handed over, which the library promises means it will not arrive later.
Either names the charger as its card is shown, the user's name for it
included.
"""
client = self.coordinator.client
await self._async_control(
self._description.set_fn(client, self._evse_id, int(value)),
command=f"a charge-current limit for {self._charger_name}",
failed_key="evse_charge_limit_failed",
not_delivered_key="evse_charge_limit_not_delivered",
placeholders={"charger": self._charger_name},
placeholders={"charger": self._displayed_device_name()},
)

await self.coordinator.async_request_refresh()
Expand Down
6 changes: 4 additions & 2 deletions custom_components/span_panel/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
circuit_device_name,
circuit_has_a_priority_select,
construct_circuit_identifier_from_tabs,
construct_circuit_label,
construct_tabs_attribute,
construct_voltage_attribute,
remove_withdrawn_controls,
Expand Down Expand Up @@ -240,6 +239,9 @@ async def async_select_option(self, option: str) -> None:
resolved an address and was never handed to the broker, which the
library promises means it will not arrive later either. Both live in
`_async_control`, which every control in this integration shares.

The circuit is named as its own device is shown, the user's name for it
included.
"""
_LOGGER.debug("Selecting option: %s", option)
client = self.coordinator.client
Expand All @@ -250,7 +252,7 @@ async def async_select_option(self, option: str) -> None:
command=f"a priority change for {self.entity_id}",
failed_key="circuit_priority_failed",
not_delivered_key="circuit_priority_not_delivered",
placeholders={"circuit": construct_circuit_label(self._get_circuit(), self.id)},
placeholders={"circuit": self._displayed_device_name()},
)

await self.coordinator.async_request_refresh()
Expand Down
11 changes: 4 additions & 7 deletions custom_components/span_panel/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
circuit_device_name,
circuit_has_a_breaker_switch,
construct_circuit_identifier_from_tabs,
construct_circuit_label,
construct_shared_with_attributes,
construct_tabs_attribute,
construct_voltage_attribute,
Expand Down Expand Up @@ -316,19 +315,17 @@ async def _async_set_relay(self, state: str, *, is_on: bool) -> None:
address, and a `FAILED` outcome that resolved one and was never handed
over -- are raised by `_async_control`, which every control in this
integration shares.

The circuit is named as its own device is shown, the user's name for it
included.
"""
client = self.coordinator.client
await self._async_control(
client.set_circuit_relay(self._circuit_id, state),
command=f"a relay command for {self.entity_id}",
failed_key="circuit_relay_failed",
not_delivered_key="circuit_relay_not_delivered",
placeholders={
"circuit": construct_circuit_label(
self.coordinator.data.circuits.get(self._circuit_id),
self._circuit_id,
)
},
placeholders={"circuit": self._displayed_device_name()},
)

self._attr_is_on = is_on
Expand Down
17 changes: 10 additions & 7 deletions custom_components/span_panel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,16 @@ def evse_display_name(
panel_name: str,
display_suffix: str | None = None,
) -> str:
"""Return what this charger is called wherever a person reads about it.

Separate from the builder below because a charger is named in two places
that have to agree: its device card, and any message about it. The wire
calls a charger by its node id, which is what the panel needs and what
nobody reading a notification can act on -- so anything user-facing asks
here instead.
"""Return the name a charger's device card is registered under.

It is what the device list shows until the user renames the charger there.
The errors its controls raise name the card as Home Assistant shows it
(`SpanPanelEntity._displayed_device_name`) rather than by this. Separate
from the builder below because those controls keep it as `_charger_name`,
to name the charger in the log line of each command they send
(`command=`). The wire calls a charger by its node id, which is what the
panel needs and what nobody reading a message can act on, so nothing
user-facing names it by that.
"""
base_name = evse.model or "EV Charger"
name = f"{base_name} ({display_suffix})" if display_suffix else base_name
Expand Down
45 changes: 45 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from unittest.mock import AsyncMock, MagicMock, patch

from homeassistant import data_entry_flow
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
Expand All @@ -28,6 +29,7 @@
from custom_components.span_panel.curation import CurationOverlay
from custom_components.span_panel.sensor_definitions import PANEL_ENERGY_SENSORS
from custom_components.span_panel.sensor_panel import SpanPanelEnergySensor
from custom_components.span_panel.switch import SpanPanelCircuitsSwitch
from custom_components.span_panel.util import circuit_device_info

from .factories import SpanPanelSnapshotFactory, pv_binding_for
Expand Down Expand Up @@ -306,6 +308,49 @@ def coordinator_stand_in(
return coordinator, entry


async def add_through_platform(
hass: HomeAssistant, entry: ConfigEntry, domain: str, entity: Entity
) -> MockEntityPlatform:
"""Add one entity through a real platform, as setup adds it, and return the platform.

Core links the entity to its device as it adds it, and that `device_entry`
is what a control names its device by in an error. An entity that was only
constructed has none, so a test that operates a control adds it here first.

`entry` must be added to `hass` with the panel's device registered, as
`coordinator_stand_in` leaves it: the registry refuses a device for an
unknown entry, and Core drops an entity whose device names a parent that
does not exist, logging an error rather than raising.
"""
platform = MockEntityPlatform(
hass, domain=domain, platform_name=DOMAIN, logger=logging.getLogger(__name__)
)
platform.config_entry = entry
await platform.async_add_entities([entity])
await hass.async_block_till_done()
assert entity.device_entry is not None, f"the platform did not add {entity.unique_id}"
return platform


async def added_circuit_switch(
hass: HomeAssistant, circuit: SpanCircuitSnapshot
) -> tuple[MagicMock, SpanPanelCircuitsSwitch]:
"""Return a circuit's breaker switch, added through a real platform, and its coordinator.

The coordinator is `coordinator_stand_in`'s on a panel holding `circuit`,
with an async client and refresh for the switch to write through. Added
because a breaker error names the circuit by its device, which Core links
only as it adds the switch.
"""
snapshot = SpanPanelSnapshotFactory.create(circuits={circuit.circuit_id: circuit})
coordinator, entry = coordinator_stand_in(hass, snapshot)
coordinator.client = AsyncMock()
coordinator.async_request_refresh = AsyncMock()
switch = SpanPanelCircuitsSwitch(coordinator, snapshot, circuit.circuit_id, PANEL_NAME)
await add_through_platform(hass, entry, "switch", switch)
return coordinator, switch


@dataclass(frozen=True, slots=True)
class MainMeterConsumedEnergy:
"""The main meter's Consumed Energy, live on a real sensor platform, and what feeds it."""
Expand Down
Loading