More stable startup
This commit is contained in:
parent
9f365de4ce
commit
b645a1ff94
@ -9,7 +9,8 @@ from .const import (
|
|||||||
DATA_ALIASES,
|
DATA_ALIASES,
|
||||||
DATA_ADDERS,
|
DATA_ADDERS,
|
||||||
CONFIG_DEVICES,
|
CONFIG_DEVICES,
|
||||||
DATA_CONFIG
|
DATA_CONFIG,
|
||||||
|
DATA_SETUP_COMPLETE,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -17,8 +18,6 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
|
|
||||||
setup_view(hass)
|
|
||||||
|
|
||||||
aliases = {}
|
aliases = {}
|
||||||
for d in config[DOMAIN].get(CONFIG_DEVICES, {}):
|
for d in config[DOMAIN].get(CONFIG_DEVICES, {}):
|
||||||
name = config[DOMAIN][CONFIG_DEVICES][d].get("name", None)
|
name = config[DOMAIN][CONFIG_DEVICES][d].get("name", None)
|
||||||
@ -30,8 +29,12 @@ async def async_setup(hass, config):
|
|||||||
DATA_ALIASES: aliases,
|
DATA_ALIASES: aliases,
|
||||||
DATA_ADDERS: {},
|
DATA_ADDERS: {},
|
||||||
DATA_CONFIG: config[DOMAIN],
|
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
|
async_load_platform = hass.helpers.discovery.async_load_platform
|
||||||
await async_load_platform("media_player", DOMAIN, {}, config)
|
await async_load_platform("media_player", DOMAIN, {}, config)
|
||||||
await async_load_platform("sensor", 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 async_load_platform("camera", DOMAIN, {}, config)
|
||||||
|
|
||||||
await setup_service(hass)
|
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
|
return True
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
import logging
|
import logging
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.websocket_api import websocket_command, result_message, event_message, async_register_command
|
from homeassistant.components.websocket_api import (
|
||||||
from homeassistant.helpers.entity import Entity, async_generate_entity_id
|
websocket_command,
|
||||||
|
result_message,
|
||||||
|
event_message,
|
||||||
|
async_register_command
|
||||||
|
)
|
||||||
|
|
||||||
from .const import DOMAIN, WS_CONNECT, WS_UPDATE, WS_CAMERA
|
from .const import WS_CONNECT, WS_UPDATE
|
||||||
from .helpers import get_devices, create_entity, get_config
|
from .helpers import get_devices, create_entity, get_config, is_setup_complete
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def setup_connection(hass, config):
|
async def setup_connection(hass, config):
|
||||||
|
|
||||||
@websocket_command({
|
@websocket_command({
|
||||||
@ -18,7 +23,8 @@ async def setup_connection(hass, config):
|
|||||||
def handle_connect(hass, connection, msg):
|
def handle_connect(hass, connection, msg):
|
||||||
deviceID = msg["deviceID"]
|
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"])
|
device.connect(connection, msg["id"])
|
||||||
get_devices(hass)[deviceID] = device
|
get_devices(hass)[deviceID] = device
|
||||||
|
|
||||||
@ -38,6 +44,7 @@ async def setup_connection(hass, config):
|
|||||||
async_register_command(hass, handle_connect)
|
async_register_command(hass, handle_connect)
|
||||||
async_register_command(hass, handle_update)
|
async_register_command(hass, handle_update)
|
||||||
|
|
||||||
|
|
||||||
class BrowserModConnection:
|
class BrowserModConnection:
|
||||||
def __init__(self, hass, deviceID):
|
def __init__(self, hass, deviceID):
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
@ -52,7 +59,7 @@ class BrowserModConnection:
|
|||||||
|
|
||||||
def connect(self, connection, cid):
|
def connect(self, connection, cid):
|
||||||
self.connection.append((connection, cid))
|
self.connection.append((connection, cid))
|
||||||
self.send("update", **get_config(self.hass, self.deviceID))
|
self.trigger_update()
|
||||||
|
|
||||||
def disconnect():
|
def disconnect():
|
||||||
self.connection.remove((connection, cid))
|
self.connection.remove((connection, cid))
|
||||||
@ -67,6 +74,10 @@ class BrowserModConnection:
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
def trigger_update(self):
|
||||||
|
if is_setup_complete(self.hass):
|
||||||
|
self.send("update", **get_config(self.hass, self.deviceID))
|
||||||
|
|
||||||
def update(self, data):
|
def update(self, data):
|
||||||
if data.get('browser'):
|
if data.get('browser'):
|
||||||
self.sensor = self.sensor or create_entity(
|
self.sensor = self.sensor or create_entity(
|
||||||
@ -112,4 +123,3 @@ class BrowserModConnection:
|
|||||||
self)
|
self)
|
||||||
if self.camera:
|
if self.camera:
|
||||||
self.camera.data = data.get('camera')
|
self.camera.data = data.get('camera')
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ DATA_DEVICES = "devices"
|
|||||||
DATA_ALIASES = "aliases"
|
DATA_ALIASES = "aliases"
|
||||||
DATA_ADDERS = "adders"
|
DATA_ADDERS = "adders"
|
||||||
DATA_CONFIG = "config"
|
DATA_CONFIG = "config"
|
||||||
|
DATA_SETUP_COMPLETE = "setup_complete"
|
||||||
|
|
||||||
CONFIG_DEVICES = "devices"
|
CONFIG_DEVICES = "devices"
|
||||||
CONFIG_PREFIX = "prefix"
|
CONFIG_PREFIX = "prefix"
|
||||||
|
@ -2,35 +2,53 @@ import logging
|
|||||||
|
|
||||||
from homeassistant.helpers.entity import Entity, async_generate_entity_id
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_devices(hass):
|
def get_devices(hass):
|
||||||
return hass.data[DOMAIN][DATA_DEVICES]
|
return hass.data[DOMAIN][DATA_DEVICES]
|
||||||
|
|
||||||
|
|
||||||
def get_alias(hass, deviceID):
|
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:
|
if v == deviceID:
|
||||||
return k
|
return k
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_config(hass, deviceID):
|
def get_config(hass, deviceID):
|
||||||
config = hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DEVICES, {})
|
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):
|
def create_entity(hass, platform, deviceID, connection):
|
||||||
conf = get_config(hass, deviceID)
|
conf = get_config(hass, deviceID)
|
||||||
if conf and (platform in conf.get(CONFIG_DISABLE, [])
|
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
|
return None
|
||||||
if not conf and (platform in hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DISABLE, [])
|
if not conf and \
|
||||||
or CONFIG_DISABLE_ALL in hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DISABLE, [])):
|
(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
|
return None
|
||||||
adder = hass.data[DOMAIN][DATA_ADDERS][platform]
|
adder = hass.data[DOMAIN][DATA_ADDERS][platform]
|
||||||
entity = adder(hass, deviceID, connection, get_alias(hass, deviceID))
|
entity = adder(hass, deviceID, connection, get_alias(hass, deviceID))
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, async_add_devices, platform, cls):
|
def setup_platform(hass, config, async_add_devices, platform, cls):
|
||||||
def adder(hass, deviceID, connection, alias=None):
|
def adder(hass, deviceID, connection, alias=None):
|
||||||
entity = cls(hass, connection, deviceID, alias)
|
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
|
hass.data[DOMAIN][DATA_ADDERS][platform] = adder
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def is_setup_complete(hass):
|
||||||
|
return hass.data[DOMAIN][DATA_SETUP_COMPLETE]
|
||||||
|
|
||||||
|
|
||||||
class BrowserModEntity(Entity):
|
class BrowserModEntity(Entity):
|
||||||
|
|
||||||
def __init__(self, hass, connection, deviceID, alias=None):
|
def __init__(self, hass, connection, deviceID, alias=None):
|
||||||
@ -47,7 +70,11 @@ class BrowserModEntity(Entity):
|
|||||||
self.deviceID = deviceID
|
self.deviceID = deviceID
|
||||||
self._data = {}
|
self._data = {}
|
||||||
prefix = hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_PREFIX, '')
|
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):
|
def updated(self):
|
||||||
pass
|
pass
|
||||||
@ -55,6 +82,7 @@ class BrowserModEntity(Entity):
|
|||||||
@property
|
@property
|
||||||
def data(self):
|
def data(self):
|
||||||
return self._data
|
return self._data
|
||||||
|
|
||||||
@data.setter
|
@data.setter
|
||||||
def data(self, data):
|
def data(self, data):
|
||||||
self._data = data
|
self._data = data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user