Skip to content

Commit ebc6088

Browse files
authored
Do not create restart button for sleeping gen2+ Shelly devices (home-assistant#158047)
1 parent 5d13a41 commit ebc6088

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

homeassistant/components/shelly/button.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
)
4545
from .utils import (
4646
async_remove_orphaned_entities,
47+
async_remove_shelly_entity,
4748
format_ble_addr,
4849
get_blu_trv_device_info,
4950
get_device_entry_gen,
@@ -80,6 +81,7 @@ class RpcButtonDescription(RpcEntityDescription, ButtonEntityDescription):
8081
device_class=ButtonDeviceClass.RESTART,
8182
entity_category=EntityCategory.CONFIG,
8283
press_action="trigger_reboot",
84+
supported=lambda coordinator: coordinator.sleep_period == 0,
8385
),
8486
ShellyButtonDescription[ShellyBlockCoordinator](
8587
key="self_test",
@@ -197,7 +199,8 @@ async def async_setup_entry(
197199
"""Set up button entities."""
198200
entry_data = config_entry.runtime_data
199201
coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator | None
200-
if get_device_entry_gen(config_entry) in RPC_GENERATIONS:
202+
device_gen = get_device_entry_gen(config_entry)
203+
if device_gen in RPC_GENERATIONS:
201204
coordinator = entry_data.rpc
202205
else:
203206
coordinator = entry_data.block
@@ -210,6 +213,12 @@ async def async_setup_entry(
210213
hass, config_entry.entry_id, partial(async_migrate_unique_ids, coordinator)
211214
)
212215

216+
# Remove the 'restart' button for sleeping devices as it was mistakenly
217+
# added in https://github.com/home-assistant/core/pull/154673
218+
entry_sleep_period = config_entry.data[CONF_SLEEP_PERIOD]
219+
if device_gen in RPC_GENERATIONS and entry_sleep_period:
220+
async_remove_shelly_entity(hass, BUTTON_PLATFORM, f"{coordinator.mac}-reboot")
221+
213222
entities: list[ShellyButton] = []
214223

215224
entities.extend(
@@ -224,7 +233,7 @@ async def async_setup_entry(
224233
return
225234

226235
# add RPC buttons
227-
if config_entry.data[CONF_SLEEP_PERIOD]:
236+
if entry_sleep_period:
228237
async_setup_entry_rpc(
229238
hass,
230239
config_entry,

tests/components/shelly/test_button.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,30 @@ async def test_wall_display_screen_buttons(
554554
blocking=True,
555555
)
556556
mock_rpc_device.wall_display_set_screen.assert_called_once_with(value=value)
557+
558+
559+
async def test_rpc_remove_restart_button_for_sleeping_devices(
560+
hass: HomeAssistant,
561+
mock_rpc_device: Mock,
562+
monkeypatch: pytest.MonkeyPatch,
563+
device_registry: DeviceRegistry,
564+
entity_registry: EntityRegistry,
565+
) -> None:
566+
"""Test RPC remove restart button for sleeping devices."""
567+
config_entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True)
568+
device_entry = register_device(device_registry, config_entry)
569+
entity_id = register_entity(
570+
hass,
571+
BUTTON_DOMAIN,
572+
"test_name_restart",
573+
"reboot",
574+
config_entry,
575+
device_id=device_entry.id,
576+
)
577+
578+
assert entity_registry.async_get(entity_id) is not None
579+
580+
await hass.config_entries.async_setup(config_entry.entry_id)
581+
await hass.async_block_till_done()
582+
583+
assert entity_registry.async_get(entity_id) is None

0 commit comments

Comments
 (0)