More stable startup
This commit is contained in:
parent
9f365de4ce
commit
b645a1ff94
@ -9,7 +9,8 @@ from .const import (
|
||||
DATA_ALIASES,
|
||||
DATA_ADDERS,
|
||||
CONFIG_DEVICES,
|
||||
DATA_CONFIG
|
||||
DATA_CONFIG,
|
||||
DATA_SETUP_COMPLETE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -17,8 +18,6 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_setup(hass, config):
|
||||
|
||||
setup_view(hass)
|
||||
|
||||
aliases = {}
|
||||
for d in config[DOMAIN].get(CONFIG_DEVICES, {}):
|
||||
name = config[DOMAIN][CONFIG_DEVICES][d].get("name", None)
|
||||
@ -30,8 +29,12 @@ async def async_setup(hass, config):
|
||||
DATA_ALIASES: aliases,
|
||||
DATA_ADDERS: {},
|
||||
DATA_CONFIG: config[DOMAIN],
|
||||
DATA_SETUP_COMPLETE: False,
|
||||
}
|
||||
|
||||
await setup_connection(hass, config)
|
||||
setup_view(hass)
|
||||
|
||||
async_load_platform = hass.helpers.discovery.async_load_platform
|
||||
await async_load_platform("media_player", DOMAIN, {}, config)
|
||||
await async_load_platform("sensor", DOMAIN, {}, config)
|
||||
@ -40,6 +43,10 @@ async def async_setup(hass, config):
|
||||
await async_load_platform("camera", DOMAIN, {}, config)
|
||||
|
||||
await setup_service(hass)
|
||||
await setup_connection(hass, config)
|
||||
|
||||
hass.data[DOMAIN][DATA_SETUP_COMPLETE] = True
|
||||
|
||||
for device in hass.data[DOMAIN][DATA_DEVICES].values():
|
||||
device.trigger_update()
|
||||
|
||||
return True
|
||||
|
@ -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')
|
||||
|
||||
|
@ -8,6 +8,7 @@ DATA_DEVICES = "devices"
|
||||
DATA_ALIASES = "aliases"
|
||||
DATA_ADDERS = "adders"
|
||||
DATA_CONFIG = "config"
|
||||
DATA_SETUP_COMPLETE = "setup_complete"
|
||||
|
||||
CONFIG_DEVICES = "devices"
|
||||
CONFIG_PREFIX = "prefix"
|
||||
|
@ -2,35 +2,53 @@ import logging
|
||||
|
||||
from homeassistant.helpers.entity import Entity, async_generate_entity_id
|
||||
|
||||
from .const import DOMAIN, DATA_DEVICES, DATA_ALIASES, DATA_ADDERS, CONFIG_DEVICES, DATA_CONFIG, CONFIG_PREFIX, CONFIG_DISABLE, CONFIG_DISABLE_ALL
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
DATA_DEVICES,
|
||||
DATA_ALIASES,
|
||||
DATA_ADDERS,
|
||||
CONFIG_DEVICES,
|
||||
DATA_CONFIG,
|
||||
CONFIG_PREFIX,
|
||||
CONFIG_DISABLE,
|
||||
CONFIG_DISABLE_ALL,
|
||||
DATA_SETUP_COMPLETE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_devices(hass):
|
||||
return hass.data[DOMAIN][DATA_DEVICES]
|
||||
|
||||
|
||||
def get_alias(hass, deviceID):
|
||||
for k,v in hass.data[DOMAIN][DATA_ALIASES].items():
|
||||
for k, v in hass.data[DOMAIN][DATA_ALIASES].items():
|
||||
if v == deviceID:
|
||||
return k
|
||||
return None
|
||||
|
||||
|
||||
def get_config(hass, deviceID):
|
||||
config = hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DEVICES, {})
|
||||
return config.get(deviceID, config.get(deviceID.replace('-','_'), {}))
|
||||
return config.get(deviceID, config.get(deviceID.replace('-', '_'), {}))
|
||||
|
||||
|
||||
def create_entity(hass, platform, deviceID, connection):
|
||||
conf = get_config(hass, deviceID)
|
||||
if conf and (platform in conf.get(CONFIG_DISABLE, [])
|
||||
or CONFIG_DISABLE_ALL in conf.get(CONFIG_DISABLE, [])):
|
||||
or CONFIG_DISABLE_ALL in conf.get(CONFIG_DISABLE, [])):
|
||||
return None
|
||||
if not conf and (platform in hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DISABLE, [])
|
||||
or CONFIG_DISABLE_ALL in hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DISABLE, [])):
|
||||
if not conf and \
|
||||
(platform in hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DISABLE, [])
|
||||
or CONFIG_DISABLE_ALL in
|
||||
hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DISABLE, [])):
|
||||
return None
|
||||
adder = hass.data[DOMAIN][DATA_ADDERS][platform]
|
||||
entity = adder(hass, deviceID, connection, get_alias(hass, deviceID))
|
||||
return entity
|
||||
|
||||
|
||||
def setup_platform(hass, config, async_add_devices, platform, cls):
|
||||
def adder(hass, deviceID, connection, alias=None):
|
||||
entity = cls(hass, connection, deviceID, alias)
|
||||
@ -39,6 +57,11 @@ def setup_platform(hass, config, async_add_devices, platform, cls):
|
||||
hass.data[DOMAIN][DATA_ADDERS][platform] = adder
|
||||
return True
|
||||
|
||||
|
||||
def is_setup_complete(hass):
|
||||
return hass.data[DOMAIN][DATA_SETUP_COMPLETE]
|
||||
|
||||
|
||||
class BrowserModEntity(Entity):
|
||||
|
||||
def __init__(self, hass, connection, deviceID, alias=None):
|
||||
@ -47,7 +70,11 @@ class BrowserModEntity(Entity):
|
||||
self.deviceID = deviceID
|
||||
self._data = {}
|
||||
prefix = hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_PREFIX, '')
|
||||
self.entity_id = async_generate_entity_id(self.domain+".{}", alias or f"{prefix}{deviceID}", hass=hass)
|
||||
self.entity_id = async_generate_entity_id(
|
||||
self.domain+".{}",
|
||||
alias or f"{prefix}{deviceID}",
|
||||
hass=hass
|
||||
)
|
||||
|
||||
def updated(self):
|
||||
pass
|
||||
@ -55,6 +82,7 @@ class BrowserModEntity(Entity):
|
||||
@property
|
||||
def data(self):
|
||||
return self._data
|
||||
|
||||
@data.setter
|
||||
def data(self, data):
|
||||
self._data = data
|
||||
|
Loading…
x
Reference in New Issue
Block a user