mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add action enums to fritz (#190303)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"""Services for Fritz integration."""
|
||||
|
||||
from enum import StrEnum
|
||||
|
||||
from fritzconnection.core.exceptions import (
|
||||
FritzActionError,
|
||||
FritzActionFailedError,
|
||||
@@ -9,6 +11,7 @@ from fritzconnection.core.exceptions import (
|
||||
from fritzconnection.lib.fritzwlan import DEFAULT_PASSWORD_LENGTH
|
||||
import probatio
|
||||
|
||||
from homeassistant.const import CommonServiceArgument
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.helpers.service import (
|
||||
@@ -19,20 +22,39 @@ from homeassistant.helpers.service import (
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .coordinator import FritzConfigEntry
|
||||
|
||||
SERVICE_SET_GUEST_WIFI_PW = "set_guest_wifi_password"
|
||||
|
||||
class FritzService(StrEnum):
|
||||
"""Store keys for FRITZ!Box Tools services."""
|
||||
|
||||
SET_GUEST_WIFI_PASSWORD = "set_guest_wifi_password"
|
||||
DIAL = "dial"
|
||||
|
||||
|
||||
class FritzServiceArgument(StrEnum):
|
||||
"""Store keys for FRITZ!Box Tools service arguments."""
|
||||
|
||||
PASSWORD = "password"
|
||||
LENGTH = "length"
|
||||
NUMBER = "number"
|
||||
MAX_RING_SECONDS = "max_ring_seconds"
|
||||
|
||||
|
||||
SERVICE_SCHEMA_SET_GUEST_WIFI_PW = probatio.Schema(
|
||||
{
|
||||
probatio.Required("device_id"): str,
|
||||
probatio.Optional("password"): probatio.Length(min=8, max=63),
|
||||
probatio.Optional("length"): probatio.Range(min=8, max=63),
|
||||
probatio.Required(CommonServiceArgument.DEVICE_ID): str,
|
||||
probatio.Optional(FritzServiceArgument.PASSWORD): probatio.Length(
|
||||
min=8, max=63
|
||||
),
|
||||
probatio.Optional(FritzServiceArgument.LENGTH): probatio.Range(min=8, max=63),
|
||||
}
|
||||
)
|
||||
SERVICE_DIAL = "dial"
|
||||
SERVICE_SCHEMA_DIAL = probatio.Schema(
|
||||
{
|
||||
probatio.Required("device_id"): str,
|
||||
probatio.Required("number"): str,
|
||||
probatio.Required("max_ring_seconds"): probatio.Range(min=1, max=300),
|
||||
probatio.Required(CommonServiceArgument.DEVICE_ID): str,
|
||||
probatio.Required(FritzServiceArgument.NUMBER): str,
|
||||
probatio.Required(FritzServiceArgument.MAX_RING_SECONDS): probatio.Range(
|
||||
min=1, max=300
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -60,8 +82,10 @@ async def _async_set_guest_wifi_password(service_call: ServiceCall) -> None:
|
||||
avm_wrapper = target_entry.runtime_data
|
||||
try:
|
||||
await avm_wrapper.async_trigger_set_guest_password(
|
||||
service_call.data.get("password"),
|
||||
service_call.data.get("length", DEFAULT_PASSWORD_LENGTH),
|
||||
service_call.data.get(FritzServiceArgument.PASSWORD),
|
||||
service_call.data.get(
|
||||
FritzServiceArgument.LENGTH, DEFAULT_PASSWORD_LENGTH
|
||||
),
|
||||
)
|
||||
except (FritzServiceError, FritzActionError) as ex:
|
||||
raise HomeAssistantError(
|
||||
@@ -96,8 +120,10 @@ async def _async_dial(service_call: ServiceCall) -> None:
|
||||
avm_wrapper = target_entry.runtime_data
|
||||
try:
|
||||
await avm_wrapper.async_trigger_dial(
|
||||
service_call.data["number"],
|
||||
max_ring_seconds=service_call.data["max_ring_seconds"],
|
||||
service_call.data[FritzServiceArgument.NUMBER],
|
||||
max_ring_seconds=service_call.data[
|
||||
FritzServiceArgument.MAX_RING_SECONDS
|
||||
],
|
||||
)
|
||||
except (FritzServiceError, FritzActionError) as ex:
|
||||
raise HomeAssistantError(
|
||||
@@ -120,8 +146,10 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
async_register_admin_service(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SERVICE_SET_GUEST_WIFI_PW,
|
||||
FritzService.SET_GUEST_WIFI_PASSWORD,
|
||||
_async_set_guest_wifi_password,
|
||||
SERVICE_SCHEMA_SET_GUEST_WIFI_PW,
|
||||
)
|
||||
hass.services.async_register(DOMAIN, SERVICE_DIAL, _async_dial, SERVICE_SCHEMA_DIAL)
|
||||
hass.services.async_register(
|
||||
DOMAIN, FritzService.DIAL, _async_dial, SERVICE_SCHEMA_DIAL
|
||||
)
|
||||
|
||||
@@ -12,10 +12,7 @@ import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
|
||||
from homeassistant.components.fritz.const import DOMAIN
|
||||
from homeassistant.components.fritz.services import (
|
||||
SERVICE_DIAL,
|
||||
SERVICE_SET_GUEST_WIFI_PW,
|
||||
)
|
||||
from homeassistant.components.fritz.services import FritzService
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -30,8 +27,8 @@ async def test_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
services = hass.services.async_services_for_domain(DOMAIN)
|
||||
assert services
|
||||
assert SERVICE_SET_GUEST_WIFI_PW in services
|
||||
assert SERVICE_DIAL in services
|
||||
assert FritzService.SET_GUEST_WIFI_PASSWORD in services
|
||||
assert FritzService.DIAL in services
|
||||
|
||||
|
||||
async def test_service_set_guest_wifi_password(
|
||||
@@ -56,7 +53,7 @@ async def test_service_set_guest_wifi_password(
|
||||
"homeassistant.components.fritz.coordinator.AvmWrapper.async_trigger_set_guest_password"
|
||||
) as mock_async_trigger_set_guest_password:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_GUEST_WIFI_PW, {"device_id": device.id}
|
||||
DOMAIN, FritzService.SET_GUEST_WIFI_PASSWORD, {"device_id": device.id}
|
||||
)
|
||||
assert mock_async_trigger_set_guest_password.called
|
||||
|
||||
@@ -86,7 +83,7 @@ async def test_service_set_guest_wifi_password_unknown_parameter(
|
||||
side_effect=FritzServiceError("boom"),
|
||||
) as mock_async_trigger_set_guest_password:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_GUEST_WIFI_PW, {"device_id": device.id}
|
||||
DOMAIN, FritzService.SET_GUEST_WIFI_PASSWORD, {"device_id": device.id}
|
||||
)
|
||||
assert mock_async_trigger_set_guest_password.called
|
||||
assert "HomeAssistantError: Action or parameter unknown" in caplog.text
|
||||
@@ -117,7 +114,7 @@ async def test_service_set_guest_wifi_password_service_not_supported(
|
||||
side_effect=FritzConnectionException("boom"),
|
||||
) as mock_async_trigger_set_guest_password:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_GUEST_WIFI_PW, {"device_id": device.id}
|
||||
DOMAIN, FritzService.SET_GUEST_WIFI_PASSWORD, {"device_id": device.id}
|
||||
)
|
||||
assert mock_async_trigger_set_guest_password.called
|
||||
assert "HomeAssistantError: Action not supported" in caplog.text
|
||||
@@ -135,7 +132,7 @@ async def test_service_set_guest_wifi_password_unloaded(
|
||||
"homeassistant.components.fritz.coordinator.AvmWrapper.async_trigger_set_guest_password"
|
||||
) as mock_async_trigger_set_guest_password:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_GUEST_WIFI_PW, {"device_id": "12345678"}
|
||||
DOMAIN, FritzService.SET_GUEST_WIFI_PASSWORD, {"device_id": "12345678"}
|
||||
)
|
||||
assert not mock_async_trigger_set_guest_password.called
|
||||
assert (
|
||||
@@ -169,7 +166,7 @@ async def test_service_dial(
|
||||
) as mock_async_trigger_dial:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{"device_id": device.id, "number": "1234567890", "max_ring_seconds": 10},
|
||||
)
|
||||
assert mock_async_trigger_dial.called
|
||||
@@ -203,7 +200,7 @@ async def test_service_dial_unknown_parameter(
|
||||
) as mock_async_trigger_dial:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{"device_id": device.id, "number": "1234567890", "max_ring_seconds": 10},
|
||||
)
|
||||
assert mock_async_trigger_dial.called
|
||||
@@ -236,7 +233,7 @@ async def test_service_dial_wrong_parameter(
|
||||
with pytest.raises(MultipleInvalid):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{
|
||||
"device_id": device.id,
|
||||
"number": "1234567890",
|
||||
@@ -250,7 +247,7 @@ async def test_service_dial_wrong_parameter(
|
||||
with pytest.raises(MultipleInvalid):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{
|
||||
"device_id": device.id,
|
||||
"number": "1234567890",
|
||||
@@ -286,7 +283,7 @@ async def test_service_dial_service_not_supported(
|
||||
) as mock_async_trigger_dial:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{"device_id": device.id, "number": "1234567890", "max_ring_seconds": 10},
|
||||
)
|
||||
assert mock_async_trigger_dial.called
|
||||
@@ -319,7 +316,7 @@ async def test_service_dial_failed(
|
||||
) as mock_async_trigger_dial:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{"device_id": device.id, "number": "1234567890", "max_ring_seconds": 10},
|
||||
)
|
||||
assert mock_async_trigger_dial.called
|
||||
@@ -343,12 +340,12 @@ async def test_service_dial_unloaded(
|
||||
) as mock_async_trigger_dial:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_DIAL,
|
||||
FritzService.DIAL,
|
||||
{"device_id": "12345678", "number": "1234567890", "max_ring_seconds": 10},
|
||||
)
|
||||
assert not mock_async_trigger_dial.called
|
||||
assert (
|
||||
"ServiceValidationError: Failed to perform action"
|
||||
f' "{SERVICE_DIAL}".'
|
||||
f' "{FritzService.DIAL}".'
|
||||
" Config entry for target not found" in caplog.text
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user