From 6e8472b90fe4050c0ad73a39c21c3a05fbba1885 Mon Sep 17 00:00:00 2001 From: disaster37 Date: Thu, 25 May 2023 10:45:15 +0200 Subject: [PATCH] Fix/arest switch (#91553) * Fix arest switch to set output when connection is lost Signed-off-by: disaster37 * fix: lint Signed-off-by: disaster37 --------- Signed-off-by: disaster37 --- homeassistant/components/arest/switch.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/arest/switch.py b/homeassistant/components/arest/switch.py index 6efa24c5a0f..1c67723fc02 100644 --- a/homeassistant/components/arest/switch.py +++ b/homeassistant/components/arest/switch.py @@ -161,16 +161,13 @@ class ArestSwitchFunction(ArestSwitchBase): class ArestSwitchPin(ArestSwitchBase): """Representation of an aREST switch. Based on digital I/O.""" - def __init__(self, resource, location, name, pin, invert): + def __init__(self, resource, location, name, pin, invert) -> None: """Initialize the switch.""" super().__init__(resource, location, name) self._pin = pin self.invert = invert - request = requests.get(f"{resource}/mode/{pin}/o", timeout=10) - if request.status_code != HTTPStatus.OK: - _LOGGER.error("Can't set mode") - self._attr_available = False + self.__set_pin_output() def turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" @@ -200,7 +197,15 @@ class ArestSwitchPin(ArestSwitchBase): request = requests.get(f"{self._resource}/digital/{self._pin}", timeout=10) status_value = int(self.invert) self._attr_is_on = request.json()["return_value"] != status_value - self._attr_available = True + if self._attr_available is False: + self._attr_available = True + self.__set_pin_output() except requests.exceptions.ConnectionError: _LOGGER.warning("No route to device %s", self._resource) self._attr_available = False + + def __set_pin_output(self) -> None: + request = requests.get(f"{self._resource}/mode/{self._pin}/o", timeout=10) + if request.status_code != HTTPStatus.OK: + _LOGGER.error("Can't set mode") + self._attr_available = False