fix issue with initial state

This commit is contained in:
Bas Nijholt 2020-10-22 11:15:38 +02:00
commit 4de7e8d1e0
3 changed files with 47 additions and 58 deletions

View file

@ -21,9 +21,6 @@
"description": "All settings for a Adaptive Lighting component. The option names correspond with the YAML settings. No options are shown if you have this entry defined in YAML.",
"data": {
"lights": "lights",
"adapt_brightness": "adapt_brightness",
"adapt_color_temp": "adapt_color_temp, adapt color temperature using 'color_temp' if supported",
"adapt_rgb_color": "adapt_rgb_color, adapt color temperature using RGB/XY if supported",
"initial_transition": "initial_transition, when lights go 'off' to 'on' or when 'sleep_state' changes",
"interval": "interval, time between switch updates in seconds",
"max_brightness": "max_brightness, in %",

View file

@ -855,12 +855,11 @@ class SimpleSwitch(SwitchEntity, RestoreEntity):
"""Initialize the Adaptive Lighting switch."""
self.hass = hass
data = validate(config_entry)
self._name = data[CONF_NAME]
self._icon = ICON
self._state = None
self._which = which
self._unique_id = f"{self._name}_{slugify(self._which)}"
self._name = f"Adaptive Lighting {which}: {self._name}"
self._name = f"Adaptive Lighting {which}: {data[CONF_NAME]}"
self._initial_state = initial_state
@property
@ -886,14 +885,10 @@ class SimpleSwitch(SwitchEntity, RestoreEntity):
async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to hass."""
last_state = await self.async_get_last_state()
if last_state is None: # newly added to HA
if self._initial_state:
await self.async_turn_on()
else:
await self.async_turn_off()
elif STATE_ON:
_LOGGER.debug("%s: last state is %s", self._name, last_state)
if (last_state is None and self._initial_state) or last_state.state == STATE_ON:
await self.async_turn_on()
elif STATE_OFF:
else:
await self.async_turn_off()
async def async_turn_on(self, **kwargs) -> None:

View file

@ -1,51 +1,48 @@
{
"title": "Adaptive Lighting",
"config": {
"step": {
"user": {
"title": "Choose a name for the Adaptive Lighting",
"description": "Every instance can contain multiple lights!",
"data": {
"name": "Name"
"config": {
"abort": {
"already_configured": "Device is already configured"
},
"step": {
"user": {
"data": {
"name": "Name"
},
"description": "Every instance can contain multiple lights!",
"title": "Choose a name for the Adaptive Lighting"
}
}
}
},
"abort": {
"already_configured": "Device is already configured"
}
},
"options": {
"step": {
"init": {
"title": "Adaptive Lighting options",
"description": "All settings for a Adaptive Lighting component. The option names correspond with the YAML settings. No options are shown if you have this entry defined in YAML.",
"data": {
"lights": "lights",
"adapt_brightness": "adapt_brightness",
"adapt_color_temp": "adapt_color_temp, adapt color temperature using 'color_temp' if supported",
"adapt_rgb_color": "adapt_rgb_color, adapt color temperature using RGB/XY if supported",
"initial_transition": "initial_transition, when lights go 'off' to 'on' or when 'sleep_state' changes",
"interval": "interval, time between switch updates in seconds",
"max_brightness": "max_brightness, in %",
"max_color_temp": "max_color_temp, in Kelvin",
"min_brightness": "min_brightness, in %",
"min_color_temp": "min_color_temp, in Kelvin",
"only_once": "only_once, only adapt the lights when turning them on",
"prefer_rgb_color": "prefer_rgb_color, use 'rgb_color' over 'color_temp' when possible",
"sleep_brightness": "sleep_brightness, in %",
"sleep_color_temp": "sleep_color_temp, in Kelvin",
"sunrise_offset": "sunrise_offset, in +/- seconds",
"sunrise_time": "sunrise_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunrise time at your location)",
"sunset_offset": "sunset_offset, in +/- seconds",
"sunset_time": "sunset_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunset time at your location)",
"take_over_control": "take_over_control, if anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.",
"detect_non_ha_changes": "detect_non_ha_changes, detects all >5% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)",
"transition": "transition, in seconds"
"options": {
"error": {
"option_error": "Invalid option"
},
"step": {
"init": {
"data": {
"detect_non_ha_changes": "detect_non_ha_changes, detects all >5% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)",
"initial_transition": "initial_transition, when lights go 'off' to 'on' or when 'sleep_state' changes",
"interval": "interval, time between switch updates in seconds",
"lights": "lights",
"max_brightness": "max_brightness, in %",
"max_color_temp": "max_color_temp, in Kelvin",
"min_brightness": "min_brightness, in %",
"min_color_temp": "min_color_temp, in Kelvin",
"only_once": "only_once, only adapt the lights when turning them on",
"prefer_rgb_color": "prefer_rgb_color, use 'rgb_color' over 'color_temp' when possible",
"sleep_brightness": "sleep_brightness, in %",
"sleep_color_temp": "sleep_color_temp, in Kelvin",
"sunrise_offset": "sunrise_offset, in +/- seconds",
"sunrise_time": "sunrise_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunrise time at your location)",
"sunset_offset": "sunset_offset, in +/- seconds",
"sunset_time": "sunset_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunset time at your location)",
"take_over_control": "take_over_control, if anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.",
"transition": "transition, in seconds"
},
"description": "All settings for a Adaptive Lighting component. The option names correspond with the YAML settings. No options are shown if you have this entry defined in YAML.",
"title": "Adaptive Lighting options"
}
}
}
},
"error": {
"option_error": "Invalid option"
}
}
}
"title": "Adaptive Lighting"
}