Lots of changes and modernization. WIP

This commit is contained in:
2022-07-13 21:02:47 +00:00
parent 69e9642b4b
commit 466a5eb5e7
26 changed files with 1691 additions and 1185 deletions

View File

@@ -1,5 +1,6 @@
import logging
import voluptuous as vol
from datetime import datetime, timezone
from homeassistant.components.websocket_api import (
websocket_command,
@@ -8,115 +9,138 @@ from homeassistant.components.websocket_api import (
async_register_command,
)
from .const import WS_CONNECT, WS_UPDATE
from homeassistant.components import websocket_api
from .const import WS_CONNECT, WS_REGISTER, WS_UNREGISTER, WS_REREGISTER, WS_UPDATE, DOMAIN
from .helpers import get_devices, create_entity, get_config, is_setup_complete
from .coordinator import Coordinator
from .device import getDevice
_LOGGER = logging.getLogger(__name__)
async def setup_connection(hass, config):
@websocket_command(
async def async_setup_connection(hass):
@websocket_api.websocket_command(
{
vol.Required("type"): WS_CONNECT,
vol.Required("deviceID"): str,
}
)
def handle_connect(hass, connection, msg):
@websocket_api.async_response
async def handle_connect(hass, connection, msg):
deviceID = msg["deviceID"]
store = hass.data[DOMAIN]["store"]
device = get_devices(hass).get(deviceID, BrowserModConnection(hass, deviceID))
device.connect(connection, msg["id"])
get_devices(hass)[deviceID] = device
def listener(data):
connection.send_message(event_message(msg["id"], {"result": data}))
connection.send_message(result_message(msg["id"]))
connection.subscriptions[msg["id"]] = store.add_listener(listener)
connection.send_result(msg["id"])
@websocket_command(
if store.get_device(deviceID).enabled:
dev = getDevice(hass, deviceID)
dev.connection = (connection, msg["id"])
await store.set_device(deviceID,
last_seen=datetime.now(
tz=timezone.utc
).isoformat()
)
listener(store.asdict())
@websocket_api.websocket_command(
{
vol.Required("type"): WS_REGISTER,
vol.Required("deviceID"): str,
}
)
@websocket_api.async_response
async def handle_register(hass, connection, msg):
deviceID = msg["deviceID"]
store = hass.data[DOMAIN]["store"]
await store.set_device(deviceID,
enabled=True
)
connection.send_result(msg["id"])
@websocket_api.websocket_command(
{
vol.Required("type"): WS_UNREGISTER,
vol.Required("deviceID"): str,
}
)
@websocket_api.async_response
async def handle_unregister(hass, connection, msg):
deviceID = msg["deviceID"]
store = hass.data[DOMAIN]["store"]
devices = hass.data[DOMAIN]["devices"]
if deviceID in devices:
devices[deviceID].delete(hass)
del devices[deviceID]
await store.delete_device(deviceID)
connection.send_result(msg["id"])
@websocket_api.websocket_command(
{
vol.Required("type"): WS_REREGISTER,
vol.Required("deviceID"): str,
vol.Required("data"): dict,
}
)
@websocket_api.async_response
async def handle_reregister(hass, connection, msg):
deviceID = msg["deviceID"]
store = hass.data[DOMAIN]["store"]
devices = hass.data[DOMAIN]["devices"]
data = msg["data"]
del data["last_seen"]
device = {}
if "deviceID" in data:
newDeviceID = data["deviceID"]
del data["deviceID"]
oldDevice = store.get_device(deviceID)
if oldDevice:
device = oldDevice.asdict()
await store.delete_device(deviceID)
if deviceID in devices:
devices[deviceID].delete(hass)
del devices[deviceID]
deviceID = newDeviceID
device.update(data)
await store.set_device(deviceID, **device)
@websocket_api.websocket_command(
{
vol.Required("type"): WS_UPDATE,
vol.Required("deviceID"): str,
vol.Optional("data"): dict,
}
)
def handle_update(hass, connection, msg):
devices = get_devices(hass)
@websocket_api.async_response
async def handle_update(hass, connection, msg):
deviceID = msg["deviceID"]
if deviceID in devices and is_setup_complete(hass):
devices[deviceID].update(msg.get("data", None))
store = hass.data[DOMAIN]["store"]
devices = hass.data[DOMAIN]["devices"]
if store.get_device(deviceID).enabled:
dev = getDevice(hass, deviceID)
dev.data.update(msg.get("data", {}))
dev.coordinator.async_set_updated_data(dev.data)
async_register_command(hass, handle_connect)
async_register_command(hass, handle_update)
class BrowserModConnection:
def __init__(self, hass, deviceID):
self.hass = hass
self.deviceID = deviceID
self.connection = []
self.media_player = None
self.screen = None
self.sensor = None
self.fully = None
self.camera = None
def connect(self, connection, cid):
self.connection.append((connection, cid))
self.trigger_update()
def disconnect():
self.connection.remove((connection, cid))
connection.subscriptions[cid] = disconnect
def send(self, command, **kwargs):
if self.connection:
connection, cid = self.connection[-1]
connection.send_message(
event_message(
cid,
{
"command": command,
**kwargs,
},
)
)
def trigger_update(self):
if is_setup_complete(self.hass):
self.send("update", **get_config(self.hass, self.deviceID))
def update(self, data):
if data.get("browser"):
self.sensor = self.sensor or create_entity(
self.hass, "sensor", self.deviceID, self
)
if self.sensor:
self.sensor.data = data.get("browser")
if data.get("player"):
self.media_player = self.media_player or create_entity(
self.hass, "media_player", self.deviceID, self
)
if self.media_player:
self.media_player.data = data.get("player")
if data.get("screen"):
self.screen = self.screen or create_entity(
self.hass, "light", self.deviceID, self
)
if self.screen:
self.screen.data = data.get("screen")
if data.get("fully"):
self.fully = self.fully or create_entity(
self.hass, "binary_sensor", self.deviceID, self
)
if self.fully:
self.fully.data = data.get("fully")
if data.get("camera"):
self.camera = self.camera or create_entity(
self.hass, "camera", self.deviceID, self
)
if self.camera:
self.camera.data = data.get("camera")
async_register_command(hass, handle_register)
async_register_command(hass, handle_unregister)
async_register_command(hass, handle_reregister)
async_register_command(hass, handle_update)