Refactor
This commit is contained in:
parent
fe5270dd81
commit
4cf5a90748
@ -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))
|
||||
|
44
custom_components/browser_mod/connection.py
Normal file
44
custom_components/browser_mod/connection.py
Normal file
@ -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))
|
1
custom_components/browser_mod/const.py
Normal file
1
custom_components/browser_mod/const.py
Normal file
@ -0,0 +1 @@
|
||||
DOMAIN = "browser_mod"
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user