Home Assistant Unofficial Reference 2024.12.1
significant_change.py
Go to the documentation of this file.
1 """Helper to test significant Alarm Control Panel state changes."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.core import HomeAssistant, callback
8 
9 from . import ATTR_CHANGED_BY, ATTR_CODE_ARM_REQUIRED
10 
11 SIGNIFICANT_ATTRIBUTES: set[str] = {
12  ATTR_CHANGED_BY,
13  ATTR_CODE_ARM_REQUIRED,
14 }
15 
16 
17 @callback
19  hass: HomeAssistant,
20  old_state: str,
21  old_attrs: dict,
22  new_state: str,
23  new_attrs: dict,
24  **kwargs: Any,
25 ) -> bool | None:
26  """Test if state significantly changed."""
27  if old_state != new_state:
28  return True
29 
30  old_attrs_s = set(
31  {k: v for k, v in old_attrs.items() if k in SIGNIFICANT_ATTRIBUTES}.items()
32  )
33  new_attrs_s = set(
34  {k: v for k, v in new_attrs.items() if k in SIGNIFICANT_ATTRIBUTES}.items()
35  )
36  changed_attrs: set[str] = {item[0] for item in old_attrs_s ^ new_attrs_s}
37 
38  if changed_attrs:
39  return True
40 
41  # no significant attribute change detected
42  return False
bool|None async_check_significant_change(HomeAssistant hass, str old_state, dict old_attrs, str new_state, dict new_attrs, **Any kwargs)