Add support for restoring state to rpi_gpio_pwm (#19944)

This commit is contained in:
Sören Oldag 2019-01-11 18:25:14 +01:00 committed by Paulus Schoutsen
parent 49cfebd903
commit 17dce6697f
2 changed files with 26 additions and 5 deletions

View file

@ -8,14 +8,15 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_NAME, CONF_TYPE from homeassistant.const import CONF_NAME, CONF_TYPE, STATE_ON
from homeassistant.components.light import ( from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS, ATTR_HS_COLOR, ATTR_TRANSITION, Light, ATTR_BRIGHTNESS, ATTR_HS_COLOR, ATTR_TRANSITION,
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_TRANSITION, PLATFORM_SCHEMA) SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_TRANSITION, PLATFORM_SCHEMA)
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
from homeassistant.helpers.restore_state import RestoreEntity
REQUIREMENTS = ['pwmled==1.4.0'] REQUIREMENTS = ['pwmled==1.4.1']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -34,6 +35,7 @@ CONF_LED_TYPE_RGB = 'rgb'
CONF_LED_TYPE_RGBW = 'rgbw' CONF_LED_TYPE_RGBW = 'rgbw'
CONF_LED_TYPES = [CONF_LED_TYPE_SIMPLE, CONF_LED_TYPE_RGB, CONF_LED_TYPE_RGBW] CONF_LED_TYPES = [CONF_LED_TYPE_SIMPLE, CONF_LED_TYPE_RGB, CONF_LED_TYPE_RGBW]
DEFAULT_BRIGHTNESS = 255
DEFAULT_COLOR = [0, 0] DEFAULT_COLOR = [0, 0]
SUPPORT_SIMPLE_LED = (SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION) SUPPORT_SIMPLE_LED = (SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION)
@ -95,7 +97,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
add_entities(leds) add_entities(leds)
class PwmSimpleLed(Light): class PwmSimpleLed(Light, RestoreEntity):
"""Representation of a simple one-color PWM LED.""" """Representation of a simple one-color PWM LED."""
def __init__(self, led, name): def __init__(self, led, name):
@ -103,7 +105,18 @@ class PwmSimpleLed(Light):
self._led = led self._led = led
self._name = name self._name = name
self._is_on = False self._is_on = False
self._brightness = 255 self._brightness = DEFAULT_BRIGHTNESS
async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
self._is_on = (last_state.state == STATE_ON)
self._brightness = last_state.attributes.get('brightness',
DEFAULT_BRIGHTNESS)
self._led.set(is_on=self._is_on,
brightness=_from_hass_brightness(self._brightness))
@property @property
def should_poll(self): def should_poll(self):
@ -169,6 +182,14 @@ class PwmRgbLed(PwmSimpleLed):
super().__init__(led, name) super().__init__(led, name)
self._color = DEFAULT_COLOR self._color = DEFAULT_COLOR
async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
self._color = last_state.attributes.get('hs_color', DEFAULT_COLOR)
self._led.set(color=_from_hass_color(self._color))
@property @property
def hs_color(self): def hs_color(self):
"""Return the color property.""" """Return the color property."""

View file

@ -841,7 +841,7 @@ pushbullet.py==0.11.0
pushetta==1.0.15 pushetta==1.0.15
# homeassistant.components.light.rpi_gpio_pwm # homeassistant.components.light.rpi_gpio_pwm
pwmled==1.4.0 pwmled==1.4.1
# homeassistant.components.august # homeassistant.components.august
py-august==0.7.0 py-august==0.7.0