mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Added option to invert aREST pin switch logic for active low relays (#14467)
* Added option to invert aREST pin switch logic for active low relays * Fixed line lengths * Changed naming and set optional invert default value. * Fixed line length * Removed default from get
This commit is contained in:
parent
5ff5c73e2b
commit
1533a68c06
@ -18,11 +18,13 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
CONF_FUNCTIONS = 'functions'
|
CONF_FUNCTIONS = 'functions'
|
||||||
CONF_PINS = 'pins'
|
CONF_PINS = 'pins'
|
||||||
|
CONF_INVERT = 'invert'
|
||||||
|
|
||||||
DEFAULT_NAME = 'aREST switch'
|
DEFAULT_NAME = 'aREST switch'
|
||||||
|
|
||||||
PIN_FUNCTION_SCHEMA = vol.Schema({
|
PIN_FUNCTION_SCHEMA = vol.Schema({
|
||||||
vol.Optional(CONF_NAME): cv.string,
|
vol.Optional(CONF_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_INVERT, default=False): cv.boolean,
|
||||||
})
|
})
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
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():
|
for pinnum, pin in pins.items():
|
||||||
dev.append(ArestSwitchPin(
|
dev.append(ArestSwitchPin(
|
||||||
resource, config.get(CONF_NAME, response.json()[CONF_NAME]),
|
resource, config.get(CONF_NAME, response.json()[CONF_NAME]),
|
||||||
pin.get(CONF_NAME), pinnum))
|
pin.get(CONF_NAME), pinnum, pin.get(CONF_INVERT)))
|
||||||
|
|
||||||
functions = config.get(CONF_FUNCTIONS)
|
functions = config.get(CONF_FUNCTIONS)
|
||||||
for funcname, func in functions.items():
|
for funcname, func in functions.items():
|
||||||
@ -152,10 +154,11 @@ class ArestSwitchFunction(ArestSwitchBase):
|
|||||||
class ArestSwitchPin(ArestSwitchBase):
|
class ArestSwitchPin(ArestSwitchBase):
|
||||||
"""Representation of an aREST switch. Based on digital I/O."""
|
"""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."""
|
"""Initialize the switch."""
|
||||||
super().__init__(resource, location, name)
|
super().__init__(resource, location, name)
|
||||||
self._pin = pin
|
self._pin = pin
|
||||||
|
self.invert = invert
|
||||||
|
|
||||||
request = requests.get(
|
request = requests.get(
|
||||||
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
|
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
|
||||||
@ -165,8 +168,11 @@ class ArestSwitchPin(ArestSwitchBase):
|
|||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
|
turn_on_payload = int(not self.invert)
|
||||||
request = requests.get(
|
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:
|
if request.status_code == 200:
|
||||||
self._state = True
|
self._state = True
|
||||||
else:
|
else:
|
||||||
@ -175,8 +181,11 @@ class ArestSwitchPin(ArestSwitchBase):
|
|||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
|
turn_off_payload = int(self.invert)
|
||||||
request = requests.get(
|
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:
|
if request.status_code == 200:
|
||||||
self._state = False
|
self._state = False
|
||||||
else:
|
else:
|
||||||
@ -188,7 +197,8 @@ class ArestSwitchPin(ArestSwitchBase):
|
|||||||
try:
|
try:
|
||||||
request = requests.get(
|
request = requests.get(
|
||||||
'{}/digital/{}'.format(self._resource, self._pin), timeout=10)
|
'{}/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
|
self._available = True
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.warning("No route to device %s", self._resource)
|
_LOGGER.warning("No route to device %s", self._resource)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user