mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-19 18:24:04 +02:00
Compare commits
8 commits
main
...
getattribu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78fa4258e6 |
||
|
|
abc4ea8203 |
||
|
|
7106a77543 | ||
|
|
6cfe8bff93 | ||
|
|
04265b1db6 |
||
|
|
4defd73156 | ||
|
|
cd3f7608de | ||
|
|
e07e18d077 |
1 changed files with 26 additions and 25 deletions
|
|
@ -315,9 +315,10 @@ def _get_switches_with_lights(
|
||||||
continue
|
continue
|
||||||
switch = data[config.entry_id]["instance"]
|
switch = data[config.entry_id]["instance"]
|
||||||
all_check_lights = _expand_light_groups(hass, lights)
|
all_check_lights = _expand_light_groups(hass, lights)
|
||||||
switch._expand_light_groups()
|
|
||||||
# Check if any of the lights are in the switch's lights
|
# Check if any of the lights are in the switch's lights
|
||||||
if set(switch._lights) & set(all_check_lights):
|
if set(switch.all_lights) & set(
|
||||||
|
all_check_lights
|
||||||
|
): # pylint: disable=protected-access
|
||||||
switches.append(switch)
|
switches.append(switch)
|
||||||
return switches
|
return switches
|
||||||
|
|
||||||
|
|
@ -418,7 +419,7 @@ async def handle_change_switch_settings(
|
||||||
data,
|
data,
|
||||||
)
|
)
|
||||||
|
|
||||||
all_lights = switch._lights # pylint: disable=protected-access
|
all_lights = switch.all_lights # pylint: disable=protected-access
|
||||||
switch.turn_on_off_listener.reset(*all_lights, reset_manual_control=False)
|
switch.turn_on_off_listener.reset(*all_lights, reset_manual_control=False)
|
||||||
if switch.is_on:
|
if switch.is_on:
|
||||||
await switch._update_attrs_and_maybe_adapt_lights( # pylint: disable=protected-access
|
await switch._update_attrs_and_maybe_adapt_lights( # pylint: disable=protected-access
|
||||||
|
|
@ -501,10 +502,7 @@ async def async_setup_entry(
|
||||||
switches = _get_switches_from_service_call(hass, service_call)
|
switches = _get_switches_from_service_call(hass, service_call)
|
||||||
lights = data[CONF_LIGHTS]
|
lights = data[CONF_LIGHTS]
|
||||||
for switch in switches:
|
for switch in switches:
|
||||||
if not lights:
|
all_lights = lights or switch.all_lights # pylint: disable=protected-access
|
||||||
all_lights = switch._lights # pylint: disable=protected-access
|
|
||||||
else:
|
|
||||||
all_lights = _expand_light_groups(switch.hass, lights)
|
|
||||||
switch.turn_on_off_listener.lights.update(all_lights)
|
switch.turn_on_off_listener.lights.update(all_lights)
|
||||||
for light in all_lights:
|
for light in all_lights:
|
||||||
if data[CONF_TURN_ON_LIGHTS] or is_on(hass, light):
|
if data[CONF_TURN_ON_LIGHTS] or is_on(hass, light):
|
||||||
|
|
@ -531,10 +529,7 @@ async def async_setup_entry(
|
||||||
switches = _get_switches_from_service_call(hass, service_call)
|
switches = _get_switches_from_service_call(hass, service_call)
|
||||||
lights = data[CONF_LIGHTS]
|
lights = data[CONF_LIGHTS]
|
||||||
for switch in switches:
|
for switch in switches:
|
||||||
if not lights:
|
all_lights = lights or switch.all_lights # pylint: disable=protected-access
|
||||||
all_lights = switch._lights # pylint: disable=protected-access
|
|
||||||
else:
|
|
||||||
all_lights = _expand_light_groups(switch.hass, lights)
|
|
||||||
if service_call.data[CONF_MANUAL_CONTROL]:
|
if service_call.data[CONF_MANUAL_CONTROL]:
|
||||||
for light in all_lights:
|
for light in all_lights:
|
||||||
_fire_manual_control_event(switch, light, service_call.context)
|
_fire_manual_control_event(switch, light, service_call.context)
|
||||||
|
|
@ -630,8 +625,8 @@ def _expand_light_groups(hass: HomeAssistant, lights: list[str]) -> list[str]:
|
||||||
if state is None:
|
if state is None:
|
||||||
_LOGGER.debug("State of %s is None", light)
|
_LOGGER.debug("State of %s is None", light)
|
||||||
all_lights.add(light)
|
all_lights.add(light)
|
||||||
elif "entity_id" in state.attributes: # it's a light group
|
elif ATTR_ENTITY_ID in state.attributes: # it's a light group
|
||||||
group = state.attributes["entity_id"]
|
group = state.attributes[ATTR_ENTITY_ID]
|
||||||
turn_on_off_listener.lights.discard(light)
|
turn_on_off_listener.lights.discard(light)
|
||||||
all_lights.update(group)
|
all_lights.update(group)
|
||||||
_LOGGER.debug("Expanded %s to %s", light, group)
|
_LOGGER.debug("Expanded %s to %s", light, group)
|
||||||
|
|
@ -921,7 +916,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
)
|
)
|
||||||
self._take_over_control = True
|
self._take_over_control = True
|
||||||
self._auto_reset_manual_control_time = data[CONF_AUTORESET_CONTROL]
|
self._auto_reset_manual_control_time = data[CONF_AUTORESET_CONTROL]
|
||||||
self._expand_light_groups() # updates manual control timers
|
self._update_lights() # updates manual control timers
|
||||||
_loc = get_astral_location(self.hass)
|
_loc = get_astral_location(self.hass)
|
||||||
if isinstance(_loc, tuple):
|
if isinstance(_loc, tuple):
|
||||||
# Astral v2.2
|
# Astral v2.2
|
||||||
|
|
@ -973,6 +968,11 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
"""Return true if adaptive lighting is on."""
|
"""Return true if adaptive lighting is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def all_lights(self):
|
||||||
|
self._update_lights()
|
||||||
|
return self._lights
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Call when entity about to be added to hass."""
|
"""Call when entity about to be added to hass."""
|
||||||
if self.hass.is_running:
|
if self.hass.is_running:
|
||||||
|
|
@ -993,7 +993,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
"""Remove the listeners upon removing the component."""
|
"""Remove the listeners upon removing the component."""
|
||||||
self._remove_listeners()
|
self._remove_listeners()
|
||||||
|
|
||||||
def _expand_light_groups(self) -> None:
|
def _update_lights(self) -> None:
|
||||||
all_lights = _expand_light_groups(self.hass, self._lights)
|
all_lights = _expand_light_groups(self.hass, self._lights)
|
||||||
self.turn_on_off_listener.lights.update(all_lights)
|
self.turn_on_off_listener.lights.update(all_lights)
|
||||||
self.turn_on_off_listener.set_auto_reset_manual_control_times(
|
self.turn_on_off_listener.set_auto_reset_manual_control_times(
|
||||||
|
|
@ -1020,10 +1020,10 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
|
|
||||||
self.remove_listeners.extend([remove_interval, remove_sleep])
|
self.remove_listeners.extend([remove_interval, remove_sleep])
|
||||||
|
|
||||||
if self._lights:
|
lights = self.all_lights
|
||||||
self._expand_light_groups()
|
if lights:
|
||||||
remove_state = async_track_state_change_event(
|
remove_state = async_track_state_change_event(
|
||||||
self.hass, self._lights, self._light_event
|
self.hass, lights, self._light_event
|
||||||
)
|
)
|
||||||
self.remove_listeners.append(remove_state)
|
self.remove_listeners.append(remove_state)
|
||||||
|
|
||||||
|
|
@ -1045,16 +1045,17 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
for key in self._settings:
|
for key in self._settings:
|
||||||
extra_state_attributes[key] = None
|
extra_state_attributes[key] = None
|
||||||
return extra_state_attributes
|
return extra_state_attributes
|
||||||
|
lights = self.all_lights
|
||||||
extra_state_attributes["manual_control"] = [
|
extra_state_attributes["manual_control"] = [
|
||||||
light
|
light
|
||||||
for light in self._lights
|
for light in lights
|
||||||
if self.turn_on_off_listener.manual_control.get(light)
|
if self.turn_on_off_listener.manual_control.get(light)
|
||||||
]
|
]
|
||||||
extra_state_attributes.update(self._settings)
|
extra_state_attributes.update(self._settings)
|
||||||
timers = self.turn_on_off_listener.auto_reset_manual_control_timers
|
timers = self.turn_on_off_listener.auto_reset_manual_control_timers
|
||||||
extra_state_attributes["autoreset_time_remaining"] = {
|
extra_state_attributes["autoreset_time_remaining"] = {
|
||||||
light: time
|
light: time
|
||||||
for light in self._lights
|
for light in lights
|
||||||
if (timer := timers.get(light)) and (time := timer.remaining_time()) > 0
|
if (timer := timers.get(light)) and (time := timer.remaining_time()) > 0
|
||||||
}
|
}
|
||||||
return extra_state_attributes
|
return extra_state_attributes
|
||||||
|
|
@ -1087,7 +1088,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
if self.is_on:
|
if self.is_on:
|
||||||
return
|
return
|
||||||
self._state = True
|
self._state = True
|
||||||
self.turn_on_off_listener.reset(*self._lights)
|
self.turn_on_off_listener.reset(*self.all_lights)
|
||||||
await self._setup_listeners()
|
await self._setup_listeners()
|
||||||
if adapt_lights:
|
if adapt_lights:
|
||||||
await self._update_attrs_and_maybe_adapt_lights(
|
await self._update_attrs_and_maybe_adapt_lights(
|
||||||
|
|
@ -1102,7 +1103,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
return
|
return
|
||||||
self._state = False
|
self._state = False
|
||||||
self._remove_listeners()
|
self._remove_listeners()
|
||||||
self.turn_on_off_listener.reset(*self._lights)
|
self.turn_on_off_listener.reset(*self.all_lights)
|
||||||
|
|
||||||
async def _async_update_at_interval(self, now=None) -> None:
|
async def _async_update_at_interval(self, now=None) -> None:
|
||||||
await self._update_attrs_and_maybe_adapt_lights(
|
await self._update_attrs_and_maybe_adapt_lights(
|
||||||
|
|
@ -1237,7 +1238,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
if lights is None:
|
if lights is None:
|
||||||
lights = self._lights
|
lights = self.all_lights
|
||||||
|
|
||||||
filtered_lights = []
|
filtered_lights = []
|
||||||
if not force:
|
if not force:
|
||||||
|
|
@ -1329,7 +1330,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
"%s: _sleep_mode_switch_state_event, event: '%s'", self._name, event
|
"%s: _sleep_mode_switch_state_event, event: '%s'", self._name, event
|
||||||
)
|
)
|
||||||
# Reset the manually controlled status when the "sleep mode" changes
|
# Reset the manually controlled status when the "sleep mode" changes
|
||||||
self.turn_on_off_listener.reset(*self._lights)
|
self.turn_on_off_listener.reset(*self.all_lights)
|
||||||
await self._update_attrs_and_maybe_adapt_lights(
|
await self._update_attrs_and_maybe_adapt_lights(
|
||||||
transition=self._sleep_transition,
|
transition=self._sleep_transition,
|
||||||
force=True,
|
force=True,
|
||||||
|
|
@ -1713,7 +1714,7 @@ class TurnOnOffListener:
|
||||||
timer.cancel()
|
timer.cancel()
|
||||||
timers_dict.pop(light)
|
timers_dict.pop(light)
|
||||||
else: # Timer object already exists, just update the delay and restart it
|
else: # Timer object already exists, just update the delay and restart it
|
||||||
timer.delay = delay
|
timer.delay = delay # pylint: disable=protected-access
|
||||||
timer.start()
|
timer.start()
|
||||||
elif delay is not None: # Timer object does not exist, create it
|
elif delay is not None: # Timer object does not exist, create it
|
||||||
timer = _AsyncSingleShotTimer(delay, reset_coroutine)
|
timer = _AsyncSingleShotTimer(delay, reset_coroutine)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue