-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Added option to invert aREST pin switch logic for active low relays #14467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| }) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
|
|
@@ -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))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the default here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(): | ||
|
|
@@ -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) | ||
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not happy about the naming.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
@@ -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) | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍