Skip to content

Commit

Permalink
add capability for manually specifying IP and port of Konnected devic…
Browse files Browse the repository at this point in the history
…e(s)
  • Loading branch information
heythisisnate committed Sep 29, 2018
1 parent 7d1960b commit b2b8c63
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
45 changes: 39 additions & 6 deletions homeassistant/components/konnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import logging
import hmac
import json
import time
import voluptuous as vol
import konnected

from aiohttp.hdrs import AUTHORIZATION
from aiohttp.web import Request, Response
Expand All @@ -16,17 +18,17 @@
from homeassistant.components.discovery import SERVICE_KONNECTED
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import (
HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_UNAUTHORIZED,
CONF_DEVICES, CONF_BINARY_SENSORS, CONF_SWITCHES, CONF_HOST, CONF_PORT,
CONF_ID, CONF_NAME, CONF_TYPE, CONF_PIN, CONF_ZONE, CONF_ACCESS_TOKEN,
ATTR_ENTITY_ID, ATTR_STATE, STATE_ON)
EVENT_HOMEASSISTANT_START, HTTP_BAD_REQUEST, HTTP_NOT_FOUND,
HTTP_UNAUTHORIZED, CONF_DEVICES, CONF_BINARY_SENSORS, CONF_SWITCHES,
CONF_HOST, CONF_PORT, CONF_ID, CONF_NAME, CONF_TYPE, CONF_PIN, CONF_ZONE,
CONF_ACCESS_TOKEN, ATTR_ENTITY_ID, ATTR_STATE, STATE_ON)
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers import discovery
from homeassistant.helpers import config_validation as cv

_LOGGER = logging.getLogger(__name__)

REQUIREMENTS = ['konnected==0.1.2']
REQUIREMENTS = ['konnected==0.1.3']

DOMAIN = 'konnected'

Expand Down Expand Up @@ -81,6 +83,8 @@
cv.ensure_list, [_BINARY_SENSOR_SCHEMA]),
vol.Optional(CONF_SWITCHES): vol.All(
cv.ensure_list, [_SWITCH_SCHEMA]),
vol.Optional(CONF_HOST): cv.string,
vol.Optional(CONF_PORT): cv.positive_int,
}],
}),
},
Expand Down Expand Up @@ -119,6 +123,35 @@ def device_discovered(service, info):
" but not specified in configuration.yaml",
discovered.device_id)

def manual_discovery(event):
"""Init devices on the network with manually assigned addresses."""
specified = list(filter( # list of devices where host and port is set
lambda dev: dev.get(CONF_HOST) and dev.get(CONF_PORT),
cfg.get(CONF_DEVICES)))

while specified:
for dev in specified:
_LOGGER.debug("Discovering Konnected device %s at %s:%s",
dev.get(CONF_ID),
dev.get(CONF_HOST),
dev.get(CONF_PORT))
try:
discovered = DiscoveredDevice(hass,
dev.get(CONF_HOST),
dev.get(CONF_PORT))
if discovered.is_configured:
discovered.setup()
specified.remove(dev)
else:
_LOGGER.err("""
Konnected device %s was manually configured, but
its mac address is not found in configuration.yaml
""", discovered.device_id)
except konnected.Client.ClientError as err:
_LOGGER.error(err)
time.sleep(10) # try again in 10 seconds

# Initialize devices specified in the configuration on boot
for device in cfg.get(CONF_DEVICES):
ConfiguredDevice(hass, device).save_data()

Expand All @@ -128,6 +161,7 @@ def device_discovered(service, info):
device_discovered)

hass.http.register_view(KonnectedView(access_token))
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, manual_discovery)

return True

Expand Down Expand Up @@ -214,7 +248,6 @@ def __init__(self, hass, host, port):
self.host = host
self.port = port

import konnected
self.client = konnected.Client(host, str(port))
self.status = self.client.get_status()

Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ keyrings.alt==3.1
kiwiki-client==0.1.1

# homeassistant.components.konnected
konnected==0.1.2
konnected==0.1.3

# homeassistant.components.eufy
lakeside==0.10
Expand Down

0 comments on commit b2b8c63

Please sign in to comment.