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