More stable startup

This commit is contained in:
2020-10-23 11:29:38 +02:00
parent 9f365de4ce
commit b645a1ff94
4 changed files with 65 additions and 19 deletions

View File

@@ -1,14 +1,19 @@
import logging
import voluptuous as vol
from homeassistant.components.websocket_api import websocket_command, result_message, event_message, async_register_command
from homeassistant.helpers.entity import Entity, async_generate_entity_id
from homeassistant.components.websocket_api import (
websocket_command,
result_message,
event_message,
async_register_command
)
from .const import DOMAIN, WS_CONNECT, WS_UPDATE, WS_CAMERA
from .helpers import get_devices, create_entity, get_config
from .const import WS_CONNECT, WS_UPDATE
from .helpers import get_devices, create_entity, get_config, is_setup_complete
_LOGGER = logging.getLogger(__name__)
async def setup_connection(hass, config):
@websocket_command({
@@ -18,7 +23,8 @@ async def setup_connection(hass, config):
def handle_connect(hass, connection, msg):
deviceID = msg["deviceID"]
device = get_devices(hass).get(deviceID, BrowserModConnection(hass, deviceID))
device = get_devices(hass).get(deviceID,
BrowserModConnection(hass, deviceID))
device.connect(connection, msg["id"])
get_devices(hass)[deviceID] = device
@@ -29,7 +35,7 @@ async def setup_connection(hass, config):
vol.Required("deviceID"): str,
vol.Optional("data"): dict,
})
def handle_update( hass, connection, msg):
def handle_update(hass, connection, msg):
devices = get_devices(hass)
deviceID = msg["deviceID"]
if deviceID in devices:
@@ -38,6 +44,7 @@ async def setup_connection(hass, config):
async_register_command(hass, handle_connect)
async_register_command(hass, handle_update)
class BrowserModConnection:
def __init__(self, hass, deviceID):
self.hass = hass
@@ -52,7 +59,7 @@ class BrowserModConnection:
def connect(self, connection, cid):
self.connection.append((connection, cid))
self.send("update", **get_config(self.hass, self.deviceID))
self.trigger_update()
def disconnect():
self.connection.remove((connection, cid))
@@ -67,6 +74,10 @@ class BrowserModConnection:
**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(
@@ -112,4 +123,3 @@ class BrowserModConnection:
self)
if self.camera:
self.camera.data = data.get('camera')