Skip to content

Commit 39af964

Browse files
committed
Add device prefix to new installs only
1 parent 84fb419 commit 39af964

File tree

7 files changed

+65
-18
lines changed

7 files changed

+65
-18
lines changed

custom_components/span_panel/binary_sensor.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1616

1717
from .const import (COORDINATOR, DOMAIN, SYSTEM_DOOR_STATE_CLOSED,
18-
SYSTEM_DOOR_STATE_OPEN)
18+
SYSTEM_DOOR_STATE_OPEN, USE_DEVICE_PREFIX)
1919
from .coordinator import SpanPanelCoordinator
2020
from .span_panel import SpanPanel
2121
from .span_panel_hardware_status import SpanPanelHardwareStatus
@@ -82,11 +82,22 @@ def __init__(
8282
span_panel: SpanPanel = data_coordinator.data
8383

8484
self.entity_description = description
85-
self._attr_name = f"{description.name}"
85+
device_info = panel_to_device_info(span_panel)
86+
self._attr_device_info = device_info
87+
base_name = f"{description.name}"
88+
89+
if (data_coordinator.config_entry is not None and
90+
data_coordinator.config_entry.options.get(USE_DEVICE_PREFIX, False) and
91+
device_info is not None and
92+
isinstance(device_info, dict) and
93+
"name" in device_info):
94+
self._attr_name = f"{device_info['name']} {base_name}"
95+
else:
96+
self._attr_name = base_name
97+
8698
self._attr_unique_id = (
8799
f"span_{span_panel.status.serial_number}_{description.key}"
88100
)
89-
self._attr_device_info = panel_to_device_info(span_panel)
90101

91102
_LOGGER.debug("CREATE BINSENSOR [%s]", self._attr_name)
92103

custom_components/span_panel/config_flow.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from homeassistant.helpers.httpx_client import get_async_client
1818
from homeassistant.util.network import is_ipv4_address
1919

20-
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
20+
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, USE_DEVICE_PREFIX
2121
from .options import (BATTERY_ENABLE, INVERTER_ENABLE, INVERTER_LEG1,
2222
INVERTER_LEG2)
2323
from .span_panel_api import SpanPanelApi
@@ -334,7 +334,14 @@ def create_new_entry(
334334
Creates a new SPAN panel entry.
335335
"""
336336
return self.async_create_entry(
337-
title=serial_number, data={CONF_HOST: host, CONF_ACCESS_TOKEN: access_token}
337+
title=serial_number,
338+
data={
339+
CONF_HOST: host,
340+
CONF_ACCESS_TOKEN: access_token
341+
},
342+
options={
343+
USE_DEVICE_PREFIX: True # Only set for new installations
344+
}
338345
)
339346

340347
def update_existing_entry(
@@ -407,6 +414,10 @@ async def async_step_init(
407414
) -> ConfigFlowResult:
408415
"""Manage the options."""
409416
if user_input is not None:
417+
# Preserve the USE_DEVICE_PREFIX setting from the original entry
418+
use_prefix = self.entry.options.get(USE_DEVICE_PREFIX, False)
419+
if use_prefix:
420+
user_input[USE_DEVICE_PREFIX] = use_prefix
410421
return self.async_create_entry(title="", data=user_input)
411422

412423
defaults = {

custom_components/span_panel/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
MAIN_RELAY_STATE = "mainRelayState"
4747

4848
PANEL_MAIN_RELAY_STATE_UNKNOWN_VALUE = "UNKNOWN"
49+
USE_DEVICE_PREFIX = "use_device_prefix"
4950

5051
DEFAULT_SCAN_INTERVAL = timedelta(seconds=15)
5152
API_TIMEOUT = 30

custom_components/span_panel/select.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1010
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1111

12-
from .const import COORDINATOR, DOMAIN, CircuitPriority
12+
from .const import COORDINATOR, DOMAIN, USE_DEVICE_PREFIX, CircuitPriority
1313
from .coordinator import SpanPanelCoordinator
1414
from .span_panel import SpanPanel
1515
from .util import panel_to_device_info
@@ -37,7 +37,10 @@ def __init__(self, coordinator: SpanPanelCoordinator, id: str, name: str) -> Non
3737
def name(self):
3838
"""Return the switch name."""
3939
span_panel: SpanPanel = self.coordinator.data
40-
return f"{span_panel.circuits[self.id].name} Circuit Priority"
40+
base_name = f"{span_panel.circuits[self.id].name} Circuit Priority"
41+
if self.coordinator.config_entry.options.get(USE_DEVICE_PREFIX, False):
42+
return f"{self._attr_device_info['name']} {base_name}"
43+
return base_name
4144

4245
@cached_property
4346
def options(self) -> list[str]:

custom_components/span_panel/sensor.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from .const import (CIRCUITS_ENERGY_CONSUMED, CIRCUITS_ENERGY_PRODUCED,
2020
CIRCUITS_POWER, COORDINATOR, CURRENT_RUN_CONFIG, DOMAIN,
2121
DSM_GRID_STATE, DSM_STATE, MAIN_RELAY_STATE,
22-
STATUS_SOFTWARE_VER, STORAGE_BATTERY_PERCENTAGE)
22+
STATUS_SOFTWARE_VER, STORAGE_BATTERY_PERCENTAGE,
23+
USE_DEVICE_PREFIX)
2324
from .coordinator import SpanPanelCoordinator
2425
from .options import BATTERY_ENABLE, INVERTER_ENABLE
2526
from .span_panel import SpanPanel
@@ -258,11 +259,22 @@ def __init__(
258259
"""Initialize Span Panel Sensor base entity."""
259260
super().__init__(data_coordinator, context=description)
260261
self.entity_description = description
261-
self._attr_name = f"{description.name}"
262+
device_info = panel_to_device_info(span_panel)
263+
self._attr_device_info = device_info
264+
base_name = f"{description.name}"
265+
266+
if (data_coordinator.config_entry is not None and
267+
data_coordinator.config_entry.options.get(USE_DEVICE_PREFIX, False) and
268+
device_info is not None and
269+
isinstance(device_info, dict) and
270+
"name" in device_info):
271+
self._attr_name = f"{device_info['name']} {base_name}"
272+
else:
273+
self._attr_name = base_name
274+
262275
self._attr_unique_id = (
263276
f"span_{span_panel.status.serial_number}_{description.key}"
264277
)
265-
self._attr_device_info = panel_to_device_info(span_panel)
266278

267279
_LOGGER.debug("CREATE SENSOR SPAN [%s]", self._attr_name)
268280

@@ -298,9 +310,15 @@ def __init__(
298310
span_panel: SpanPanel,
299311
) -> None:
300312
"""Initialize Span Panel Circuit entity."""
301-
super().__init__(coordinator, description, span_panel)
313+
# Create a new description with modified name including circuit name
314+
circuit_description = SpanPanelCircuitsSensorEntityDescription(
315+
**{
316+
**vars(description),
317+
"name": f"{name} {description.name}"
318+
}
319+
)
320+
super().__init__(coordinator, circuit_description, span_panel)
302321
self.id = circuit_id
303-
self._attr_name = f"{name} {description.name}"
304322
self._attr_unique_id = (
305323
f"span_{span_panel.status.serial_number}_{circuit_id}_{description.key}"
306324
)

custom_components/span_panel/span_panel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(
4545
async_client: httpx.AsyncClient | None = None,
4646
) -> None:
4747
"""Initialize the Span Panel."""
48-
self._options = options # Make it protected
48+
self._options = options
4949
self.api = SpanPanelApi(host, access_token, options, async_client)
5050
self._status: SpanPanelHardwareStatus | None = None
5151
self._panel: SpanPanelData | None = None
@@ -56,19 +56,19 @@ def _get_hardware_status(self) -> SpanPanelHardwareStatus:
5656
"""Get hardware status with type checking."""
5757
if self._status is None:
5858
raise RuntimeError("Hardware status not available")
59-
return deepcopy(self._status) # Return copy for thread safety
59+
return deepcopy(self._status)
6060

6161
def _get_data(self) -> SpanPanelData:
6262
"""Get data with type checking."""
6363
if self._panel is None:
6464
raise RuntimeError("Panel data not available")
65-
return deepcopy(self._panel) # Return copy for thread safety
65+
return deepcopy(self._panel)
6666

6767
def _get_storage_battery(self) -> SpanPanelStorageBattery:
6868
"""Get storage battery with type checking."""
6969
if self._storage_battery is None:
7070
raise RuntimeError("Storage battery not available")
71-
return deepcopy(self._storage_battery) # Return copy for thread safety
71+
return deepcopy(self._storage_battery)
7272

7373
@property
7474
def host(self) -> str:

custom_components/span_panel/switch.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1111
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1212

13-
from .const import COORDINATOR, DOMAIN, CircuitRelayState
13+
from .const import COORDINATOR, DOMAIN, USE_DEVICE_PREFIX, CircuitRelayState
1414
from .coordinator import SpanPanelCoordinator
1515
from .span_panel import SpanPanel
1616
from .util import panel_to_device_info
@@ -74,7 +74,10 @@ def icon(self):
7474
def name(self):
7575
"""Return the switch name."""
7676
span_panel: SpanPanel = self.coordinator.data
77-
return f"{span_panel.circuits[self.id].name} Breaker"
77+
base_name = f"{span_panel.circuits[self.id].name} Breaker"
78+
if self.coordinator.config_entry.options.get(USE_DEVICE_PREFIX, False):
79+
return f"{self._attr_device_info['name']} {base_name}"
80+
return base_name
7881

7982
@property
8083
def is_on(self) -> bool | None:

0 commit comments

Comments
 (0)