From 0be0353eed3c9ddb94471b3359a70a43502a3488 Mon Sep 17 00:00:00 2001 From: manonstreet Date: Sat, 27 Jul 2019 10:13:48 -0400 Subject: [PATCH] Add last_run_success boolean attribute to Switchbot for error trapping (#25474) * Add last_run_error boolean attribute to Switchbot entity to allow for trapping of errors * Add last_run_success boolean attribute to Switchbot for error trapping * Add last_run_success boolean attribute to Switchbot for error trapping --- homeassistant/components/switchbot/switch.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/homeassistant/components/switchbot/switch.py b/homeassistant/components/switchbot/switch.py index c29dfea6737..6b348b69417 100644 --- a/homeassistant/components/switchbot/switch.py +++ b/homeassistant/components/switchbot/switch.py @@ -1,5 +1,6 @@ """Support for Switchbot.""" import logging +from typing import Any, Dict import voluptuous as vol @@ -36,6 +37,7 @@ class SwitchBot(SwitchDevice, RestoreEntity): import switchbot self._state = None + self._last_run_success = None self._name = name self._mac = mac self._device = switchbot.Switchbot(mac=mac) @@ -52,11 +54,17 @@ class SwitchBot(SwitchDevice, RestoreEntity): """Turn device on.""" if self._device.turn_on(): self._state = True + self._last_run_success = True + else: + self._last_run_success = False def turn_off(self, **kwargs) -> None: """Turn device off.""" if self._device.turn_off(): self._state = False + self._last_run_success = True + else: + self._last_run_success = False @property def assumed_state(self) -> bool: @@ -77,3 +85,8 @@ class SwitchBot(SwitchDevice, RestoreEntity): def name(self) -> str: """Return the name of the switch.""" return self._name + + @property + def device_state_attributes(self) -> Dict[str, Any]: + """Return the state attributes.""" + return {'last_run_success': self._last_run_success}