Skip to content

Commit 01a133a

Browse files
bieniufrenck
authored andcommitted
Use Shelly main device area as suggested area for sub-devices (home-assistant#146810)
1 parent b249ae4 commit 01a133a

File tree

7 files changed

+57
-12
lines changed

7 files changed

+57
-12
lines changed

homeassistant/components/shelly/button.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,15 @@ def __init__(
235235
self._attr_unique_id = f"{coordinator.mac}_{description.key}"
236236
if isinstance(coordinator, ShellyBlockCoordinator):
237237
self._attr_device_info = get_block_device_info(
238-
coordinator.device, coordinator.mac
238+
coordinator.device,
239+
coordinator.mac,
240+
suggested_area=coordinator.suggested_area,
239241
)
240242
else:
241243
self._attr_device_info = get_rpc_device_info(
242-
coordinator.device, coordinator.mac
244+
coordinator.device,
245+
coordinator.mac,
246+
suggested_area=coordinator.suggested_area,
243247
)
244248
self._attr_device_info = DeviceInfo(
245249
connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}

homeassistant/components/shelly/climate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ def __init__(
211211
elif entry is not None:
212212
self._unique_id = entry.unique_id
213213
self._attr_device_info = get_block_device_info(
214-
coordinator.device, coordinator.mac, sensor_block
214+
coordinator.device,
215+
coordinator.mac,
216+
sensor_block,
217+
suggested_area=coordinator.suggested_area,
215218
)
216219
self._attr_name = get_block_entity_name(
217220
self.coordinator.device, sensor_block, None

homeassistant/components/shelly/coordinator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
Platform,
3232
)
3333
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
34-
from homeassistant.helpers import device_registry as dr, issue_registry as ir
34+
from homeassistant.helpers import (
35+
area_registry as ar,
36+
device_registry as dr,
37+
issue_registry as ir,
38+
)
3539
from homeassistant.helpers.debounce import Debouncer
3640
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
3741
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@@ -114,6 +118,7 @@ def __init__(
114118
self.device = device
115119
self.device_id: str | None = None
116120
self._pending_platforms: list[Platform] | None = None
121+
self.suggested_area: str | None = None
117122
device_name = device.name if device.initialized else entry.title
118123
interval_td = timedelta(seconds=update_interval)
119124
# The device has come online at least once. In the case of a sleeping RPC
@@ -176,6 +181,11 @@ def async_setup(self, pending_platforms: list[Platform] | None = None) -> None:
176181
hw_version=f"gen{get_device_entry_gen(self.config_entry)}",
177182
configuration_url=f"http://{get_host(self.config_entry.data[CONF_HOST])}:{get_http_port(self.config_entry.data)}",
178183
)
184+
# We want to use the main device area as the suggested area for sub-devices.
185+
if (area_id := device_entry.area_id) is not None:
186+
area_registry = ar.async_get(self.hass)
187+
if (area := area_registry.async_get_area(area_id)) is not None:
188+
self.suggested_area = area.name
179189
self.device_id = device_entry.id
180190

181191
async def shutdown(self) -> None:

homeassistant/components/shelly/entity.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,10 @@ def __init__(self, coordinator: ShellyBlockCoordinator, block: Block) -> None:
362362
self.block = block
363363
self._attr_name = get_block_entity_name(coordinator.device, block)
364364
self._attr_device_info = get_block_device_info(
365-
coordinator.device, coordinator.mac, block
365+
coordinator.device,
366+
coordinator.mac,
367+
block,
368+
suggested_area=coordinator.suggested_area,
366369
)
367370
self._attr_unique_id = f"{coordinator.mac}-{block.description}"
368371

@@ -405,7 +408,10 @@ def __init__(self, coordinator: ShellyRpcCoordinator, key: str) -> None:
405408
super().__init__(coordinator)
406409
self.key = key
407410
self._attr_device_info = get_rpc_device_info(
408-
coordinator.device, coordinator.mac, key
411+
coordinator.device,
412+
coordinator.mac,
413+
key,
414+
suggested_area=coordinator.suggested_area,
409415
)
410416
self._attr_unique_id = f"{coordinator.mac}-{key}"
411417
self._attr_name = get_rpc_entity_name(coordinator.device, key)
@@ -521,7 +527,9 @@ def __init__(
521527
)
522528
self._attr_unique_id = f"{coordinator.mac}-{attribute}"
523529
self._attr_device_info = get_block_device_info(
524-
coordinator.device, coordinator.mac
530+
coordinator.device,
531+
coordinator.mac,
532+
suggested_area=coordinator.suggested_area,
525533
)
526534
self._last_value = None
527535

@@ -630,7 +638,10 @@ def __init__(
630638
self.entity_description = description
631639

632640
self._attr_device_info = get_block_device_info(
633-
coordinator.device, coordinator.mac, block
641+
coordinator.device,
642+
coordinator.mac,
643+
block,
644+
suggested_area=coordinator.suggested_area,
634645
)
635646

636647
if block is not None:
@@ -698,7 +709,10 @@ def __init__(
698709
self.entity_description = description
699710

700711
self._attr_device_info = get_rpc_device_info(
701-
coordinator.device, coordinator.mac, key
712+
coordinator.device,
713+
coordinator.mac,
714+
key,
715+
suggested_area=coordinator.suggested_area,
702716
)
703717
self._attr_unique_id = self._attr_unique_id = (
704718
f"{coordinator.mac}-{key}-{attribute}"

homeassistant/components/shelly/event.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ def __init__(
207207
super().__init__(coordinator)
208208
self.event_id = int(key.split(":")[-1])
209209
self._attr_device_info = get_rpc_device_info(
210-
coordinator.device, coordinator.mac, key
210+
coordinator.device,
211+
coordinator.mac,
212+
key,
213+
suggested_area=coordinator.suggested_area,
211214
)
212215
self._attr_unique_id = f"{coordinator.mac}-{key}"
213216
self._attr_name = get_rpc_entity_name(coordinator.device, key)

homeassistant/components/shelly/sensor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ def __init__(
139139
super().__init__(coordinator, key, attribute, description)
140140

141141
self._attr_device_info = get_rpc_device_info(
142-
coordinator.device, coordinator.mac, key, description.emeter_phase
142+
coordinator.device,
143+
coordinator.mac,
144+
key,
145+
emeter_phase=description.emeter_phase,
146+
suggested_area=coordinator.suggested_area,
143147
)
144148

145149

homeassistant/components/shelly/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ def get_rpc_device_info(
751751
mac: str,
752752
key: str | None = None,
753753
emeter_phase: str | None = None,
754+
suggested_area: str | None = None,
754755
) -> DeviceInfo:
755756
"""Return device info for RPC device."""
756757
if key is None:
@@ -770,6 +771,7 @@ def get_rpc_device_info(
770771
identifiers={(DOMAIN, f"{mac}-{key}-{emeter_phase.lower()}")},
771772
name=get_rpc_sub_device_name(device, key, emeter_phase),
772773
manufacturer="Shelly",
774+
suggested_area=suggested_area,
773775
via_device=(DOMAIN, mac),
774776
)
775777

@@ -784,6 +786,7 @@ def get_rpc_device_info(
784786
identifiers={(DOMAIN, f"{mac}-{key}")},
785787
name=get_rpc_sub_device_name(device, key),
786788
manufacturer="Shelly",
789+
suggested_area=suggested_area,
787790
via_device=(DOMAIN, mac),
788791
)
789792

@@ -805,7 +808,10 @@ def get_blu_trv_device_info(
805808

806809

807810
def get_block_device_info(
808-
device: BlockDevice, mac: str, block: Block | None = None
811+
device: BlockDevice,
812+
mac: str,
813+
block: Block | None = None,
814+
suggested_area: str | None = None,
809815
) -> DeviceInfo:
810816
"""Return device info for Block device."""
811817
if (
@@ -820,6 +826,7 @@ def get_block_device_info(
820826
identifiers={(DOMAIN, f"{mac}-{block.description}")},
821827
name=get_block_sub_device_name(device, block),
822828
manufacturer="Shelly",
829+
suggested_area=suggested_area,
823830
via_device=(DOMAIN, mac),
824831
)
825832

0 commit comments

Comments
 (0)