Renamed Device to Browser throughout

This commit is contained in:
2022-07-18 20:51:07 +00:00
parent 3bf2481e5b
commit acc4a15e02
18 changed files with 227 additions and 369 deletions

View File

@@ -1,17 +1,10 @@
import logging
from homeassistant.helpers.entity_registry import (
async_entries_for_config_entry,
async_entries_for_device,
)
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.helpers import device_registry, area_registry
from homeassistant.helpers import device_registry
from .const import (
DOMAIN,
DATA_DEVICES,
DATA_ALIASES,
USER_COMMANDS,
DATA_BROWSERS,
)
_LOGGER = logging.getLogger(__name__)
@@ -20,29 +13,31 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_services(hass):
def call_service(service, targets, data):
devices = hass.data[DOMAIN][DATA_DEVICES]
browsers = hass.data[DOMAIN][DATA_BROWSERS]
if isinstance(targets, str):
targets = [targets]
# If no targets were specified, send to all browsers
if len(targets) == 0:
targets = browsers.keys()
for target in targets:
if target not in devices:
if target not in browsers:
continue
device = devices[target]
device.send(service, **data)
browser = browsers[target]
browser.send(service, **data)
def handle_service(call):
service = call.service
data = {**call.data}
device_ids = set(data.get("device_id", []))
data.pop("device_id", None)
area_ids = set(data.get("area_id", []))
data.pop("area_id", None)
targets = data.get("target", [])
if isinstance(targets, str):
targets = [targets]
targets = set(targets)
data.pop("target", None)
browsers = data.pop("browser_id", [])
if isinstance(browsers, str):
browsers = [browsers]
browsers = set(browsers)
device_ids = set(data.pop("device_id", []))
area_ids = set(data.pop("area_id", []))
dr = device_registry.async_get(hass)
@@ -53,53 +48,16 @@ async def async_setup_services(hass):
browserID = list(dev.identifiers)[0][1]
if browserID is None:
continue
targets.add(browserID)
browsers.add(browserID)
for area in area_ids:
for dev in device_registry.async_entries_for_area(dr, area):
browserID = list(dev.identifiers)[0][1]
if browserID is None:
continue
targets.add(browserID)
browsers.add(browserID)
_LOGGER.error(service)
_LOGGER.error(targets)
_LOGGER.error(data)
call_service(service, targets, data)
call_service(service, browsers, data)
hass.services.async_register(DOMAIN, "test", handle_service)
hass.services.async_register(DOMAIN, "popup", handle_service)
async def setup_service(hass):
def handle_command(call):
command = call.data.get("command", None)
if not command:
return
targets = call.data.get("deviceID", None)
if isinstance(targets, str):
targets = [targets]
devices = hass.data[DOMAIN][DATA_DEVICES]
aliases = hass.data[DOMAIN][DATA_ALIASES]
if not targets:
targets = devices.keys()
targets = [aliases.get(t, t) for t in targets]
data = dict(call.data)
del data["command"]
for t in targets:
if t in devices:
devices[t].send(command, **data)
def command_wrapper(call):
command = call.service.replace("_", "-")
call.data = dict(call.data)
call.data["command"] = command
handle_command(call)
hass.services.async_register(DOMAIN, "command", handle_command)
for cmd in USER_COMMANDS:
hass.services.async_register(DOMAIN, cmd.replace("-", "_"), command_wrapper)