diff --git a/API.md b/API.md index 9211fc110ac..ddf383234a9 100644 --- a/API.md +++ b/API.md @@ -176,6 +176,11 @@ Optional: ### Network - GET `/network/info` +```json +{ + "hostname": "" +} +``` - POST `/network/options` ```json diff --git a/hassio/api/network.py b/hassio/api/network.py index 1d622edcabe..8c42b3a05d2 100644 --- a/hassio/api/network.py +++ b/hassio/api/network.py @@ -1,11 +1,19 @@ """Init file for HassIO network rest api.""" import logging -from .util import api_process_hostcontrol +import voluptuous as vol + +from .util import api_process, api_process_hostcontrol, api_validate +from ..const import ATTR_HOSTNAME _LOGGER = logging.getLogger(__name__) +SCHEMA_OPTIONS = vol.Schema({ + vol.Optional(ATTR_HOSTNAME): vol.Coerce(str), +}) + + class APINetwork(object): """Handle rest api for network functions.""" @@ -15,12 +23,21 @@ def __init__(self, config, loop, host_control): self.loop = loop self.host_control = host_control - @api_process_hostcontrol - def info(self, request): + @api_process + async def info(self, request): """Show network settings.""" - pass + return { + ATTR_HOSTNAME: self.host_control.hostname, + } @api_process_hostcontrol - def options(self, request): + async def options(self, request): """Edit network settings.""" - pass + body = await api_validate(SCHEMA_OPTIONS, request) + + # hostname + if ATTR_HOSTNAME in body: + if self.host_control.hostname != body[ATTR_HOSTNAME]: + await self.host_control.set_hostname(body[ATTR_HOSTNAME]) + + return True diff --git a/hassio/host_control.py b/hassio/host_control.py index 845742f27ac..54290e40625 100644 --- a/hassio/host_control.py +++ b/hassio/host_control.py @@ -17,6 +17,7 @@ FEATURES_SHUTDOWN = 'shutdown' FEATURES_REBOOT = 'reboot' FEATURES_UPDATE = 'update' +FEATURES_HOSTNAME = 'hostname' FEATURES_NETWORK_INFO = 'network_info' FEATURES_NETWORK_CONTROL = 'network_control' @@ -117,3 +118,7 @@ def update(self, version=None): if version: return self._send_command("update {}".format(version)) return self._send_command("update") + + def set_hostname(self, hostname): + """Update hostname on host.""" + return self._send_command("hostname {}".format(hostname))