Skip to content

Commit

Permalink
add constants suggested by @syssi
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisisnate committed May 15, 2018
1 parent 254ad68 commit 6049128
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
26 changes: 16 additions & 10 deletions homeassistant/components/konnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from homeassistant.const import (
HTTP_BAD_REQUEST, HTTP_INTERNAL_SERVER_ERROR, HTTP_UNAUTHORIZED,
CONF_DEVICES, CONF_BINARY_SENSORS, CONF_SWITCHES, CONF_HOST, CONF_PORT,
CONF_ID, CONF_NAME, CONF_TYPE, CONF_PIN, CONF_ZONE, ATTR_STATE)
CONF_ID, CONF_NAME, CONF_TYPE, CONF_PIN, CONF_ZONE, CONF_ACCESS_TOKEN,
ATTR_STATE)
from homeassistant.helpers import discovery
from homeassistant.helpers import config_validation as cv

Expand All @@ -28,6 +29,10 @@

DOMAIN = 'konnected'

CONF_ACTIVATION = 'activation'
STATE_LOW = 'low'
STATE_HIGH = 'high'

PIN_TO_ZONE = {1: 1, 2: 2, 5: 3, 6: 4, 7: 5, 8: 'out', 9: 6}
ZONE_TO_PIN = {zone: pin for pin, zone in PIN_TO_ZONE.items()}

Expand All @@ -45,15 +50,15 @@
vol.Exclusive(CONF_PIN, 'a_pin'): vol.Any(*PIN_TO_ZONE),
vol.Exclusive(CONF_ZONE, 'a_pin'): vol.Any(*ZONE_TO_PIN),
vol.Optional(CONF_NAME): cv.string,
vol.Optional('activation', default='high'):
vol.All(vol.Lower, vol.Any('high', 'low'))
vol.Optional(CONF_ACTIVATION, default=STATE_HIGH):
vol.All(vol.Lower, vol.Any(STATE_HIGH, STATE_LOW))
}), cv.has_at_least_one_key(CONF_PIN, CONF_ZONE)
)

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema({
vol.Required('auth_token'): cv.string,
vol.Required(CONF_ACCESS_TOKEN): cv.string,
vol.Required(CONF_DEVICES): [{
vol.Required(CONF_ID): cv.string,
vol.Optional(CONF_BINARY_SENSORS): vol.All(
Expand All @@ -78,9 +83,9 @@ async def async_setup(hass, config):
if cfg is None:
cfg = {}

auth_token = cfg.get('auth_token')
access_token = cfg.get(CONF_ACCESS_TOKEN)
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {'auth_token': auth_token}
hass.data[DOMAIN] = {CONF_ACCESS_TOKEN: access_token}

async def async_device_discovered(service, info):
"""Call when a Konnected device has been discovered."""
Expand All @@ -96,7 +101,7 @@ async def async_device_discovered(service, info):
SERVICE_KONNECTED,
async_device_discovered)

hass.http.register_view(KonnectedView(auth_token))
hass.http.register_view(KonnectedView(access_token))

return True

Expand Down Expand Up @@ -195,7 +200,7 @@ def save_data(self):
CONF_NAME, 'Konnected {} Actuator {}'.format(
self.device_id[6:], PIN_TO_ZONE[pin])),
ATTR_STATE: initial_state,
'activation': entity['activation'],
CONF_ACTIVATION: entity[CONF_ACTIVATION],
}
_LOGGER.debug('Set up actuator %s (initial state: %s)',
actuators[pin].get(CONF_NAME),
Expand Down Expand Up @@ -228,7 +233,8 @@ def sensor_configuration(self):
def actuator_configuration(self):
"""Return the configuration map for syncing actuators."""
return [{'pin': p,
'trigger': (0 if data.get('activation') in [0, 'low'] else 1)}
'trigger': (0 if data.get(CONF_ACTIVATION) in [0, STATE_LOW]
else 1)}
for p, data in
self.stored_configuration[CONF_SWITCHES].items()]

Expand All @@ -255,7 +261,7 @@ async def async_sync_device_config(self):
self.client.put_settings(
desired_sensor_configuration,
desired_actuator_config,
self.hass.data[DOMAIN].get('auth_token'),
self.hass.data[DOMAIN].get(CONF_ACCESS_TOKEN),
self.hass.config.api.base_url + ENDPOINT_ROOT
)

Expand Down
11 changes: 7 additions & 4 deletions homeassistant/components/switch/konnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import asyncio
import logging

from homeassistant.components.konnected import (DOMAIN, PIN_TO_ZONE)
from homeassistant.components.konnected import (
DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION, STATE_LOW, STATE_HIGH)
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import (CONF_DEVICES, CONF_SWITCHES, ATTR_STATE)

Expand Down Expand Up @@ -41,7 +42,7 @@ def __init__(self, device_id, pin_num, data, client):
self._device_id = device_id
self._pin_num = pin_num
self._state = self._data.get(ATTR_STATE)
self._activation = self._data.get('activation', 'high')
self._activation = self._data.get(CONF_ACTIVATION, STATE_HIGH)
self._name = self._data.get(
'name', 'Konnected {} Actuator {}'.format(
device_id, PIN_TO_ZONE[pin_num]))
Expand All @@ -61,12 +62,14 @@ def is_on(self):

def turn_on(self, **kwargs):
"""Send a command to turn on the switch."""
self._client.put_device(self._pin_num, int(self._activation == 'high'))
self._client.put_device(self._pin_num,
int(self._activation == STATE_HIGH))
self._set_state(True)

def turn_off(self, **kwargs):
"""Send a command to turn off the switch."""
self._client.put_device(self._pin_num, int(self._activation == 'low'))
self._client.put_device(self._pin_num,
int(self._activation == STATE_LOW))
self._set_state(False)

def _set_state(self, state):
Expand Down

0 comments on commit 6049128

Please sign in to comment.