Skip to content

Commit

Permalink
add config option to invert state of binary_sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisisnate committed Sep 7, 2018
1 parent 7ad094b commit 4bb0fcc
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions homeassistant/components/konnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
CONF_MOMENTARY = 'momentary'
CONF_PAUSE = 'pause'
CONF_REPEAT = 'repeat'
CONF_INVERSE = 'inverse'

STATE_LOW = 'low'
STATE_HIGH = 'high'
Expand All @@ -48,6 +49,7 @@
vol.Exclusive(CONF_ZONE, 's_pin'): vol.Any(*ZONE_TO_PIN),
vol.Required(CONF_TYPE): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_INVERSE): cv.boolean,
}), cv.has_at_least_one_key(CONF_PIN, CONF_ZONE)
)

Expand Down Expand Up @@ -156,6 +158,7 @@ def save_data(self):
CONF_TYPE: entity[CONF_TYPE],
CONF_NAME: entity.get(CONF_NAME, 'Konnected {} Zone {}'.format(
self.device_id[6:], PIN_TO_ZONE[pin])),
CONF_INVERSE: entity.get(CONF_INVERSE),
ATTR_STATE: None
}
_LOGGER.debug('Set up sensor %s (initial state: %s)',
Expand Down Expand Up @@ -259,15 +262,19 @@ def actuator_configuration(self):

def update_initial_states(self):
"""Update the initial state of each sensor from status poll."""
for sensor in self.status.get('sensors'):
entity_id = self.stored_configuration[CONF_BINARY_SENSORS]. \
get(sensor.get(CONF_PIN), {}). \
get(ATTR_ENTITY_ID)
for sensor_data in self.status.get('sensors'):
sensor_config = self.stored_configuration[CONF_BINARY_SENSORS]. \
get(sensor_data.get(CONF_PIN), {})
entity_id = sensor_config.get(ATTR_ENTITY_ID)

state = bool(sensor_data.get(ATTR_STATE))
if sensor_config.get(CONF_INVERSE):
state = not state

async_dispatcher_send(
self.hass,
SIGNAL_SENSOR_UPDATE.format(entity_id),
bool(sensor.get(ATTR_STATE)))
state)

def sync_device_config(self):
"""Sync the new pin configuration to the Konnected device."""
Expand Down Expand Up @@ -341,7 +348,6 @@ async def put(self, request: Request, device_id,
return self.json_message(
"unauthorized", status_code=HTTP_UNAUTHORIZED)
pin_num = int(pin_num)
state = bool(int(state))
device = data[CONF_DEVICES].get(device_id)
if device is None:
return self.json_message('unregistered device',
Expand All @@ -356,6 +362,9 @@ async def put(self, request: Request, device_id,
if entity_id is None:
return self.json_message('uninitialized sensor/actuator',
status_code=HTTP_NOT_FOUND)
state = bool(int(state))
if pin_data.get(CONF_INVERSE):
state = not state

async_dispatcher_send(
hass, SIGNAL_SENSOR_UPDATE.format(entity_id), state)
Expand Down

0 comments on commit 4bb0fcc

Please sign in to comment.