From 700639b9da5812e632d6dbdecab54bae633c4742 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Sun, 9 Sep 2018 21:19:20 -0400 Subject: [PATCH] Add API endpoint to get state of a Konnected switch --- homeassistant/components/konnected.py | 39 ++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/konnected.py b/homeassistant/components/konnected.py index 28cadcefa4a8bc..388fa41f36f890 100644 --- a/homeassistant/components/konnected.py +++ b/homeassistant/components/konnected.py @@ -19,7 +19,7 @@ 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) + 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 @@ -328,6 +328,43 @@ def __init__(self, auth_token): """Initialize the view.""" self.auth_token = auth_token + @staticmethod + def binary_value(state, activation): + """Return binary value for GPIO based on state and activation.""" + if activation == STATE_HIGH: + return 1 if state == STATE_ON else 0 + return 0 if state == STATE_ON else 1 + + async def get(self, request: Request, device_id) -> Response: + """Return the current binary state of a switch.""" + hass = request.app['hass'] + pin_num = int(request.query.get('pin')) + data = hass.data[DOMAIN] + + device = data[CONF_DEVICES][device_id] + if not device: + return self.json_message( + 'Device ' + device_id + ' not configured', + status_code=HTTP_NOT_FOUND) + + try: + pin = next(filter( + lambda switch: switch[CONF_PIN] == pin_num, + device[CONF_SWITCHES])) + except StopIteration: + pin = None + + if not pin: + return self.json_message( + 'Switch on pin ' + pin_num + ' not configured', + status_code=HTTP_NOT_FOUND) + + return self.json( + {'pin': pin_num, + 'state': self.binary_value( + hass.states.get(pin[ATTR_ENTITY_ID]).state, + pin[CONF_ACTIVATION])}) + async def put(self, request: Request, device_id, pin_num=None, state=None) -> Response: """Receive a sensor update via PUT request and async set state."""