Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make broadlink switch restore its state #23829

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions homeassistant/components/broadlink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
ENTITY_ID_FORMAT, PLATFORM_SCHEMA, SwitchDevice)
from homeassistant.const import (
CONF_COMMAND_OFF, CONF_COMMAND_ON, CONF_FRIENDLY_NAME, CONF_HOST, CONF_MAC,
CONF_SWITCHES, CONF_TIMEOUT, CONF_TYPE)
CONF_SWITCHES, CONF_TIMEOUT, CONF_TYPE, STATE_ON)
import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle, slugify
from homeassistant.helpers.restore_state import RestoreEntity

from . import async_setup_service, data_packet

Expand Down Expand Up @@ -115,7 +116,7 @@ def _get_mp1_slot_name(switch_friendly_name, slot):
add_entities(switches)


class BroadlinkRMSwitch(SwitchDevice):
class BroadlinkRMSwitch(SwitchDevice, RestoreEntity):
"""Representation of an Broadlink switch."""

def __init__(self, name, friendly_name, device, command_on, command_off):
Expand All @@ -127,6 +128,13 @@ def __init__(self, name, friendly_name, device, command_on, command_off):
self._command_off = command_off
self._device = device

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
self._state = state.state == STATE_ON

@property
def name(self):
"""Return the name of the switch."""
Expand Down