mirror of
https://github.com/home-assistant/core.git
synced 2025-11-15 14:00:24 +00:00
* Shelly RPC sub-devices
* Better varaible name
* Add get_rpc_device_info helper
* Revert channel name changes
* Use get_rpc_device_info
* Add get_rpc_device_info helper
* Use get_block_device_info
* Use helpers in the button platform
* Fix channel name and roller mode for block devices
* Fix EM3 gen1
* Fix channel name for RPC devices
* Revert test changes
* Fix/improve test_block_get_block_channel_name
* Fix test_get_rpc_channel_name_multiple_components
* Fix tests
* Fix tests
* Fix tests
* Use key instead of index to generate sub-device identifier
* Improve logic for Pro RGBWW PM
* Split channels for em1
* Better channel name
* Cleaning
* has_entity_name is True
* Add get_block_sub_device_name() function
* Improve block functions
* Add get_rpc_sub_device_name() function
* Remove _attr_name
* Remove name for button with device class
* Fix names of virtual components
* Better Input name
* Fix get_rpc_channel_name()
* Fix names for Inputs
* get_rpc_channel_name() improvement
* Better variable name
* Clean RPC functions
* Fix input_name type
* Fix test
* Fix entity_ids for Blu Trv
* Fix get_block_channel_name()
* Fix for Blu Trv, once again
* Revert name for reboot button
* Fix button tests
* Fix tests
* Fix coordinator tests
* Fix tests for cover platform
* Fix tests for event platform
* Fix entity_ids in init tests
* Fix get_block_channel_name() for lights
* Fix tests for light platform
* Fix test for logbook
* Update snapshots for number platform
* Fix tests for sensor platform
* Fix tests for switch platform
* Fix tests for utils
* Uncomment
* Fix tests for flood
* Fix Valve entity name
* Fix climate tests
* Fix test for diagnostics
* Fix tests for init
* Remove old snapshots
* Add tests for 2PM Gen3
* Add comment
* More tests
* Cleaning
* Clean fixtures
* Update tests
* Anonymize coordinates in fixtures
* Split Pro 3EM entities into sub-devices
* Make sub-device names more unique
* 3EM (gen1) does not support sub-devices
* Coverage
* Rename "device temperature" sensor to the "relay temperature"
* Update tests after rebase
* Support sub-devices for 3EM (gen1)
* Mark has-entity-name rule as done 🎉
* Rename `relay temperature` to `temperature`
285 lines
9.1 KiB
Python
285 lines
9.1 KiB
Python
"""Button for Shelly."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from dataclasses import dataclass
|
|
from functools import partial
|
|
from typing import TYPE_CHECKING, Any, Final
|
|
|
|
from aioshelly.const import BLU_TRV_IDENTIFIER, MODEL_BLU_GATEWAY, RPC_GENERATIONS
|
|
from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError, RpcCallError
|
|
|
|
from homeassistant.components.button import (
|
|
ButtonDeviceClass,
|
|
ButtonEntity,
|
|
ButtonEntityDescription,
|
|
)
|
|
from homeassistant.const import EntityCategory
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers import entity_registry as er
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
from homeassistant.util import slugify
|
|
|
|
from .const import DOMAIN, LOGGER, SHELLY_GAS_MODELS
|
|
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
|
|
from .utils import (
|
|
get_block_device_info,
|
|
get_blu_trv_device_info,
|
|
get_device_entry_gen,
|
|
get_rpc_device_info,
|
|
get_rpc_key_ids,
|
|
)
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class ShellyButtonDescription[
|
|
_ShellyCoordinatorT: ShellyBlockCoordinator | ShellyRpcCoordinator
|
|
](ButtonEntityDescription):
|
|
"""Class to describe a Button entity."""
|
|
|
|
press_action: str
|
|
|
|
supported: Callable[[_ShellyCoordinatorT], bool] = lambda _: True
|
|
|
|
|
|
BUTTONS: Final[list[ShellyButtonDescription[Any]]] = [
|
|
ShellyButtonDescription[ShellyBlockCoordinator | ShellyRpcCoordinator](
|
|
key="reboot",
|
|
name="Reboot",
|
|
device_class=ButtonDeviceClass.RESTART,
|
|
entity_category=EntityCategory.CONFIG,
|
|
press_action="trigger_reboot",
|
|
),
|
|
ShellyButtonDescription[ShellyBlockCoordinator](
|
|
key="self_test",
|
|
name="Self test",
|
|
translation_key="self_test",
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
press_action="trigger_shelly_gas_self_test",
|
|
supported=lambda coordinator: coordinator.device.model in SHELLY_GAS_MODELS,
|
|
),
|
|
ShellyButtonDescription[ShellyBlockCoordinator](
|
|
key="mute",
|
|
name="Mute",
|
|
translation_key="mute",
|
|
entity_category=EntityCategory.CONFIG,
|
|
press_action="trigger_shelly_gas_mute",
|
|
supported=lambda coordinator: coordinator.device.model in SHELLY_GAS_MODELS,
|
|
),
|
|
ShellyButtonDescription[ShellyBlockCoordinator](
|
|
key="unmute",
|
|
name="Unmute",
|
|
translation_key="unmute",
|
|
entity_category=EntityCategory.CONFIG,
|
|
press_action="trigger_shelly_gas_unmute",
|
|
supported=lambda coordinator: coordinator.device.model in SHELLY_GAS_MODELS,
|
|
),
|
|
]
|
|
|
|
BLU_TRV_BUTTONS: Final[list[ShellyButtonDescription]] = [
|
|
ShellyButtonDescription[ShellyRpcCoordinator](
|
|
key="calibrate",
|
|
name="Calibrate",
|
|
translation_key="calibrate",
|
|
entity_category=EntityCategory.CONFIG,
|
|
press_action="trigger_blu_trv_calibration",
|
|
supported=lambda coordinator: coordinator.device.model == MODEL_BLU_GATEWAY,
|
|
),
|
|
]
|
|
|
|
|
|
@callback
|
|
def async_migrate_unique_ids(
|
|
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator,
|
|
entity_entry: er.RegistryEntry,
|
|
) -> dict[str, Any] | None:
|
|
"""Migrate button unique IDs."""
|
|
if not entity_entry.entity_id.startswith("button"):
|
|
return None
|
|
|
|
device_name = slugify(coordinator.device.name)
|
|
|
|
for key in ("reboot", "self_test", "mute", "unmute"):
|
|
old_unique_id = f"{device_name}_{key}"
|
|
if entity_entry.unique_id == old_unique_id:
|
|
new_unique_id = f"{coordinator.mac}_{key}"
|
|
LOGGER.debug(
|
|
"Migrating unique_id for %s entity from [%s] to [%s]",
|
|
entity_entry.entity_id,
|
|
old_unique_id,
|
|
new_unique_id,
|
|
)
|
|
return {
|
|
"new_unique_id": entity_entry.unique_id.replace(
|
|
old_unique_id, new_unique_id
|
|
)
|
|
}
|
|
|
|
return None
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ShellyConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set buttons for device."""
|
|
entry_data = config_entry.runtime_data
|
|
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator | None
|
|
if get_device_entry_gen(config_entry) in RPC_GENERATIONS:
|
|
coordinator = entry_data.rpc
|
|
else:
|
|
coordinator = entry_data.block
|
|
|
|
if TYPE_CHECKING:
|
|
assert coordinator is not None
|
|
|
|
await er.async_migrate_entries(
|
|
hass, config_entry.entry_id, partial(async_migrate_unique_ids, coordinator)
|
|
)
|
|
|
|
entities: list[ShellyButton | ShellyBluTrvButton] = []
|
|
|
|
entities.extend(
|
|
ShellyButton(coordinator, button)
|
|
for button in BUTTONS
|
|
if button.supported(coordinator)
|
|
)
|
|
|
|
if blutrv_key_ids := get_rpc_key_ids(coordinator.device.status, BLU_TRV_IDENTIFIER):
|
|
if TYPE_CHECKING:
|
|
assert isinstance(coordinator, ShellyRpcCoordinator)
|
|
|
|
entities.extend(
|
|
ShellyBluTrvButton(coordinator, button, id_)
|
|
for id_ in blutrv_key_ids
|
|
for button in BLU_TRV_BUTTONS
|
|
)
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
class ShellyBaseButton(
|
|
CoordinatorEntity[ShellyRpcCoordinator | ShellyBlockCoordinator], ButtonEntity
|
|
):
|
|
"""Defines a Shelly base button."""
|
|
|
|
_attr_has_entity_name = True
|
|
entity_description: ShellyButtonDescription[
|
|
ShellyRpcCoordinator | ShellyBlockCoordinator
|
|
]
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator,
|
|
description: ShellyButtonDescription[
|
|
ShellyRpcCoordinator | ShellyBlockCoordinator
|
|
],
|
|
) -> None:
|
|
"""Initialize Shelly button."""
|
|
super().__init__(coordinator)
|
|
|
|
self.entity_description = description
|
|
|
|
async def async_press(self) -> None:
|
|
"""Triggers the Shelly button press service."""
|
|
try:
|
|
await self._press_method()
|
|
except DeviceConnectionError as err:
|
|
self.coordinator.last_update_success = False
|
|
raise HomeAssistantError(
|
|
translation_domain=DOMAIN,
|
|
translation_key="device_communication_action_error",
|
|
translation_placeholders={
|
|
"entity": self.entity_id,
|
|
"device": self.coordinator.name,
|
|
},
|
|
) from err
|
|
except RpcCallError as err:
|
|
raise HomeAssistantError(
|
|
translation_domain=DOMAIN,
|
|
translation_key="rpc_call_action_error",
|
|
translation_placeholders={
|
|
"entity": self.entity_id,
|
|
"device": self.coordinator.name,
|
|
},
|
|
) from err
|
|
except InvalidAuthError:
|
|
await self.coordinator.async_shutdown_device_and_start_reauth()
|
|
|
|
async def _press_method(self) -> None:
|
|
"""Press method."""
|
|
raise NotImplementedError
|
|
|
|
|
|
class ShellyButton(ShellyBaseButton):
|
|
"""Defines a Shelly button."""
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator,
|
|
description: ShellyButtonDescription[
|
|
ShellyRpcCoordinator | ShellyBlockCoordinator
|
|
],
|
|
) -> None:
|
|
"""Initialize Shelly button."""
|
|
super().__init__(coordinator, description)
|
|
|
|
self._attr_unique_id = f"{coordinator.mac}_{description.key}"
|
|
if isinstance(coordinator, ShellyBlockCoordinator):
|
|
self._attr_device_info = get_block_device_info(
|
|
coordinator.device, coordinator.mac
|
|
)
|
|
else:
|
|
self._attr_device_info = get_rpc_device_info(
|
|
coordinator.device, coordinator.mac
|
|
)
|
|
self._attr_device_info = DeviceInfo(
|
|
connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}
|
|
)
|
|
|
|
async def _press_method(self) -> None:
|
|
"""Press method."""
|
|
method = getattr(self.coordinator.device, self.entity_description.press_action)
|
|
|
|
if TYPE_CHECKING:
|
|
assert method is not None
|
|
|
|
await method()
|
|
|
|
|
|
class ShellyBluTrvButton(ShellyBaseButton):
|
|
"""Represent a Shelly BLU TRV button."""
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: ShellyRpcCoordinator,
|
|
description: ShellyButtonDescription,
|
|
id_: int,
|
|
) -> None:
|
|
"""Initialize."""
|
|
super().__init__(coordinator, description)
|
|
|
|
config = coordinator.device.config[f"{BLU_TRV_IDENTIFIER}:{id_}"]
|
|
ble_addr: str = config["addr"]
|
|
self._attr_unique_id = f"{ble_addr}_{description.key}"
|
|
self._attr_device_info = get_blu_trv_device_info(
|
|
config, ble_addr, coordinator.mac
|
|
)
|
|
self._id = id_
|
|
|
|
async def _press_method(self) -> None:
|
|
"""Press method."""
|
|
method = getattr(self.coordinator.device, self.entity_description.press_action)
|
|
|
|
if TYPE_CHECKING:
|
|
assert method is not None
|
|
|
|
await method(self._id)
|