mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Mark UniFi power cycle button as unavailable if PoE is not enabled on port (#122035)
This commit is contained in:
parent
4bcdb551d4
commit
aeabe3ab95
@ -8,7 +8,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
|
||||||
import secrets
|
import secrets
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
import aiounifi
|
import aiounifi
|
||||||
from aiounifi.interfaces.api_handlers import ItemEvent
|
from aiounifi.interfaces.api_handlers import ItemEvent
|
||||||
@ -44,6 +44,17 @@ from .entity import (
|
|||||||
async_wlan_device_info_fn,
|
async_wlan_device_info_fn,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .hub import UnifiHub
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_port_power_cycle_available_fn(hub: UnifiHub, obj_id: str) -> bool:
|
||||||
|
"""Check if port allows power cycle action."""
|
||||||
|
if not async_device_available_fn(hub, obj_id):
|
||||||
|
return False
|
||||||
|
return bool(hub.api.ports[obj_id].poe_enable)
|
||||||
|
|
||||||
|
|
||||||
async def async_restart_device_control_fn(
|
async def async_restart_device_control_fn(
|
||||||
api: aiounifi.Controller, obj_id: str
|
api: aiounifi.Controller, obj_id: str
|
||||||
@ -96,7 +107,7 @@ ENTITY_DESCRIPTIONS: tuple[UnifiButtonEntityDescription, ...] = (
|
|||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.CONFIG,
|
||||||
device_class=ButtonDeviceClass.RESTART,
|
device_class=ButtonDeviceClass.RESTART,
|
||||||
api_handler_fn=lambda api: api.ports,
|
api_handler_fn=lambda api: api.ports,
|
||||||
available_fn=async_device_available_fn,
|
available_fn=async_port_power_cycle_available_fn,
|
||||||
control_fn=async_power_cycle_port_control_fn,
|
control_fn=async_power_cycle_port_control_fn,
|
||||||
device_info_fn=async_device_device_info_fn,
|
device_info_fn=async_device_device_info_fn,
|
||||||
name_fn=lambda port: f"{port.name} Power Cycle",
|
name_fn=lambda port: f"{port.name} Power Cycle",
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
"""UniFi Network button platform tests."""
|
"""UniFi Network button platform tests."""
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from aiounifi.models.message import MessageKey
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, ButtonDeviceClass
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, ButtonDeviceClass
|
||||||
@ -319,3 +321,33 @@ async def test_wlan_button_entities(
|
|||||||
request_data,
|
request_data,
|
||||||
call,
|
call,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("device_payload", [DEVICE_POWER_CYCLE_POE])
|
||||||
|
@pytest.mark.usefixtures("config_entry_setup")
|
||||||
|
async def test_power_cycle_availability(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_websocket_message,
|
||||||
|
device_payload: dict[str, Any],
|
||||||
|
) -> None:
|
||||||
|
"""Verify that disabling PoE marks entity as unavailable."""
|
||||||
|
entity_id = "button.switch_port_1_power_cycle"
|
||||||
|
|
||||||
|
assert hass.states.get(entity_id).state != STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
# PoE disabled
|
||||||
|
|
||||||
|
device_1 = deepcopy(device_payload[0])
|
||||||
|
device_1["port_table"][0]["poe_enable"] = False
|
||||||
|
mock_websocket_message(message=MessageKey.DEVICE, data=device_1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
# PoE enabled
|
||||||
|
device_1 = deepcopy(device_payload[0])
|
||||||
|
device_1["port_table"][0]["poe_enable"] = True
|
||||||
|
mock_websocket_message(message=MessageKey.DEVICE, data=device_1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert hass.states.get(entity_id).state != STATE_UNAVAILABLE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user