Skip to content

Commit

Permalink
Add support for hostname change. (#68)
Browse files Browse the repository at this point in the history
* Add support for hostname change.

* Fix lint

* Fix lint p2

* Update network.py
  • Loading branch information
pvizeli authored May 22, 2017
1 parent 0cdef0d commit 97853d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ Optional:
### Network

- GET `/network/info`
```json
{
"hostname": ""
}
```

- POST `/network/options`
```json
Expand Down
29 changes: 23 additions & 6 deletions hassio/api/network.py
Original file line number Diff line number Diff line change
@@ -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."""

Expand All @@ -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
5 changes: 5 additions & 0 deletions hassio/host_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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))

0 comments on commit 97853d1

Please sign in to comment.