Skip to content
Merged
Changes from 1 commit
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
16 changes: 11 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): cv.boolean,

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 apply the default (False) 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.

👍

})

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,9 @@ def __init__(self, resource, location, name, pin):

def turn_on(self, **kwargs):
"""Turn the device on."""
one_or_zero = 0 if self.invert else 1
request = requests.get(
'{}/digital/{}/1'.format(self._resource, self._pin), timeout=10)
'{}/digital/{}/{}'.format(self._resource, self._pin, one_or_zero), timeout=10)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (90 > 79 characters)

if request.status_code == 200:
self._state = True
else:
Expand All @@ -175,8 +179,9 @@ def turn_on(self, **kwargs):

def turn_off(self, **kwargs):
"""Turn the device off."""
one_or_zero = 1 if self.invert else 0

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.

I'm not happy about the naming. turn_off_payload = int(not invert)?

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.

Like this?

request = requests.get(
'{}/digital/{}/0'.format(self._resource, self._pin), timeout=10)
'{}/digital/{}/{}'.format(self._resource, self._pin, one_or_zero), timeout=10)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (90 > 79 characters)

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