Add wake on LAN via Fritz!Box for tracked devices (#106778)

This commit is contained in:
Chris Bräucker 2024-04-03 18:37:20 +02:00 committed by GitHub
parent 6369b75653
commit 51a3e79048
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 293 additions and 17 deletions

View file

@ -1,18 +1,21 @@
"""Tests for Fritz!Tools button platform."""
import copy
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.components.fritz.const import DOMAIN
from homeassistant.components.fritz.const import DOMAIN, MeshRoles
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.util.dt import utcnow
from .const import MOCK_USER_DATA
from .const import MOCK_MESH_DATA, MOCK_NEW_DEVICE_NODE, MOCK_USER_DATA
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, async_fire_time_changed
async def test_button_setup(hass: HomeAssistant, fc_class_mock, fh_class_mock) -> None:
@ -73,3 +76,113 @@ async def test_buttons(
button = hass.states.get(entity_id)
assert button.state != STATE_UNKNOWN
async def test_wol_button(
hass: HomeAssistant,
entity_registry_enabled_by_default: None,
fc_class_mock,
fh_class_mock,
) -> None:
"""Test Fritz!Tools wake on LAN button."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
button = hass.states.get("button.printer_wake_on_lan")
assert button
assert button.state == STATE_UNKNOWN
with patch(
"homeassistant.components.fritz.common.AvmWrapper.async_wake_on_lan"
) as mock_press_action:
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.printer_wake_on_lan"},
blocking=True,
)
await hass.async_block_till_done()
mock_press_action.assert_called_once_with("AA:BB:CC:00:11:22")
button = hass.states.get("button.printer_wake_on_lan")
assert button.state != STATE_UNKNOWN
async def test_wol_button_new_device(
hass: HomeAssistant,
entity_registry_enabled_by_default: None,
fc_class_mock,
fh_class_mock,
) -> None:
"""Test WoL button is created for new device at runtime."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
entry.add_to_hass(hass)
mesh_data = copy.deepcopy(MOCK_MESH_DATA)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
assert hass.states.get("button.printer_wake_on_lan")
assert not hass.states.get("button.server_wake_on_lan")
mesh_data["nodes"].append(MOCK_NEW_DEVICE_NODE)
fh_class_mock.get_mesh_topology.return_value = mesh_data
async_fire_time_changed(hass, utcnow() + timedelta(seconds=60))
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get("button.printer_wake_on_lan")
assert hass.states.get("button.server_wake_on_lan")
async def test_wol_button_absent_for_mesh_slave(
hass: HomeAssistant,
entity_registry_enabled_by_default: None,
fc_class_mock,
fh_class_mock,
) -> None:
"""Test WoL button not created if interviewed box is in slave mode."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
entry.add_to_hass(hass)
slave_mesh_data = copy.deepcopy(MOCK_MESH_DATA)
slave_mesh_data["nodes"][0]["mesh_role"] = MeshRoles.SLAVE
fh_class_mock.get_mesh_topology.return_value = slave_mesh_data
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
button = hass.states.get("button.printer_wake_on_lan")
assert button is None
async def test_wol_button_absent_for_non_lan_device(
hass: HomeAssistant,
entity_registry_enabled_by_default: None,
fc_class_mock,
fh_class_mock,
) -> None:
"""Test WoL button not created if interviewed device is not connected via LAN."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
entry.add_to_hass(hass)
printer_wifi_data = copy.deepcopy(MOCK_MESH_DATA)
# initialization logic uses the connection type of the `node_interface_1_uid` pair of the printer
# ni-230 is wifi interface of fritzbox
printer_node_interface = printer_wifi_data["nodes"][1]["node_interfaces"][0]
printer_node_interface["type"] = "WLAN"
printer_node_interface["node_links"][0]["node_interface_1_uid"] = "ni-230"
fh_class_mock.get_mesh_topology.return_value = printer_wifi_data
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
button = hass.states.get("button.printer_wake_on_lan")
assert button is None