mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Reduce complexity of shelly button setup (#109625)
This commit is contained in:
parent
7572a73c16
commit
a7c074e388
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from functools import partial
|
||||||
from typing import TYPE_CHECKING, Any, Final, Generic, TypeVar
|
from typing import TYPE_CHECKING, Any, Final, Generic, TypeVar
|
||||||
|
|
||||||
from aioshelly.const import RPC_GENERATIONS
|
from aioshelly.const import RPC_GENERATIONS
|
||||||
@ -83,8 +84,8 @@ BUTTONS: Final[list[ShellyButtonDescription[Any]]] = [
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_migrate_unique_ids(
|
def async_migrate_unique_ids(
|
||||||
entity_entry: er.RegistryEntry,
|
|
||||||
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator,
|
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator,
|
||||||
|
entity_entry: er.RegistryEntry,
|
||||||
) -> dict[str, Any] | None:
|
) -> dict[str, Any] | None:
|
||||||
"""Migrate button unique IDs."""
|
"""Migrate button unique IDs."""
|
||||||
if not entity_entry.entity_id.startswith("button"):
|
if not entity_entry.entity_id.startswith("button"):
|
||||||
@ -117,35 +118,25 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set buttons for device."""
|
"""Set buttons for device."""
|
||||||
|
entry_data = get_entry_data(hass)[config_entry.entry_id]
|
||||||
@callback
|
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator | None
|
||||||
def _async_migrate_unique_ids(
|
|
||||||
entity_entry: er.RegistryEntry,
|
|
||||||
) -> dict[str, Any] | None:
|
|
||||||
"""Migrate button unique IDs."""
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
assert coordinator is not None
|
|
||||||
return async_migrate_unique_ids(entity_entry, coordinator)
|
|
||||||
|
|
||||||
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator | None = None
|
|
||||||
if get_device_entry_gen(config_entry) in RPC_GENERATIONS:
|
if get_device_entry_gen(config_entry) in RPC_GENERATIONS:
|
||||||
coordinator = get_entry_data(hass)[config_entry.entry_id].rpc
|
coordinator = entry_data.rpc
|
||||||
else:
|
else:
|
||||||
coordinator = get_entry_data(hass)[config_entry.entry_id].block
|
coordinator = entry_data.block
|
||||||
|
|
||||||
if coordinator is not None:
|
if TYPE_CHECKING:
|
||||||
await er.async_migrate_entries(
|
assert coordinator is not None
|
||||||
hass, config_entry.entry_id, _async_migrate_unique_ids
|
|
||||||
)
|
|
||||||
|
|
||||||
entities: list[ShellyButton] = []
|
await er.async_migrate_entries(
|
||||||
|
hass, config_entry.entry_id, partial(async_migrate_unique_ids, coordinator)
|
||||||
|
)
|
||||||
|
|
||||||
for button in BUTTONS:
|
async_add_entities(
|
||||||
if not button.supported(coordinator):
|
ShellyButton(coordinator, button)
|
||||||
continue
|
for button in BUTTONS
|
||||||
entities.append(ShellyButton(coordinator, button))
|
if button.supported(coordinator)
|
||||||
|
)
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
class ShellyButton(
|
class ShellyButton(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user