1 """Reproduce an Light state."""
3 from __future__
import annotations
6 from collections.abc
import Iterable, Mapping
8 from typing
import Any, NamedTuple, cast
35 _LOGGER = logging.getLogger(__name__)
37 VALID_STATES = {STATE_ON, STATE_OFF}
39 ATTR_GROUP = [ATTR_BRIGHTNESS, ATTR_EFFECT]
52 """Map service data parameter to state attribute for a color mode."""
58 COLOR_MODE_TO_ATTRIBUTE = {
59 ColorMode.COLOR_TEMP:
ColorModeAttr(ATTR_COLOR_TEMP, ATTR_COLOR_TEMP),
62 ColorMode.RGBW:
ColorModeAttr(ATTR_RGBW_COLOR, ATTR_RGBW_COLOR),
63 ColorMode.RGBWW:
ColorModeAttr(ATTR_RGBWW_COLOR, ATTR_RGBWW_COLOR),
70 """Test if color_mode is same."""
71 cur_color_mode = cur_state.attributes.get(ATTR_COLOR_MODE, ColorMode.UNKNOWN)
72 saved_color_mode = state.attributes.get(ATTR_COLOR_MODE, ColorMode.UNKNOWN)
75 if saved_color_mode == ColorMode.UNKNOWN:
77 return cast(bool, cur_color_mode == saved_color_mode)
84 context: Context |
None =
None,
85 reproduce_options: dict[str, Any] |
None =
None,
87 """Reproduce a single state."""
88 if (cur_state := hass.states.get(state.entity_id))
is None:
89 _LOGGER.warning(
"Unable to find entity %s", state.entity_id)
92 if state.state
not in VALID_STATES:
94 "Invalid state specified for %s: %s", state.entity_id, state.state
100 cur_state.state == state.state
104 for attr
in ATTR_GROUP + COLOR_GROUP
109 service_data: dict[str, Any] = {ATTR_ENTITY_ID: state.entity_id}
111 if reproduce_options
is not None and ATTR_TRANSITION
in reproduce_options:
112 service_data[ATTR_TRANSITION] = reproduce_options[ATTR_TRANSITION]
114 if state.state == STATE_ON:
115 service = SERVICE_TURN_ON
116 for attr
in ATTR_GROUP:
118 if (attr_state := state.attributes.get(attr))
is not None:
119 service_data[attr] = attr_state
122 state.attributes.get(ATTR_COLOR_MODE, ColorMode.UNKNOWN)
125 color_mode = state.attributes[ATTR_COLOR_MODE]
126 if cm_attr := COLOR_MODE_TO_ATTRIBUTE.get(color_mode):
127 if (cm_attr_state := state.attributes.get(cm_attr.state_attr))
is None:
129 "Color mode %s specified but attribute %s missing for: %s",
135 service_data[cm_attr.parameter] = cm_attr_state
138 for color_attr
in COLOR_GROUP:
139 if (color_attr_state := state.attributes.get(color_attr))
is not None:
140 service_data[color_attr] = color_attr_state
143 elif state.state == STATE_OFF:
144 service = SERVICE_TURN_OFF
146 await hass.services.async_call(
147 DOMAIN, service, service_data, context=context, blocking=
True
153 states: Iterable[State],
155 context: Context |
None =
None,
156 reproduce_options: dict[str, Any] |
None =
None,
158 """Reproduce Light states."""
159 await asyncio.gather(
162 hass, state, context=context, reproduce_options=reproduce_options
170 """Return true if the given attributes are equal."""
171 return attr1.get(attr_str) == attr2.get(attr_str)
bool check_attr_equal(Mapping attr1, Mapping attr2, str attr_str)
bool _color_mode_same(State cur_state, State state)
None _async_reproduce_state(HomeAssistant hass, State state, *Context|None context=None, dict[str, Any]|None reproduce_options=None)
None async_reproduce_states(HomeAssistant hass, Iterable[State] states, *Context|None context=None, dict[str, Any]|None reproduce_options=None)