diff --git a/custom_components/browser_mod/__init__.py b/custom_components/browser_mod/__init__.py index 4213cea..34368b6 100644 --- a/custom_components/browser_mod/__init__.py +++ b/custom_components/browser_mod/__init__.py @@ -1,11 +1,8 @@ import logging -import voluptuous as vol - -from homeassistant.components.websocket_api import websocket_command, result_message, event_message, async_register_command from .mod_view import setup_view - -DOMAIN = "browser_mod" +from .connection import setup_connection +from .const import DOMAIN FRONTEND_SCRIPT_URL = "/browser_mod.js" @@ -34,39 +31,6 @@ async def async_setup(hass, config): _LOGGER.error(f"Set up media_player") _LOGGER.error(hass.data[DOMAIN]["adders"]) - async_register_command(hass, handle_connect) - async_register_command(hass, handle_update) - _LOGGER.error(f"Registered connect ws command") + setup_connection(hass) return True - - - -@websocket_command({ - vol.Required("type"): "browser_mod/connect", - vol.Required("deviceID"): str, -}) -def handle_connect(hass, connection, msg): - _LOGGER.error(f"Got connection {msg}") - - devices = hass.data[DOMAIN]["devices"] - deviceID = msg["deviceID"] - if deviceID in devices: - devices[deviceID].ws_connect(connection, msg["id"]) - else: - adder = hass.data[DOMAIN]["adders"][0] - devices[deviceID] = adder(hass, deviceID, connection, msg["id"]) - connection.send_message(result_message(msg["id"])) - - -@websocket_command({ - vol.Required("type"): "browser_mod/update", - vol.Required("deviceID"): str, - vol.Optional("browser"): dict, - vol.Optional("player"): dict, -}) -def handle_update(hass, connection, msg): - devices = hass.data[DOMAIN]["devices"] - deviceID = msg["deviceID"] - if deviceID in devices: - devices[deviceID].ws_update(msg.get("browser", None), msg.get("player", None)) diff --git a/custom_components/browser_mod/connection.py b/custom_components/browser_mod/connection.py new file mode 100644 index 0000000..223b933 --- /dev/null +++ b/custom_components/browser_mod/connection.py @@ -0,0 +1,44 @@ +import logging +import voluptuous as vol + +from homeassistant.components.websocket_api import websocket_command, result_message, event_message, async_register_command + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +def setup_connection(hass): + async_register_command(hass, handle_connect) + async_register_command(hass, handle_update) + _LOGGER.error(f"Registered connect ws command") + + + +@websocket_command({ + vol.Required("type"): "browser_mod/connect", + vol.Required("deviceID"): str, +}) +def handle_connect(hass, connection, msg): + _LOGGER.error(f"Got connection {msg}") + + devices = hass.data[DOMAIN]["devices"] + deviceID = msg["deviceID"] + if deviceID in devices: + devices[deviceID].ws_connect(connection, msg["id"]) + else: + adder = hass.data[DOMAIN]["adders"][0] + devices[deviceID] = adder(hass, deviceID, connection, msg["id"]) + connection.send_message(result_message(msg["id"])) + + +@websocket_command({ + vol.Required("type"): "browser_mod/update", + vol.Required("deviceID"): str, + vol.Optional("browser"): dict, + vol.Optional("player"): dict, +}) +def handle_update(hass, connection, msg): + devices = hass.data[DOMAIN]["devices"] + deviceID = msg["deviceID"] + if deviceID in devices: + devices[deviceID].ws_update(msg.get("browser", None), msg.get("player", None)) diff --git a/custom_components/browser_mod/const.py b/custom_components/browser_mod/const.py new file mode 100644 index 0000000..f750028 --- /dev/null +++ b/custom_components/browser_mod/const.py @@ -0,0 +1 @@ +DOMAIN = "browser_mod" diff --git a/custom_components/browser_mod/media_player.py b/custom_components/browser_mod/media_player.py index 35290e0..913fe65 100644 --- a/custom_components/browser_mod/media_player.py +++ b/custom_components/browser_mod/media_player.py @@ -1,9 +1,8 @@ import logging from homeassistant.components.media_player import MediaPlayerDevice - from homeassistant.components.websocket_api import event_message -from . import DOMAIN +from .const import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -42,12 +41,19 @@ class BrowserModPlayer(MediaPlayerDevice): "browser": self.browser_state, } + def ws_send(self, command, data=None): + data = data or {} + self.connection.send_message(event_message(self.cid, { + "command": command, + **data, + })) + + def ws_connect(self, connection, cid): self.cid = cid self.connection = connection - _LOGGER.error(f"Connecting player {self.entity_id}") - connection.send_message(event_message(cid, {"command": "update"})) - pass + _LOGGER.error(f"Connecting {self.entity_id}") + self.ws_send("update") def ws_update(self, browser, player): self.browser_state = browser