Home Assistant Unofficial Reference 2024.12.1
significant_change.py
Go to the documentation of this file.
1 """Helper to test significant Light state changes."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.core import HomeAssistant, callback
8 from homeassistant.helpers.significant_change import check_absolute_change
9 
10 from . import ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_HS_COLOR
11 
12 
13 @callback
15  hass: HomeAssistant,
16  old_state: str,
17  old_attrs: dict,
18  new_state: str,
19  new_attrs: dict,
20  **kwargs: Any,
21 ) -> bool | None:
22  """Test if state significantly changed."""
23  if old_state != new_state:
24  return True
25 
26  if old_attrs.get(ATTR_EFFECT) != new_attrs.get(ATTR_EFFECT):
27  return True
28 
29  old_color = old_attrs.get(ATTR_HS_COLOR)
30  new_color = new_attrs.get(ATTR_HS_COLOR)
31 
32  if old_color and new_color:
33  # Range 0..360
34  if check_absolute_change(old_color[0], new_color[0], 5):
35  return True
36 
37  # Range 0..100
38  if check_absolute_change(old_color[1], new_color[1], 3):
39  return True
40 
42  old_attrs.get(ATTR_BRIGHTNESS), new_attrs.get(ATTR_BRIGHTNESS), 3
43  ):
44  return True
45 
47  # Default range 153..500
48  old_attrs.get(ATTR_COLOR_TEMP),
49  new_attrs.get(ATTR_COLOR_TEMP),
50  5,
51  ):
52  return True
53 
54  return False
bool|None async_check_significant_change(HomeAssistant hass, str old_state, dict old_attrs, str new_state, dict new_attrs, **Any kwargs)
bool check_absolute_change(float|None val1, float|None val2, float change)