mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Use Shelly main device area as suggested area for sub-devices (#146810)
This commit is contained in:
@ -235,11 +235,15 @@ class ShellyButton(ShellyBaseButton):
|
|||||||
self._attr_unique_id = f"{coordinator.mac}_{description.key}"
|
self._attr_unique_id = f"{coordinator.mac}_{description.key}"
|
||||||
if isinstance(coordinator, ShellyBlockCoordinator):
|
if isinstance(coordinator, ShellyBlockCoordinator):
|
||||||
self._attr_device_info = get_block_device_info(
|
self._attr_device_info = get_block_device_info(
|
||||||
coordinator.device, coordinator.mac
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._attr_device_info = get_rpc_device_info(
|
self._attr_device_info = get_rpc_device_info(
|
||||||
coordinator.device, coordinator.mac
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}
|
connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}
|
||||||
|
@ -211,7 +211,10 @@ class BlockSleepingClimate(
|
|||||||
elif entry is not None:
|
elif entry is not None:
|
||||||
self._unique_id = entry.unique_id
|
self._unique_id = entry.unique_id
|
||||||
self._attr_device_info = get_block_device_info(
|
self._attr_device_info = get_block_device_info(
|
||||||
coordinator.device, coordinator.mac, sensor_block
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
sensor_block,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._attr_name = get_block_entity_name(
|
self._attr_name = get_block_entity_name(
|
||||||
self.coordinator.device, sensor_block, None
|
self.coordinator.device, sensor_block, None
|
||||||
|
@ -31,7 +31,11 @@ from homeassistant.const import (
|
|||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr, issue_registry as ir
|
from homeassistant.helpers import (
|
||||||
|
area_registry as ar,
|
||||||
|
device_registry as dr,
|
||||||
|
issue_registry as ir,
|
||||||
|
)
|
||||||
from homeassistant.helpers.debounce import Debouncer
|
from homeassistant.helpers.debounce import Debouncer
|
||||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
@ -114,6 +118,7 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice](
|
|||||||
self.device = device
|
self.device = device
|
||||||
self.device_id: str | None = None
|
self.device_id: str | None = None
|
||||||
self._pending_platforms: list[Platform] | None = None
|
self._pending_platforms: list[Platform] | None = None
|
||||||
|
self.suggested_area: str | None = None
|
||||||
device_name = device.name if device.initialized else entry.title
|
device_name = device.name if device.initialized else entry.title
|
||||||
interval_td = timedelta(seconds=update_interval)
|
interval_td = timedelta(seconds=update_interval)
|
||||||
# The device has come online at least once. In the case of a sleeping RPC
|
# The device has come online at least once. In the case of a sleeping RPC
|
||||||
@ -176,6 +181,11 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice](
|
|||||||
hw_version=f"gen{get_device_entry_gen(self.config_entry)}",
|
hw_version=f"gen{get_device_entry_gen(self.config_entry)}",
|
||||||
configuration_url=f"http://{get_host(self.config_entry.data[CONF_HOST])}:{get_http_port(self.config_entry.data)}",
|
configuration_url=f"http://{get_host(self.config_entry.data[CONF_HOST])}:{get_http_port(self.config_entry.data)}",
|
||||||
)
|
)
|
||||||
|
# We want to use the main device area as the suggested area for sub-devices.
|
||||||
|
if (area_id := device_entry.area_id) is not None:
|
||||||
|
area_registry = ar.async_get(self.hass)
|
||||||
|
if (area := area_registry.async_get_area(area_id)) is not None:
|
||||||
|
self.suggested_area = area.name
|
||||||
self.device_id = device_entry.id
|
self.device_id = device_entry.id
|
||||||
|
|
||||||
async def shutdown(self) -> None:
|
async def shutdown(self) -> None:
|
||||||
|
@ -362,7 +362,10 @@ class ShellyBlockEntity(CoordinatorEntity[ShellyBlockCoordinator]):
|
|||||||
self.block = block
|
self.block = block
|
||||||
self._attr_name = get_block_entity_name(coordinator.device, block)
|
self._attr_name = get_block_entity_name(coordinator.device, block)
|
||||||
self._attr_device_info = get_block_device_info(
|
self._attr_device_info = get_block_device_info(
|
||||||
coordinator.device, coordinator.mac, block
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
block,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._attr_unique_id = f"{coordinator.mac}-{block.description}"
|
self._attr_unique_id = f"{coordinator.mac}-{block.description}"
|
||||||
|
|
||||||
@ -405,7 +408,10 @@ class ShellyRpcEntity(CoordinatorEntity[ShellyRpcCoordinator]):
|
|||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.key = key
|
self.key = key
|
||||||
self._attr_device_info = get_rpc_device_info(
|
self._attr_device_info = get_rpc_device_info(
|
||||||
coordinator.device, coordinator.mac, key
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
key,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._attr_unique_id = f"{coordinator.mac}-{key}"
|
self._attr_unique_id = f"{coordinator.mac}-{key}"
|
||||||
self._attr_name = get_rpc_entity_name(coordinator.device, key)
|
self._attr_name = get_rpc_entity_name(coordinator.device, key)
|
||||||
@ -521,7 +527,9 @@ class ShellyRestAttributeEntity(CoordinatorEntity[ShellyBlockCoordinator]):
|
|||||||
)
|
)
|
||||||
self._attr_unique_id = f"{coordinator.mac}-{attribute}"
|
self._attr_unique_id = f"{coordinator.mac}-{attribute}"
|
||||||
self._attr_device_info = get_block_device_info(
|
self._attr_device_info = get_block_device_info(
|
||||||
coordinator.device, coordinator.mac
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._last_value = None
|
self._last_value = None
|
||||||
|
|
||||||
@ -630,7 +638,10 @@ class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity):
|
|||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
|
||||||
self._attr_device_info = get_block_device_info(
|
self._attr_device_info = get_block_device_info(
|
||||||
coordinator.device, coordinator.mac, block
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
block,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
|
|
||||||
if block is not None:
|
if block is not None:
|
||||||
@ -698,7 +709,10 @@ class ShellySleepingRpcAttributeEntity(ShellyRpcAttributeEntity):
|
|||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
|
||||||
self._attr_device_info = get_rpc_device_info(
|
self._attr_device_info = get_rpc_device_info(
|
||||||
coordinator.device, coordinator.mac, key
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
key,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._attr_unique_id = self._attr_unique_id = (
|
self._attr_unique_id = self._attr_unique_id = (
|
||||||
f"{coordinator.mac}-{key}-{attribute}"
|
f"{coordinator.mac}-{key}-{attribute}"
|
||||||
|
@ -207,7 +207,10 @@ class ShellyRpcEvent(CoordinatorEntity[ShellyRpcCoordinator], EventEntity):
|
|||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.event_id = int(key.split(":")[-1])
|
self.event_id = int(key.split(":")[-1])
|
||||||
self._attr_device_info = get_rpc_device_info(
|
self._attr_device_info = get_rpc_device_info(
|
||||||
coordinator.device, coordinator.mac, key
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
key,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
self._attr_unique_id = f"{coordinator.mac}-{key}"
|
self._attr_unique_id = f"{coordinator.mac}-{key}"
|
||||||
self._attr_name = get_rpc_entity_name(coordinator.device, key)
|
self._attr_name = get_rpc_entity_name(coordinator.device, key)
|
||||||
|
@ -139,7 +139,11 @@ class RpcEmeterPhaseSensor(RpcSensor):
|
|||||||
super().__init__(coordinator, key, attribute, description)
|
super().__init__(coordinator, key, attribute, description)
|
||||||
|
|
||||||
self._attr_device_info = get_rpc_device_info(
|
self._attr_device_info = get_rpc_device_info(
|
||||||
coordinator.device, coordinator.mac, key, description.emeter_phase
|
coordinator.device,
|
||||||
|
coordinator.mac,
|
||||||
|
key,
|
||||||
|
emeter_phase=description.emeter_phase,
|
||||||
|
suggested_area=coordinator.suggested_area,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -751,6 +751,7 @@ def get_rpc_device_info(
|
|||||||
mac: str,
|
mac: str,
|
||||||
key: str | None = None,
|
key: str | None = None,
|
||||||
emeter_phase: str | None = None,
|
emeter_phase: str | None = None,
|
||||||
|
suggested_area: str | None = None,
|
||||||
) -> DeviceInfo:
|
) -> DeviceInfo:
|
||||||
"""Return device info for RPC device."""
|
"""Return device info for RPC device."""
|
||||||
if key is None:
|
if key is None:
|
||||||
@ -770,6 +771,7 @@ def get_rpc_device_info(
|
|||||||
identifiers={(DOMAIN, f"{mac}-{key}-{emeter_phase.lower()}")},
|
identifiers={(DOMAIN, f"{mac}-{key}-{emeter_phase.lower()}")},
|
||||||
name=get_rpc_sub_device_name(device, key, emeter_phase),
|
name=get_rpc_sub_device_name(device, key, emeter_phase),
|
||||||
manufacturer="Shelly",
|
manufacturer="Shelly",
|
||||||
|
suggested_area=suggested_area,
|
||||||
via_device=(DOMAIN, mac),
|
via_device=(DOMAIN, mac),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -784,6 +786,7 @@ def get_rpc_device_info(
|
|||||||
identifiers={(DOMAIN, f"{mac}-{key}")},
|
identifiers={(DOMAIN, f"{mac}-{key}")},
|
||||||
name=get_rpc_sub_device_name(device, key),
|
name=get_rpc_sub_device_name(device, key),
|
||||||
manufacturer="Shelly",
|
manufacturer="Shelly",
|
||||||
|
suggested_area=suggested_area,
|
||||||
via_device=(DOMAIN, mac),
|
via_device=(DOMAIN, mac),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -805,7 +808,10 @@ def get_blu_trv_device_info(
|
|||||||
|
|
||||||
|
|
||||||
def get_block_device_info(
|
def get_block_device_info(
|
||||||
device: BlockDevice, mac: str, block: Block | None = None
|
device: BlockDevice,
|
||||||
|
mac: str,
|
||||||
|
block: Block | None = None,
|
||||||
|
suggested_area: str | None = None,
|
||||||
) -> DeviceInfo:
|
) -> DeviceInfo:
|
||||||
"""Return device info for Block device."""
|
"""Return device info for Block device."""
|
||||||
if (
|
if (
|
||||||
@ -820,6 +826,7 @@ def get_block_device_info(
|
|||||||
identifiers={(DOMAIN, f"{mac}-{block.description}")},
|
identifiers={(DOMAIN, f"{mac}-{block.description}")},
|
||||||
name=get_block_sub_device_name(device, block),
|
name=get_block_sub_device_name(device, block),
|
||||||
manufacturer="Shelly",
|
manufacturer="Shelly",
|
||||||
|
suggested_area=suggested_area,
|
||||||
via_device=(DOMAIN, mac),
|
via_device=(DOMAIN, mac),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user