"""Support for Switchbot."""
import logging

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_MAC
from homeassistant.helpers.restore_state import RestoreEntity

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'Switchbot'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_MAC): cv.string,
        vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
    }
)


def setup_platform(hass, config, add_entities, discovery_info=None):
    """Perform the setup for Switchbot devices."""
    name = config.get(CONF_NAME)
    mac_addr = config[CONF_MAC]
    add_entities([SwitchBot(mac_addr, name)])


class SwitchBot(SwitchDevice, RestoreEntity):
    """Representation of a Switchbot."""

    def __init__(self, mac, name) -> None:
        """Initialize the Switchbot."""
        # pylint: disable=import-error, no-member
        import switchbot

        self._state = None
        self._name = name
        self._mac = mac
        self._device = switchbot.Switchbot(mac=mac)

    async def async_added_to_hass(self):
        """Run when entity about to be added."""
        await super().async_added_to_hass()
        state = await self.async_get_last_state()
        if not state:
            return
        self._state = state.state == 'on'

    def turn_on(self, **kwargs) -> None:
        """Turn device on."""
        if self._device.turn_on():
            self._state = True

    def turn_off(self, **kwargs) -> None:
        """Turn device off."""
        if self._device.turn_off():
            self._state = False

    @property
    def assumed_state(self) -> bool:
        """Return true if unable to access real state of entity."""
        return True

    @property
    def is_on(self) -> bool:
        """Return true if device is on."""
        return self._state

    @property
    def unique_id(self) -> str:
        """Return a unique, HASS-friendly identifier for this entity."""
        return self._mac.replace(':', '')

    @property
    def name(self) -> str:
        """Return the name of the switch."""
        return self._name

@@ -36,16 +36,45 @@
  logging
+%0Afrom typing import Any, Dict
 %0A%0Aimport
@@ -1076,16 +1076,54 @@
  = None%0A
+        self._last_run_success = None%0A
         
@@ -1617,24 +1617,123 @@
 state = True
+%0A            self._last_run_success = True%0A        else:%0A            self._last_run_success = False
 %0A%0A    def tu
@@ -1858,24 +1858,123 @@
 tate = False
+%0A            self._last_run_success = True%0A        else:%0A            self._last_run_success = False
 %0A%0A    @prope
@@ -2461,24 +2461,24 @@
  switch.%22%22%22%0A
-
         retu
@@ -2487,12 +2487,187 @@
  self._name%0A
+%0A    @property%0A    def device_state_attributes(self) -%3E Dict%5Bstr, Any%5D:%0A        %22%22%22Return the state attributes.%22%22%22%0A        return %7B'last_run_success': self._last_run_success%7D%0A

Add last_run_success boolean attribute to Switchbot for error trapping (#25474)