mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-12 06:44:04 +02:00
use pyupgrade
This commit is contained in:
parent
13ff09dc2f
commit
44a7e41e80
2 changed files with 49 additions and 49 deletions
|
|
@ -36,7 +36,7 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: Dict[str, Any]):
|
||||
async def async_setup(hass: HomeAssistant, config: dict[str, Any]):
|
||||
"""Import integration from config."""
|
||||
|
||||
if DOMAIN in config:
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ def _short_hash(string: str, length: int = 4) -> str:
|
|||
|
||||
|
||||
def create_context(
|
||||
name: str, which: str, index: int, parent: Optional[Context] = None
|
||||
name: str, which: str, index: int, parent: Context | None = None
|
||||
) -> Context:
|
||||
"""Create a context that can identify this integration."""
|
||||
# Use a hash for the name because otherwise the context might become
|
||||
|
|
@ -191,7 +191,7 @@ def create_context(
|
|||
)
|
||||
|
||||
|
||||
def is_our_context(context: Optional[Context]) -> bool:
|
||||
def is_our_context(context: Context | None) -> bool:
|
||||
"""Check whether this integration created 'context'."""
|
||||
if context is None:
|
||||
return False
|
||||
|
|
@ -367,7 +367,7 @@ def validate(config_entry: ConfigEntry):
|
|||
return data
|
||||
|
||||
|
||||
def match_switch_state_event(event: Event, from_or_to_state: List[str]):
|
||||
def match_switch_state_event(event: Event, from_or_to_state: list[str]):
|
||||
"""Match state event when either 'from_state' or 'to_state' matches."""
|
||||
old_state = event.data.get("old_state")
|
||||
from_state_match = old_state is not None and old_state.state in from_or_to_state
|
||||
|
|
@ -379,7 +379,7 @@ def match_switch_state_event(event: Event, from_or_to_state: List[str]):
|
|||
return match
|
||||
|
||||
|
||||
def _expand_light_groups(hass: HomeAssistant, lights: List[str]) -> List[str]:
|
||||
def _expand_light_groups(hass: HomeAssistant, lights: list[str]) -> list[str]:
|
||||
all_lights = set()
|
||||
turn_on_off_listener = hass.data[DOMAIN][ATTR_TURN_ON_OFF_LISTENER]
|
||||
for light in lights:
|
||||
|
|
@ -427,7 +427,7 @@ def _supported_features(hass: HomeAssistant, light: str):
|
|||
|
||||
|
||||
def color_difference_redmean(
|
||||
rgb1: Tuple[float, float, float], rgb2: Tuple[float, float, float]
|
||||
rgb1: tuple[float, float, float], rgb2: tuple[float, float, float]
|
||||
) -> float:
|
||||
"""Distance between colors in RGB space (redmean metric).
|
||||
|
||||
|
|
@ -438,7 +438,7 @@ def color_difference_redmean(
|
|||
- https://www.compuphase.com/cmetric.htm
|
||||
"""
|
||||
r_hat = (rgb1[0] + rgb2[0]) / 2
|
||||
delta_r, delta_g, delta_b = [(col1 - col2) for col1, col2 in zip(rgb1, rgb2)]
|
||||
delta_r, delta_g, delta_b = ((col1 - col2) for col1, col2 in zip(rgb1, rgb2))
|
||||
red_term = (2 + r_hat / 256) * delta_r**2
|
||||
green_term = 4 * delta_g**2
|
||||
blue_term = (2 + (255 - r_hat) / 256) * delta_b**2
|
||||
|
|
@ -447,8 +447,8 @@ def color_difference_redmean(
|
|||
|
||||
def _attributes_have_changed(
|
||||
light: str,
|
||||
old_attributes: Dict[str, Any],
|
||||
new_attributes: Dict[str, Any],
|
||||
old_attributes: dict[str, Any],
|
||||
new_attributes: dict[str, Any],
|
||||
adapt_brightness: bool,
|
||||
adapt_color: bool,
|
||||
context: Context,
|
||||
|
|
@ -604,16 +604,16 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
self._state = None
|
||||
|
||||
# Tracks 'off' → 'on' state changes
|
||||
self._on_to_off_event: Dict[str, Event] = {}
|
||||
self._on_to_off_event: dict[str, Event] = {}
|
||||
# Tracks 'on' → 'off' state changes
|
||||
self._off_to_on_event: Dict[str, Event] = {}
|
||||
self._off_to_on_event: dict[str, Event] = {}
|
||||
# Locks that prevent light adjusting when waiting for a light to 'turn_off'
|
||||
self._locks: Dict[str, asyncio.Lock] = {}
|
||||
self._locks: dict[str, asyncio.Lock] = {}
|
||||
# To count the number of `Context` instances
|
||||
self._context_cnt: int = 0
|
||||
|
||||
# Set in self._update_attrs_and_maybe_adapt_lights
|
||||
self._settings: Dict[str, Any] = {}
|
||||
self._settings: dict[str, Any] = {}
|
||||
|
||||
# Set and unset tracker in async_turn_on and async_turn_off
|
||||
self.remove_listeners = []
|
||||
|
|
@ -639,7 +639,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self) -> Optional[bool]:
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if adaptive lighting is on."""
|
||||
return self._state
|
||||
|
||||
|
|
@ -705,7 +705,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
return self._icon
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Dict[str, Any]:
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return the attributes of the switch."""
|
||||
if not self.is_on:
|
||||
return {key: None for key in self._settings}
|
||||
|
|
@ -717,7 +717,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
return dict(self._settings, manual_control=manual_control)
|
||||
|
||||
def create_context(
|
||||
self, which: str = "default", parent: Optional[Context] = None
|
||||
self, which: str = "default", parent: Context | None = None
|
||||
) -> Context:
|
||||
"""Create a context that identifies this Adaptive Lighting instance."""
|
||||
# Right now the highest number of each context_id it can create is
|
||||
|
|
@ -769,12 +769,12 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
async def _adapt_light(
|
||||
self,
|
||||
light: str,
|
||||
transition: Optional[int] = None,
|
||||
adapt_brightness: Optional[bool] = None,
|
||||
adapt_color: Optional[bool] = None,
|
||||
prefer_rgb_color: Optional[bool] = None,
|
||||
transition: int | None = None,
|
||||
adapt_brightness: bool | None = None,
|
||||
adapt_color: bool | None = None,
|
||||
prefer_rgb_color: bool | None = None,
|
||||
force: bool = False,
|
||||
context: Optional[Context] = None,
|
||||
context: Context | None = None,
|
||||
) -> None:
|
||||
lock = self._locks.get(light)
|
||||
if lock is not None and lock.locked():
|
||||
|
|
@ -863,10 +863,10 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
|
||||
async def _update_attrs_and_maybe_adapt_lights(
|
||||
self,
|
||||
lights: Optional[List[str]] = None,
|
||||
transition: Optional[int] = None,
|
||||
lights: list[str] | None = None,
|
||||
transition: int | None = None,
|
||||
force: bool = False,
|
||||
context: Optional[Context] = None,
|
||||
context: Context | None = None,
|
||||
) -> None:
|
||||
assert context is not None
|
||||
_LOGGER.debug(
|
||||
|
|
@ -887,10 +887,10 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
|
||||
async def _adapt_lights(
|
||||
self,
|
||||
lights: List[str],
|
||||
transition: Optional[int],
|
||||
lights: list[str],
|
||||
transition: int | None,
|
||||
force: bool,
|
||||
context: Optional[Context],
|
||||
context: Context | None,
|
||||
) -> None:
|
||||
assert context is not None
|
||||
_LOGGER.debug(
|
||||
|
|
@ -1021,7 +1021,7 @@ class SimpleSwitch(SwitchEntity, RestoreEntity):
|
|||
return self._icon
|
||||
|
||||
@property
|
||||
def is_on(self) -> Optional[bool]:
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if adaptive lighting is on."""
|
||||
return self._state
|
||||
|
||||
|
|
@ -1057,14 +1057,14 @@ class SunLightSettings:
|
|||
min_color_temp: int
|
||||
sleep_brightness: int
|
||||
sleep_color_temp: int
|
||||
sunrise_offset: Optional[datetime.timedelta]
|
||||
sunrise_time: Optional[datetime.time]
|
||||
sunset_offset: Optional[datetime.timedelta]
|
||||
sunset_time: Optional[datetime.time]
|
||||
sunrise_offset: datetime.timedelta | None
|
||||
sunrise_time: datetime.time | None
|
||||
sunset_offset: datetime.timedelta | None
|
||||
sunset_time: datetime.time | None
|
||||
time_zone: datetime.tzinfo
|
||||
transition: int
|
||||
|
||||
def get_sun_events(self, date: datetime.datetime) -> Dict[str, float]:
|
||||
def get_sun_events(self, date: datetime.datetime) -> dict[str, float]:
|
||||
"""Get the four sun event's timestamps at 'date'."""
|
||||
|
||||
def _replace_time(date: datetime.datetime, key: str) -> datetime.datetime:
|
||||
|
|
@ -1080,7 +1080,7 @@ class SunLightSettings:
|
|||
|
||||
def calculate_noon_and_midnight(
|
||||
sunset: datetime.datetime, sunrise: datetime.datetime
|
||||
) -> Tuple[datetime.datetime, datetime.datetime]:
|
||||
) -> tuple[datetime.datetime, datetime.datetime]:
|
||||
middle = abs(sunset - sunrise) / 2
|
||||
if sunset > sunrise:
|
||||
noon = sunrise + middle
|
||||
|
|
@ -1138,7 +1138,7 @@ class SunLightSettings:
|
|||
|
||||
return events
|
||||
|
||||
def relevant_events(self, now: datetime.datetime) -> List[Tuple[str, float]]:
|
||||
def relevant_events(self, now: datetime.datetime) -> list[tuple[str, float]]:
|
||||
"""Get the previous and next sun event."""
|
||||
events = [
|
||||
self.get_sun_events(now + timedelta(days=days)) for days in [-1, 0, 1]
|
||||
|
|
@ -1186,7 +1186,7 @@ class SunLightSettings:
|
|||
|
||||
def get_settings(
|
||||
self, is_sleep, transition
|
||||
) -> Dict[str, Union[float, Tuple[float, float], Tuple[float, float, float]]]:
|
||||
) -> dict[str, float | tuple[float, float] | tuple[float, float, float]]:
|
||||
"""Get all light settings.
|
||||
|
||||
Calculating all values takes <0.5ms.
|
||||
|
|
@ -1199,11 +1199,11 @@ class SunLightSettings:
|
|||
brightness_pct = self.calc_brightness_pct(percent, is_sleep)
|
||||
color_temp_kelvin = self.calc_color_temp_kelvin(percent, is_sleep)
|
||||
color_temp_mired: float = color_temperature_kelvin_to_mired(color_temp_kelvin)
|
||||
rgb_color: Tuple[float, float, float] = color_temperature_to_rgb(
|
||||
rgb_color: tuple[float, float, float] = color_temperature_to_rgb(
|
||||
color_temp_kelvin
|
||||
)
|
||||
xy_color: Tuple[float, float] = color_RGB_to_xy(*rgb_color)
|
||||
hs_color: Tuple[float, float] = color_xy_to_hs(*xy_color)
|
||||
xy_color: tuple[float, float] = color_RGB_to_xy(*rgb_color)
|
||||
hs_color: tuple[float, float] = color_xy_to_hs(*xy_color)
|
||||
return {
|
||||
"brightness_pct": brightness_pct,
|
||||
"color_temp_kelvin": color_temp_kelvin,
|
||||
|
|
@ -1224,19 +1224,19 @@ class TurnOnOffListener:
|
|||
self.lights = set()
|
||||
|
||||
# Tracks 'light.turn_off' service calls
|
||||
self.turn_off_event: Dict[str, Event] = {}
|
||||
self.turn_off_event: dict[str, Event] = {}
|
||||
# Tracks 'light.turn_on' service calls
|
||||
self.turn_on_event: Dict[str, Event] = {}
|
||||
self.turn_on_event: dict[str, Event] = {}
|
||||
# Keep 'asyncio.sleep' tasks that can be cancelled by 'light.turn_on' events
|
||||
self.sleep_tasks: Dict[str, asyncio.Task] = {}
|
||||
self.sleep_tasks: dict[str, asyncio.Task] = {}
|
||||
# Tracks which lights are manually controlled
|
||||
self.manual_control: Dict[str, bool] = {}
|
||||
self.manual_control: dict[str, bool] = {}
|
||||
# Counts the number of times (in a row) a light had a changed state.
|
||||
self.cnt_significant_changes: Dict[str, int] = defaultdict(int)
|
||||
self.cnt_significant_changes: dict[str, int] = defaultdict(int)
|
||||
# Track 'state_changed' events of self.lights resulting from this integration
|
||||
self.last_state_change: Dict[str, List[State]] = {}
|
||||
self.last_state_change: dict[str, list[State]] = {}
|
||||
# Track last 'service_data' to 'light.turn_on' resulting from this integration
|
||||
self.last_service_data: Dict[str, Dict[str, Any]] = {}
|
||||
self.last_service_data: dict[str, dict[str, Any]] = {}
|
||||
|
||||
# When a state is different `max_cnt_significant_changes` times in a row,
|
||||
# mark it as manually_controlled.
|
||||
|
|
@ -1326,7 +1326,7 @@ class TurnOnOffListener:
|
|||
# called with a color_temp outside of its range (and HA reports the
|
||||
# incorrect 'min_mireds' and 'max_mireds', which happens e.g., for
|
||||
# Philips Hue White GU10 Bluetooth lights).
|
||||
old_state: Optional[List[State]] = self.last_state_change.get(entity_id)
|
||||
old_state: list[State] | None = self.last_state_change.get(entity_id)
|
||||
if (
|
||||
old_state is not None
|
||||
and old_state[0].context.id == new_state.context.id
|
||||
|
|
@ -1398,7 +1398,7 @@ class TurnOnOffListener:
|
|||
"""
|
||||
if light not in self.last_state_change:
|
||||
return False
|
||||
old_states: List[State] = self.last_state_change[light]
|
||||
old_states: list[State] = self.last_state_change[light]
|
||||
await self.hass.helpers.entity_component.async_update_entity(light)
|
||||
new_state = self.hass.states.get(light)
|
||||
compare_to = functools.partial(
|
||||
|
|
@ -1456,7 +1456,7 @@ class TurnOnOffListener:
|
|||
return changed
|
||||
|
||||
async def maybe_cancel_adjusting(
|
||||
self, entity_id: str, off_to_on_event: Event, on_to_off_event: Optional[Event]
|
||||
self, entity_id: str, off_to_on_event: Event, on_to_off_event: Event | None
|
||||
) -> bool:
|
||||
"""Cancel the adjusting of a light if it has just been turned off.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue