Skip to content

Commit c691f2a

Browse files
authored
Auto mapping UART devices from host (#276)
* Add hardware to docker api * set hardware to docker * add loop to dns * Use loop for dns * Update const.py * Update API.md * Update validate.py * Update addon.py * Update addon.py * fix lint * style * Update hardware.py
1 parent 110cd32 commit c691f2a

File tree

9 files changed

+25
-11
lines changed

9 files changed

+25
-11
lines changed

API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ Get all available addons.
389389
"host_dbus": "bool",
390390
"privileged": ["NET_ADMIN", "SYS_ADMIN"],
391391
"devices": ["/dev/xy"],
392+
"auto_uart": "bool",
392393
"logo": "bool",
393394
"changelog": "bool",
394395
"hassio_api": "bool",

hassio/addons/addon.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
ATTR_STATE, ATTR_TIMEOUT, ATTR_AUTO_UPDATE, ATTR_NETWORK, ATTR_WEBUI,
2323
ATTR_HASSIO_API, ATTR_AUDIO, ATTR_AUDIO_OUTPUT, ATTR_AUDIO_INPUT,
2424
ATTR_GPIO, ATTR_HOMEASSISTANT_API, ATTR_STDIN, ATTR_LEGACY, ATTR_HOST_IPC,
25-
ATTR_HOST_DBUS)
25+
ATTR_HOST_DBUS, ATTR_AUTO_UART)
2626
from .util import check_installed
2727
from ..dock.addon import DockerAddon
2828
from ..tools import write_json_file, read_json_file
@@ -259,6 +259,11 @@ def devices(self):
259259
"""Return devices of addon."""
260260
return self._mesh.get(ATTR_DEVICES)
261261

262+
@property
263+
def auto_uart(self):
264+
"""Return True if we should map all uart device."""
265+
return self._mesh.get(ATTR_AUTO_UART)
266+
262267
@property
263268
def tmpfs(self):
264269
"""Return tmpfs of addon."""

hassio/addons/validate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
ATTR_AUTO_UPDATE, ATTR_WEBUI, ATTR_AUDIO, ATTR_AUDIO_INPUT, ATTR_HOST_IPC,
1818
ATTR_AUDIO_OUTPUT, ATTR_HASSIO_API, ATTR_BUILD_FROM, ATTR_SQUASH,
1919
ATTR_ARGS, ATTR_GPIO, ATTR_HOMEASSISTANT_API, ATTR_STDIN, ATTR_LEGACY,
20-
ATTR_HOST_DBUS)
20+
ATTR_HOST_DBUS, ATTR_AUTO_UART)
2121
from ..validate import NETWORK_PORT, DOCKER_PORTS, ALSA_CHANNEL
2222

2323
_LOGGER = logging.getLogger(__name__)
@@ -96,6 +96,7 @@ def _simple_startup(value):
9696
vol.Optional(ATTR_HOST_IPC, default=False): vol.Boolean(),
9797
vol.Optional(ATTR_HOST_DBUS, default=False): vol.Boolean(),
9898
vol.Optional(ATTR_DEVICES): [vol.Match(r"^(.*):(.*):([rwm]{1,3})$")],
99+
vol.Optional(ATTR_AUTO_UART, default=False): vol.Boolean(),
99100
vol.Optional(ATTR_TMPFS):
100101
vol.Match(r"^size=(\d)*[kmg](,uid=\d{1,4})?(,rw)?$"),
101102
vol.Optional(ATTR_MAP, default=[]): [vol.Match(RE_VOLUME)],

hassio/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
ATTR_FEATURES = 'features'
6767
ATTR_ADDONS = 'addons'
6868
ATTR_VERSION = 'version'
69+
ATTR_AUTO_UART = 'auto_uart'
6970
ATTR_LAST_BOOT = 'last_boot'
7071
ATTR_LAST_VERSION = 'last_version'
7172
ATTR_BETA_CHANNEL = 'beta_channel'

hassio/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def __init__(self, loop, config):
4242
self.scheduler = Scheduler(loop)
4343
self.api = RestAPI(config, loop)
4444
self.hardware = Hardware()
45-
self.docker = DockerAPI()
46-
self.dns = DNSForward()
45+
self.docker = DockerAPI(self.hardware)
46+
self.dns = DNSForward(loop)
4747

4848
# init basic docker container
4949
self.supervisor = DockerSupervisor(

hassio/dns.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
class DNSForward(object):
1212
"""Manage DNS forwarding to internal DNS."""
1313

14-
def __init__(self):
14+
def __init__(self, loop):
1515
"""Initialize DNS forwarding."""
16+
self.loop = loop
1617
self.proc = None
1718

1819
async def start(self):
@@ -23,6 +24,7 @@ async def start(self):
2324
stdin=asyncio.subprocess.DEVNULL,
2425
stdout=asyncio.subprocess.DEVNULL,
2526
stderr=asyncio.subprocess.DEVNULL,
27+
loop=self.loop
2628
)
2729
except OSError as err:
2830
_LOGGER.error("Can't start DNS forwarding -> %s", err)

hassio/dock/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class DockerAPI(object):
1616
This class is not AsyncIO safe!
1717
"""
1818

19-
def __init__(self):
19+
def __init__(self, hardware):
2020
"""Initialize docker base wrapper."""
2121
self.docker = docker.DockerClient(
2222
base_url="unix:/{}".format(str(SOCKET_DOCKER)), version='auto')
2323
self.network = DockerNetwork(self.docker)
24+
self.hardware = hardware
2425

2526
@property
2627
def images(self):

hassio/dock/addon.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ def devices(self):
8080
"""Return needed devices."""
8181
devices = self.addon.devices or []
8282

83-
# use audio devices
83+
# Use audio devices
8484
if self.addon.with_audio and AUDIO_DEVICE not in devices:
8585
devices.append(AUDIO_DEVICE)
8686

87+
# Auto mapping UART devices
88+
if self.addon.auto_uart:
89+
for uart_dev in self.docker.hardware.serial_devices:
90+
devices.append("{0}:{0}:rwm".format(uart_dev))
91+
8792
# Return None if no devices is present
88-
if devices:
89-
return devices
90-
return None
93+
return devices or None
9194

9295
@property
9396
def ports(self):

hassio/hardware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def serial_devices(self):
3535
"""Return all serial and connected devices."""
3636
dev_list = set()
3737
for device in self.context.list_devices(subsystem='tty'):
38-
if 'ID_VENDOR' in device or RE_TTY.match(device.device_node):
38+
if 'ID_VENDOR' in device or RE_TTY.search(device.device_node):
3939
dev_list.add(device.device_node)
4040

4141
return dev_list

0 commit comments

Comments
 (0)