Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions homeassistant/components/switch/arest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

CONF_FUNCTIONS = 'functions'
CONF_PINS = 'pins'
CONF_INVERT = 'invert'

DEFAULT_NAME = 'aREST switch'

PIN_FUNCTION_SCHEMA = vol.Schema({
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_INVERT, default=False): cv.boolean,
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
Expand Down Expand Up @@ -54,7 +56,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for pinnum, pin in pins.items():
dev.append(ArestSwitchPin(
resource, config.get(CONF_NAME, response.json()[CONF_NAME]),
pin.get(CONF_NAME), pinnum))
pin.get(CONF_NAME), pinnum, pin.get(CONF_INVERT, False)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the default here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Done.


functions = config.get(CONF_FUNCTIONS)
for funcname, func in functions.items():
Expand Down Expand Up @@ -152,10 +154,11 @@ def update(self):
class ArestSwitchPin(ArestSwitchBase):
"""Representation of an aREST switch. Based on digital I/O."""

def __init__(self, resource, location, name, pin):
def __init__(self, resource, location, name, pin, invert):
"""Initialize the switch."""
super().__init__(resource, location, name)
self._pin = pin
self.invert = invert

request = requests.get(
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
Expand All @@ -165,8 +168,11 @@ def __init__(self, resource, location, name, pin):

def turn_on(self, **kwargs):
"""Turn the device on."""
turn_on_payload = int(not self.invert)
request = requests.get(
'{}/digital/{}/1'.format(self._resource, self._pin), timeout=10)
'{}/digital/{}/{}'.format(self._resource, self._pin,
turn_on_payload),
timeout=10)
if request.status_code == 200:
self._state = True
else:
Expand All @@ -175,8 +181,11 @@ def turn_on(self, **kwargs):

def turn_off(self, **kwargs):
"""Turn the device off."""
turn_off_payload = int(self.invert)
request = requests.get(
'{}/digital/{}/0'.format(self._resource, self._pin), timeout=10)
'{}/digital/{}/{}'.format(self._resource, self._pin,
turn_off_payload),
timeout=10)
if request.status_code == 200:
self._state = False
else:
Expand All @@ -188,7 +197,8 @@ def update(self):
try:
request = requests.get(
'{}/digital/{}'.format(self._resource, self._pin), timeout=10)
self._state = request.json()['return_value'] != 0
status_value = int(self.invert)
self._state = request.json()['return_value'] != status_value
self._available = True
except requests.exceptions.ConnectionError:
_LOGGER.warning("No route to device %s", self._resource)
Expand Down