Add support for udev trigger

This commit is contained in:
Pascal Vizeli 2019-09-02 11:28:49 +00:00
parent 38268ea4ea
commit c563f484c9
6 changed files with 27 additions and 2 deletions

4
API.md
View File

@ -350,6 +350,10 @@ Load host configs from a USB stick.
}
```
- POST `/hardware/trigger`
Trigger an udev reload
### Home Assistant
- GET `/homeassistant/info`

View File

@ -471,6 +471,7 @@ class Addon(AddonModel):
if self.with_audio:
self.write_asound()
# Start Add-on
try:
await self.instance.run()
except DockerAPIError:

View File

@ -101,6 +101,7 @@ class RestAPI(CoreSysAttributes):
[
web.get("/hardware/info", api_hardware.info),
web.get("/hardware/audio", api_hardware.audio),
web.post("/hardware/trigger", api_hardware.trigger),
]
)

View File

@ -1,5 +1,9 @@
"""Init file for Hass.io hardware RESTful API."""
import asyncio
import logging
from typing import Any, Dict
from aiohttp import web
from .utils import api_process
from ..const import (
@ -19,7 +23,7 @@ class APIHardware(CoreSysAttributes):
"""Handle RESTful API for hardware functions."""
@api_process
async def info(self, request):
async def info(self, request: web.Request) -> Dict[str, Any]:
"""Show hardware info."""
return {
ATTR_SERIAL: list(
@ -32,7 +36,7 @@ class APIHardware(CoreSysAttributes):
}
@api_process
async def audio(self, request):
async def audio(self, request: web.Request) -> Dict[str, Any]:
"""Show ALSA audio devices."""
return {
ATTR_AUDIO: {
@ -40,3 +44,8 @@ class APIHardware(CoreSysAttributes):
ATTR_OUTPUT: self.sys_host.alsa.output_devices,
}
}
@api_process
def trigger(self, request: web.Request) -> None:
"""Trigger a udev device reload."""
return asyncio.shield(self.sys_hardware.udev_trigger())

View File

@ -41,6 +41,7 @@ ADDONS_API_BYPASS = re.compile(
r"^(?:"
r"|/addons/self/(?!security|update)[^/]+"
r"|/info"
r"|/hardware/.+"
r"|/services.*"
r"|/discovery.*"
r"|/auth"

View File

@ -1,4 +1,5 @@
"""Read hardware info from system."""
import asyncio
from datetime import datetime
import logging
from pathlib import Path
@ -148,3 +149,11 @@ class Hardware:
return None
return datetime.utcfromtimestamp(int(found.group(1)))
async def udev_trigger(self) -> None:
"""Trigger a udev reload."""
proc = await asyncio.create_subprocess_exec("udevadm", "trigger")
await proc.wait()
if proc.returncode != 0:
_LOGGER.waring("udevadm device triggering fails!")