Add load_url service to fully_kiosk integration (#79969)

This commit is contained in:
Charles Garwood 2022-10-10 08:11:55 -04:00 committed by GitHub
parent 8fe504356a
commit ffb6434776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 0 deletions

View File

@ -5,6 +5,7 @@ from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import FullyKioskDataUpdateCoordinator
from .services import async_setup_services
PLATFORMS = [
Platform.BINARY_SENSOR,
@ -26,6 +27,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await async_setup_services(hass)
return True

View File

@ -22,3 +22,7 @@ MEDIA_SUPPORT_FULLYKIOSK = (
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.BROWSE_MEDIA
)
SERVICE_LOAD_URL = "load_url"
ATTR_URL = "url"

View File

@ -0,0 +1,41 @@
"""Services for the Fully Kiosk Browser integration."""
from __future__ import annotations
import voluptuous as vol
from homeassistant.const import ATTR_DEVICE_ID
from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.device_registry as dr
from .const import ATTR_URL, DOMAIN, SERVICE_LOAD_URL
async def async_setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Fully Kiosk Browser integration."""
async def async_load_url(call: ServiceCall) -> None:
"""Load a URL on the Fully Kiosk Browser."""
registry = dr.async_get(hass)
for target in call.data[ATTR_DEVICE_ID]:
device = registry.async_get(target)
if device:
coordinator = hass.data[DOMAIN][list(device.config_entries)[0]]
await coordinator.fully.loadUrl(call.data[ATTR_URL])
hass.services.async_register(
DOMAIN,
SERVICE_LOAD_URL,
async_load_url,
schema=vol.Schema(
vol.All(
{
vol.Required(ATTR_DEVICE_ID): cv.ensure_list,
vol.Required(
ATTR_URL,
): cv.string,
},
)
),
)

View File

@ -0,0 +1,14 @@
load_url:
name: Load URL
description: Load a URL on Fully Kiosk Browser
target:
device:
integration: fully_kiosk
fields:
url:
name: URL
description: URL to load.
example: "https://home-assistant.io"
required: true
selector:
text:

View File

@ -0,0 +1,36 @@
"""Test Fully Kiosk Browser services."""
from unittest.mock import MagicMock
from homeassistant.components.fully_kiosk.const import (
ATTR_URL,
DOMAIN,
SERVICE_LOAD_URL,
)
from homeassistant.const import ATTR_DEVICE_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from tests.common import MockConfigEntry
async def test_services(
hass: HomeAssistant,
mock_fully_kiosk: MagicMock,
init_integration: MockConfigEntry,
) -> None:
"""Test the Fully Kiosk Browser services."""
device_registry = dr.async_get(hass)
device_entry = device_registry.async_get_device(
identifiers={(DOMAIN, "abcdef-123456")}
)
assert device_entry
await hass.services.async_call(
DOMAIN,
SERVICE_LOAD_URL,
{ATTR_DEVICE_ID: [device_entry.id], ATTR_URL: "https://example.com"},
blocking=True,
)
assert len(mock_fully_kiosk.loadUrl.mock_calls) == 1