1 """Reproduce an Fan state."""
3 from __future__
import annotations
6 from collections.abc
import Iterable
26 SERVICE_SET_DIRECTION,
27 SERVICE_SET_PERCENTAGE,
28 SERVICE_SET_PRESET_MODE,
31 _LOGGER = logging.getLogger(__name__)
33 VALID_STATES = {STATE_ON, STATE_OFF}
36 SPEED_AND_MODE_ATTRIBUTES = {
37 ATTR_PERCENTAGE: SERVICE_SET_PERCENTAGE,
38 ATTR_PRESET_MODE: SERVICE_SET_PRESET_MODE,
42 ATTR_DIRECTION: SERVICE_SET_DIRECTION,
43 ATTR_OSCILLATING: SERVICE_OSCILLATE,
51 context: Context |
None =
None,
52 reproduce_options: dict[str, Any] |
None =
None,
54 """Reproduce a single state."""
55 if (cur_state := hass.states.get(state.entity_id))
is None:
56 _LOGGER.warning(
"Unable to find entity %s", state.entity_id)
59 if state.state
not in VALID_STATES:
61 "Invalid state specified for %s: %s", state.entity_id, state.state
65 service_calls: dict[str, dict[str, Any]] = {}
67 if state.state == STATE_ON:
69 if cur_state.state != STATE_ON:
73 service_calls[SERVICE_TURN_ON] = {
74 attr: state.attributes.get(attr)
75 for attr
in SPEED_AND_MODE_ATTRIBUTES
76 if state.attributes.get(attr)
is not None
86 for attr, service
in SPEED_AND_MODE_ATTRIBUTES.items():
87 value = state.attributes.get(attr)
88 if value
is not None and value != cur_state.attributes.get(attr):
89 service_calls[service] = {attr: value}
95 for attr, service
in SIMPLE_ATTRIBUTES.items():
96 if (value := state.attributes.get(attr)) != cur_state.attributes.get(attr):
97 service_calls[service] = {attr: value}
98 elif state.state == STATE_OFF
and cur_state.state != state.state:
99 service_calls[SERVICE_TURN_OFF] = {}
101 for service, data
in service_calls.items():
102 await hass.services.async_call(
105 {ATTR_ENTITY_ID: state.entity_id, **data},
113 states: Iterable[State],
115 context: Context |
None =
None,
116 reproduce_options: dict[str, Any] |
None =
None,
118 """Reproduce Fan states."""
119 await asyncio.gather(
122 hass, state, context=context, reproduce_options=reproduce_options
None async_reproduce_states(HomeAssistant hass, Iterable[State] states, *Context|None context=None, dict[str, Any]|None reproduce_options=None)
None _async_reproduce_state(HomeAssistant hass, State state, *Context|None context=None, dict[str, Any]|None reproduce_options=None)