Improved thread safety

This commit is contained in:
Thomas Lovén 2022-08-16 20:42:35 +00:00
parent ae9ffb65c1
commit 39f727206f
11 changed files with 82 additions and 8 deletions

View File

@ -3,6 +3,7 @@ import logging
from homeassistant.components.websocket_api import event_message from homeassistant.components.websocket_api import event_message
from homeassistant.helpers import device_registry, entity_registry from homeassistant.helpers import device_registry, entity_registry
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.core import callback
from .const import DATA_BROWSERS, DOMAIN, DATA_ADDERS from .const import DATA_BROWSERS, DOMAIN, DATA_ADDERS
from .sensor import BrowserSensor from .sensor import BrowserSensor
@ -130,11 +131,14 @@ class BrowserModBrowser:
er.async_remove(self.entities["camera"].entity_id) er.async_remove(self.entities["camera"].entity_id)
del self.entities["camera"] del self.entities["camera"]
self.send( hass.create_task(
None, browserEntities={k: v.entity_id for k, v in self.entities.items()} self.send(
None, browserEntities={k: v.entity_id for k, v in self.entities.items()}
)
) )
def send(self, command, **kwargs): @callback
async def send(self, command, **kwargs):
"""Send a command to this browser.""" """Send a command to this browser."""
if self.connection is None: if self.connection is None:
return return

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,8 @@ from homeassistant.components.websocket_api import (
from homeassistant.components import websocket_api from homeassistant.components import websocket_api
from homeassistant.core import callback
from .const import ( from .const import (
BROWSER_ID, BROWSER_ID,
DATA_STORE, DATA_STORE,
@ -40,6 +42,7 @@ async def async_setup_connection(hass):
browserID = msg[BROWSER_ID] browserID = msg[BROWSER_ID]
store = hass.data[DOMAIN][DATA_STORE] store = hass.data[DOMAIN][DATA_STORE]
@callback
def send_update(data): def send_update(data):
connection.send_message(event_message(msg["id"], {"result": data})) connection.send_message(event_message(msg["id"], {"result": data}))

View File

@ -27,7 +27,7 @@ async def async_setup_services(hass):
if target not in browsers: if target not in browsers:
continue continue
browser = browsers[target] browser = browsers[target]
browser.send(service, **data) hass.create_task(browser.send(service, **data))
def handle_service(call): def handle_service(call):
service = call.service service = call.service

View File

@ -13,7 +13,7 @@ export const ConnectionMixin = (SuperClass) => {
public browserEntities = {}; public browserEntities = {};
LOG(...args) { LOG(...args) {
return; if (window.browser_mod_log === undefined) return;
const dt = new Date(); const dt = new Date();
console.log(`${dt.toLocaleTimeString()}`, ...args); console.log(`${dt.toLocaleTimeString()}`, ...args);
@ -37,6 +37,7 @@ export const ConnectionMixin = (SuperClass) => {
this.update_config(msg.result); this.update_config(msg.result);
} }
this._connectionResolve?.(); this._connectionResolve?.();
this._connectionResolve = undefined;
} }
private update_config(cfg) { private update_config(cfg) {

View File

@ -32,6 +32,7 @@ interface FullyKiosk {
declare global { declare global {
interface Window { interface Window {
browser_mod?: BrowserMod; browser_mod?: BrowserMod;
browser_mod_log?: any;
fully?: FullyKiosk; fully?: FullyKiosk;
hassConnection?: Promise<any>; hassConnection?: Promise<any>;
customCards?: [{}?]; customCards?: [{}?];

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "browser_mod", "name": "browser_mod",
"version": "2.0.0b2", "version": "2.0.0b4",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

31
test/automations.yaml Normal file
View File

@ -0,0 +1,31 @@
- id: "1660669793583"
alias: Toggle bed light
description: ""
trigger:
- platform: time_pattern
seconds: /3
condition: []
action:
- type: toggle
device_id: 98861bdf58b3c79183c03be06da14f27
entity_id: light.bed_light
domain: light
mode: single
- alias: Popup when kitchen light togggled
trigger:
- platform: state
entity_id: light.kitchen_lights
action:
- service: browser_mod.sequence
data:
sequence:
- service: delay
data:
time: 5000
- service: browser_mod.popup
data:
title: automation
content:
type: markdown
content: "{%raw%}{{states('light.bed_light')}}{%endraw%}"

View File

@ -1,5 +1,7 @@
default_config: default_config:
automation: !include test/automations.yaml
demo: demo:
http: http:
@ -12,6 +14,8 @@ logger:
logs: logs:
custom_components.browser_mod: info custom_components.browser_mod: info
# debugpy:
# browser_mod: # browser_mod:
# devices: # devices:
# camdevice: # camdevice:

View File

@ -32,6 +32,7 @@ views:
action: more-info action: more-info
- !include views/popup.yaml - !include views/popup.yaml
- !include views/frontend-backend.yaml
- title: Popup card - title: Popup card
popup_cards: popup_cards:

View File

@ -0,0 +1,30 @@
title: frontend vs backend
cards:
- type: entities
entities:
- light.bed_light
- light.kitchen_lights
- type: button
name: fire-dom-event
tap_action:
action: fire-dom-event
browser_mod:
service: browser_mod.popup
data:
title: fire-dom-event
content:
type: markdown
content: "{{states('light.bed_light')}}"
- type: button
name: call-service
tap_action:
action: call-service
service: browser_mod.popup
data:
title: call-service
content:
type: markdown
content: "{{states('light.bed_light')}}"