diff --git a/custom_components/browser_mod/__init__.py b/custom_components/browser_mod/__init__.py index 45fd2c7..bbec55f 100644 --- a/custom_components/browser_mod/__init__.py +++ b/custom_components/browser_mod/__init__.py @@ -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 diff --git a/custom_components/browser_mod/connection.py b/custom_components/browser_mod/connection.py index 966ec50..ed77d4f 100644 --- a/custom_components/browser_mod/connection.py +++ b/custom_components/browser_mod/connection.py @@ -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 diff --git a/custom_components/browser_mod/service.py b/custom_components/browser_mod/service.py new file mode 100644 index 0000000..0bbd814 --- /dev/null +++ b/custom_components/browser_mod/service.py @@ -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) diff --git a/custom_components/browser_mod/services.yaml b/custom_components/browser_mod/services.yaml new file mode 100644 index 0000000..a1c58ba --- /dev/null +++ b/custom_components/browser_mod/services.yaml @@ -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"]'