Service to send command to browsers
This commit is contained in:
parent
4149759dd1
commit
f58467445c
@ -2,6 +2,7 @@ import logging
|
|||||||
|
|
||||||
from .mod_view import setup_view
|
from .mod_view import setup_view
|
||||||
from .connection import setup_connection
|
from .connection import setup_connection
|
||||||
|
from .service import setup_service
|
||||||
from .const import DOMAIN, DATA_DEVICES, DATA_ALIASES, DATA_ADDERS, CONFIG_DEVICES
|
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_connection(hass)
|
||||||
|
|
||||||
|
setup_service(hass)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -53,10 +53,11 @@ class BrowserModEntity(Entity):
|
|||||||
self.entity_id = async_generate_entity_id("media_player.{}", alias or deviceID, hass=hass)
|
self.entity_id = async_generate_entity_id("media_player.{}", alias or deviceID, hass=hass)
|
||||||
|
|
||||||
def ws_send(self, command, **kwargs):
|
def ws_send(self, command, **kwargs):
|
||||||
self._ws_connection.send_message(event_message(self._ws_cid, {
|
if self._ws_connection:
|
||||||
"command": command,
|
self._ws_connection.send_message(event_message(self._ws_cid, {
|
||||||
**kwargs,
|
"command": command,
|
||||||
}))
|
**kwargs,
|
||||||
|
}))
|
||||||
|
|
||||||
def ws_connect(self, connection, cid):
|
def ws_connect(self, connection, cid):
|
||||||
self._ws_cid = cid
|
self._ws_cid = cid
|
||||||
|
24
custom_components/browser_mod/service.py
Normal file
24
custom_components/browser_mod/service.py
Normal 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)
|
9
custom_components/browser_mod/services.yaml
Normal file
9
custom_components/browser_mod/services.yaml
Normal 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"]'
|
Loading…
x
Reference in New Issue
Block a user