Check exists hardware for audio/gpio devices (#753)

* Update hardware.py

* Update addon.py

* Update hardware.py

* Update addon.py
This commit is contained in:
Pascal Vizeli 2018-10-12 10:22:58 +02:00 committed by GitHub
parent 468cb0c36b
commit 7dbbcf24c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -1,7 +1,6 @@
"""Init file for Hass.io add-on Docker object.""" """Init file for Hass.io add-on Docker object."""
import logging import logging
import os import os
from pathlib import Path
import docker import docker
import requests import requests
@ -101,7 +100,7 @@ class DockerAddon(DockerInterface):
devices = self.addon.devices or [] devices = self.addon.devices or []
# Use audio devices # Use audio devices
if self.addon.with_audio and AUDIO_DEVICE not in devices: if self.addon.with_audio and self.sys_hardware.support_audio:
devices.append(AUDIO_DEVICE) devices.append(AUDIO_DEVICE)
# Auto mapping UART devices # Auto mapping UART devices
@ -216,10 +215,8 @@ class DockerAddon(DockerInterface):
# Init other hardware mappings # Init other hardware mappings
# GPIO support # GPIO support
if self.addon.with_gpio: if self.addon.with_gpio and self.sys_hardware.support_gpio:
for gpio_path in ("/sys/class/gpio", "/sys/devices/platform/soc"): for gpio_path in ("/sys/class/gpio", "/sys/devices/platform/soc"):
if not Path(gpio_path).exists():
continue
volumes.update({ volumes.update({
gpio_path: { gpio_path: {
'bind': gpio_path, 'mode': 'rw' 'bind': gpio_path, 'mode': 'rw'

View File

@ -20,6 +20,7 @@ PROC_STAT = Path("/proc/stat")
RE_BOOT_TIME = re.compile(r"btime (\d+)") RE_BOOT_TIME = re.compile(r"btime (\d+)")
GPIO_DEVICES = Path("/sys/class/gpio") GPIO_DEVICES = Path("/sys/class/gpio")
SOC_DEVICES = Path("/sys/devices/platform/soc")
RE_TTY = re.compile(r"tty[A-Z]+") RE_TTY = re.compile(r"tty[A-Z]+")
@ -60,6 +61,11 @@ class Hardware:
return dev_list return dev_list
@property
def support_audio(self):
"""Return True if the system have audio support."""
return bool(self.audio_devices)
@property @property
def audio_devices(self): def audio_devices(self):
"""Return all available audio interfaces.""" """Return all available audio interfaces."""
@ -68,10 +74,8 @@ class Hardware:
return {} return {}
try: try:
with ASOUND_CARDS.open('r') as cards_file: cards = ASOUND_CARDS.read_text()
cards = cards_file.read() devices = ASOUND_DEVICES.read_text()
with ASOUND_DEVICES.open('r') as devices_file:
devices = devices_file.read()
except OSError as err: except OSError as err:
_LOGGER.error("Can't read asound data: %s", err) _LOGGER.error("Can't read asound data: %s", err)
return {} return {}
@ -97,6 +101,11 @@ class Hardware:
return audio_list return audio_list
@property
def support_gpio(self):
"""Return True if device support GPIOs."""
return SOC_DEVICES.exists() and GPIO_DEVICES.exists()
@property @property
def gpio_devices(self): def gpio_devices(self):
"""Return list of GPIO interface on device.""" """Return list of GPIO interface on device."""