Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Add additional services (#22)
Browse files Browse the repository at this point in the history
* LoadStartURL and LoadURL services

* Additional services

* Add play_audio service, support boolean config
  • Loading branch information
bjw-s authored Oct 16, 2020
1 parent 9902ed6 commit 0c3a1c0
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 27 deletions.
13 changes: 13 additions & 0 deletions custom_components/fullykiosk/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
COORDINATOR = "coordinator"
CONTROLLER = "controller"

ATTR_APPLICATION = "application"
ATTR_CONFIG_TYPE = "config_type"
ATTR_KEY = "key"
ATTR_STREAM = "stream"
ATTR_URL = "url"
ATTR_VALUE = "value"

AUDIOMANAGER_STREAM_MUSIC = 3

SERVICE_LOAD_START_URL = "load_start_url"
SERVICE_LOAD_URL = "load_url"
SERVICE_PLAY_AUDIO = "play_audio"
SERVICE_RESTART_APP = "restart"
SERVICE_SET_CONFIG = "set_config"
SERVICE_START_APPLICATION = "start_application"
SERVICE_TO_FOREGROUND = "to_foreground"
2 changes: 1 addition & 1 deletion custom_components/fullykiosk/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/fullykiosk",
"requirements": [
"python-fullykiosk==0.0.5"
"python-fullykiosk==0.0.6"
],
"ssdp": [],
"zeroconf": [],
Expand Down
140 changes: 115 additions & 25 deletions custom_components/fullykiosk/media_player.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
"""Fully Kiosk Browser media_player entity."""
import logging
import voluptuous as vol

from homeassistant.helpers import config_validation as cv, entity_platform, service

import voluptuous as vol
from homeassistant.components.media_player import (
ATTR_MEDIA_VOLUME_LEVEL,
SERVICE_VOLUME_SET,
SUPPORT_PLAY_MEDIA,
SUPPORT_VOLUME_SET,
MediaPlayerEntity,
)

SUPPORT_FULLYKIOSK = SUPPORT_PLAY_MEDIA | SUPPORT_VOLUME_SET
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import entity_platform

from .const import (
DOMAIN,
COORDINATOR,
CONTROLLER,
ATTR_APPLICATION,
ATTR_CONFIG_TYPE,
ATTR_KEY,
ATTR_STREAM,
AUDIOMANAGER_STREAM_MUSIC
ATTR_URL,
ATTR_VALUE,
AUDIOMANAGER_STREAM_MUSIC,
CONTROLLER,
COORDINATOR,
DOMAIN,
SERVICE_LOAD_START_URL,
SERVICE_LOAD_URL,
SERVICE_PLAY_AUDIO,
SERVICE_RESTART_APP,
SERVICE_SET_CONFIG,
SERVICE_START_APPLICATION,
SERVICE_TO_FOREGROUND,
)

SUPPORT_FULLYKIOSK = SUPPORT_PLAY_MEDIA | SUPPORT_VOLUME_SET


_LOGGER = logging.getLogger(__name__)


Expand All @@ -32,17 +45,54 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

platform = entity_platform.current_platform.get()

# This will call Entity.set_fullykiosk_volume_level(volume_level=VALUE, stream=VALUE)
platform.async_register_entity_service(
SERVICE_LOAD_START_URL, {}, "async_fullykiosk_load_start_url"
)

platform.async_register_entity_service(
SERVICE_LOAD_URL, {vol.Required(ATTR_URL): cv.url}, "async_fullykiosk_load_url"
)

platform.async_register_entity_service(
SERVICE_PLAY_AUDIO,
{
vol.Required(ATTR_URL): cv.string,
vol.Required(ATTR_STREAM): vol.All(vol.Number(scale=0), vol.Range(1, 10)),
},
"async_fullykiosk_play_audio",
)

platform.async_register_entity_service(
SERVICE_RESTART_APP, {}, "async_fullykiosk_restart"
)

platform.async_register_entity_service(
SERVICE_SET_CONFIG,
{
vol.Required(ATTR_CONFIG_TYPE): vol.In(["string", "bool"]),
vol.Required(ATTR_KEY): cv.string,
vol.Required(ATTR_VALUE): vol.Any(cv.string, cv.boolean),
},
"async_fullykiosk_set_config",
)

platform.async_register_entity_service(
SERVICE_VOLUME_SET,
{
vol.Required(ATTR_MEDIA_VOLUME_LEVEL): cv.small_float,
vol.Required(ATTR_STREAM): vol.All(
vol.Number(scale=0),
vol.Range(1, 10),
),
vol.Required(ATTR_STREAM): vol.All(vol.Number(scale=0), vol.Range(1, 10)),
},
"async_set_fullykiosk_volume_level",
"async_fullykiosk_set_volume_level",
)

platform.async_register_entity_service(
SERVICE_START_APPLICATION,
{vol.Required(ATTR_APPLICATION): cv.string},
"async_fullykiosk_start_app",
)

platform.async_register_entity_service(
SERVICE_TO_FOREGROUND, {}, "async_fullykiosk_to_foreground"
)

async_add_entities([FullyMediaPlayer(coordinator, controller)], False)
Expand Down Expand Up @@ -79,24 +129,64 @@ def device_info(self):
def unique_id(self):
return self._unique_id

def fullykiosk_set_volume_level(self, volume_level, stream):
"""Set volume level for a stream, range 0..1."""
self.controller.setAudioVolume(int(volume_level * 100), stream)

def fullykiosk_play_audio(self, url, stream):
"""Play a piece of audio on a specific stream."""
self.controller.playSound(url, stream)

def play_media(self, media_type, media_id, **kwargs):
self.controller.playSound(media_id)
"""Play a piece of media."""
self.fullykiosk_play_audio(media_id, AUDIOMANAGER_STREAM_MUSIC)

def set_fullykiosk_volume_level(self, volume_level, stream):
def set_volume_level(self, volume_level):
"""Set volume level, range 0..1."""
self.fullykiosk_set_volume_level(volume_level, AUDIOMANAGER_STREAM_MUSIC)

async def async_fullykiosk_load_start_url(self):
"""Load the start URL on a fullykiosk browser."""
await self.hass.async_add_executor_job(self.controller.loadStartUrl)

async def async_fullykiosk_load_url(self, url):
"""Load URL on a fullykiosk browser."""
await self.hass.async_add_executor_job(self.controller.loadUrl, url)

async def async_fullykiosk_play_audio(self, url, stream):
"""Play a piece of audio on a specific stream."""
await self.hass.async_add_executor_job(self.fullykiosk_play_audio, url, stream)

async def async_fullykiosk_restart(self):
"""Restart the fullykiosk browser app."""
await self.hass.async_add_executor_job(self.controller.restartApp)

async def async_fullykiosk_set_config(self, config_type, key, value):
"""Set fullykiosk configuration value."""
if config_type == "string":
await self.hass.async_add_executor_job(
self.controller.setConfigurationString, key, value
)
elif config_type == "bool":
await self.hass.async_add_executor_job(
self.controller.setConfigurationBool, key, value
)

async def async_fullykiosk_set_volume_level(self, volume_level, stream):
"""Set volume level for a stream, range 0..1."""
self.controller.sendCommand(
cmd="setAudioVolume", level=str(int(volume_level * 100)), stream=str(stream)
await self.hass.async_add_executor_job(
self.fullykiosk_set_volume_level, volume_level, stream
)

async def async_set_fullykiosk_volume_level(self, volume_level, stream):
"""Set volume level for a stream, range 0..1."""
async def async_fullykiosk_start_app(self, application):
"""Start an application on the device running the fullykiosk browser app."""
await self.hass.async_add_executor_job(
self.set_fullykiosk_volume_level, volume_level, stream
self.controller.startApplication, application
)

def set_volume_level(self, volume_level):
"""Set volume level, range 0..1."""
self.set_fullykiosk_volume_level(volume_level=volume_level, stream=AUDIOMANAGER_STREAM_MUSIC)
async def async_fullykiosk_to_foreground(self):
"""Bring the fullykiosk browser app back to the foreground."""
await self.hass.async_add_executor_job(self.controller.toForeground)

async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications."""
Expand Down
72 changes: 71 additions & 1 deletion custom_components/fullykiosk/services.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,77 @@
# Describes the format for available fullykiosk services

load_start_url:
description: Load Fully Kiosk Browser start URL.
fields:
entity_id:
description: Name(s) of entities to load the start URL on.
example: "media_player.amazon_fire_media_player"

load_url:
description: Load URL on Fully Kiosk Browser.
fields:
entity_id:
description: Name(s) of entities to load the start URL on.
example: "media_player.amazon_fire_media_player"
url:
description: URL to be loaded.
example: "https://www.fully-kiosk.com"

play_audio:
description: Play audio on a Fully Kiosk Browser.
fields:
entity_id:
description: Name(s) of entities to play media on
example: "media_player.amazon_fire_media_player"
url:
description: The URL of the audio to play.
example: "https://home-assistant.io/audio/test.mp3"
stream:
description: Stream on which to play the audio
example: 3

restart:
description: Restart the Fully Kiosk Browser app.
fields:
entity_id:
description: Name(s) of entities to restart the app on.
example: "media_player.amazon_fire_media_player"

set_config:
description: Modify a setting in Fully Kiosk Browser.
fields:
entity_id:
description: Name(s) of entities to modify the setting on.
example: "media_player.amazon_fire_media_player"
config_type:
description: Type of setting (either 'string' or 'bool')
example: "string"
key:
description: Key of the setting to change
example: "startURL"
value:
description: New value of the setting
example: "https://www.fully-kiosk.com"

start_application:
description: Restart the Fully Kiosk Browser app.
fields:
entity_id:
description: Name(s) of entities to restart the app on.
example: "media_player.amazon_fire_media_player"
application:
description: Package name of the application to start.
example: "de.ozerov.fully"

to_foreground:
description: Bring the Fully Kiosk Browser app to the foreground.
fields:
entity_id:
description: Name(s) of entities on which to bring the app to the foreground.
example: "media_player.amazon_fire_media_player"

volume_set:
description: Set a fullykiosk volume level.
description: Set a Fully Kiosk Browser volume level.
fields:
entity_id:
description: Name(s) of entities to set volume level on.
Expand Down

0 comments on commit 0c3a1c0

Please sign in to comment.