Service to send command to browsers

This commit is contained in:
Thomas Lovén 2019-06-27 13:30:54 +02:00
parent 4149759dd1
commit f58467445c
4 changed files with 41 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import logging
from .mod_view import setup_view
from .connection import setup_connection
from .service import setup_service
from .const import DOMAIN, DATA_DEVICES, DATA_ALIASES, DATA_ADDERS, CONFIG_DEVICES
@ -32,4 +33,6 @@ async def async_setup(hass, config):
setup_connection(hass)
setup_service(hass)
return True

View File

@ -53,10 +53,11 @@ class BrowserModEntity(Entity):
self.entity_id = async_generate_entity_id("media_player.{}", alias or deviceID, hass=hass)
def ws_send(self, command, **kwargs):
self._ws_connection.send_message(event_message(self._ws_cid, {
"command": command,
**kwargs,
}))
if self._ws_connection:
self._ws_connection.send_message(event_message(self._ws_cid, {
"command": command,
**kwargs,
}))
def ws_connect(self, connection, cid):
self._ws_cid = cid

View File

@ -0,0 +1,24 @@
from .const import DOMAIN, DATA_DEVICES, DATA_ALIASES
def setup_service(hass):
def handle_command(call):
command = call.data.get("command", None)
if not command:
return
targets = call.data.get("deviceID", None)
devices = hass.data[DOMAIN][DATA_DEVICES]
aliases = hass.data[DOMAIN][DATA_ALIASES]
if not targets:
targets = devices.keys()
targets = [aliases.get(t, t) for t in targets]
data = dict(call.data)
del data["command"]
for t in targets:
if t in devices:
devices[t].ws_send(command, **data)
hass.services.async_register(DOMAIN, 'command', handle_command)

View File

@ -0,0 +1,9 @@
command:
description: Send a command to a browser
fields:
command:
description: Command to send
example: 'navigate'
deviceID:
description: List of receiving browsers
example: '["99980b13-dabc9563", "office_computer"]'