Support USB discovery (#3100)

* Support USB discovery

* fix policy

* Fix scan command

* address comments

Co-authored-by: Ludeeus <ludeeus@ludeeus.dev>
This commit is contained in:
Pascal Vizeli 2021-09-02 20:34:18 +02:00 committed by GitHub
parent 208fb549b7
commit 32af7ef28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,13 +18,17 @@ from ..const import (
ATTR_PORT, ATTR_PORT,
ATTR_REFRESH_TOKEN, ATTR_REFRESH_TOKEN,
ATTR_SSL, ATTR_SSL,
ATTR_TYPE,
ATTR_UUID, ATTR_UUID,
ATTR_VERSION, ATTR_VERSION,
ATTR_WAIT_BOOT, ATTR_WAIT_BOOT,
ATTR_WATCHDOG, ATTR_WATCHDOG,
FILE_HASSIO_HOMEASSISTANT, FILE_HASSIO_HOMEASSISTANT,
BusEvent,
) )
from ..coresys import CoreSys, CoreSysAttributes from ..coresys import CoreSys, CoreSysAttributes
from ..hardware.const import PolicyGroup
from ..hardware.data import Device
from ..utils.common import FileConfiguration from ..utils.common import FileConfiguration
from .api import HomeAssistantAPI from .api import HomeAssistantAPI
from .core import HomeAssistantCore from .core import HomeAssistantCore
@ -238,6 +242,9 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
"""Prepare Home Assistant object.""" """Prepare Home Assistant object."""
await asyncio.wait([self.secrets.load(), self.core.load()]) await asyncio.wait([self.secrets.load(), self.core.load()])
# Register for events
self.sys_bus.register_event(BusEvent.HARDWARE_NEW_DEVICE, self._hardware_events)
def write_pulse(self): def write_pulse(self):
"""Write asound config to file and return True on success.""" """Write asound config to file and return True on success."""
pulse_config = self.sys_plugins.audio.pulse_client( pulse_config = self.sys_plugins.audio.pulse_client(
@ -256,3 +263,20 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
_LOGGER.error("Home Assistant can't write pulse/client.config: %s", err) _LOGGER.error("Home Assistant can't write pulse/client.config: %s", err)
else: else:
_LOGGER.info("Update pulse/client.config: %s", self.path_pulse) _LOGGER.info("Update pulse/client.config: %s", self.path_pulse)
async def _hardware_events(self, device: Device) -> None:
"""Process hardware requests."""
if (
not self.sys_hardware.policy.is_match_cgroup(PolicyGroup.UART, device)
or not self.version
or self.version < "2021.9.0"
):
return
configuration = await self.sys_homeassistant.websocket.async_send_command(
{ATTR_TYPE: "get_config"}
)
if not configuration or "usb" not in configuration.get("components", []):
return
self.sys_homeassistant.websocket.send_command({ATTR_TYPE: "usb/scan"})