1 """Platform allowing several switches to be grouped into one switch."""
3 from __future__
import annotations
8 import voluptuous
as vol
11 DOMAIN
as SWITCH_DOMAIN,
12 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
32 from .entity
import GroupEntity
34 DEFAULT_NAME =
"Switch Group"
40 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
42 vol.Required(CONF_ENTITIES): cv.entities_domain(SWITCH_DOMAIN),
43 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
44 vol.Optional(CONF_UNIQUE_ID): cv.string,
45 vol.Optional(CONF_ALL, default=
False): cv.boolean,
49 _LOGGER = logging.getLogger(__name__)
55 async_add_entities: AddEntitiesCallback,
56 discovery_info: DiscoveryInfoType |
None =
None,
58 """Set up the Switch Group platform."""
62 config.get(CONF_UNIQUE_ID),
64 config[CONF_ENTITIES],
65 config.get(CONF_ALL,
False),
73 config_entry: ConfigEntry,
74 async_add_entities: AddEntitiesCallback,
76 """Initialize Switch Group config entry."""
77 registry = er.async_get(hass)
78 entities = er.async_validate_entity_ids(
79 registry, config_entry.options[CONF_ENTITIES]
84 config_entry.entry_id,
87 config_entry.options.get(CONF_ALL),
95 hass: HomeAssistant, name: str, validated_config: dict[str, Any]
97 """Create a preview sensor."""
101 validated_config[CONF_ENTITIES],
102 validated_config.get(CONF_ALL,
False),
107 """Representation of a switch group."""
109 _attr_available =
False
110 _attr_should_poll =
False
114 unique_id: str |
None,
116 entity_ids: list[str],
119 """Initialize a switch group."""
130 """Forward the turn_on command to all switches in the group."""
131 data = {ATTR_ENTITY_ID: self.
_entity_ids_entity_ids}
132 _LOGGER.debug(
"Forwarded turn_on command: %s", data)
134 await self.
hasshass.services.async_call(
143 """Forward the turn_off command to all switches in the group."""
144 data = {ATTR_ENTITY_ID: self.
_entity_ids_entity_ids}
145 await self.
hasshass.services.async_call(
155 """Query all members and determine the switch group state."""
159 if (state := self.
hasshass.states.get(entity_id))
is not None
162 valid_state = self.
modemode(
163 state
not in (STATE_UNKNOWN, STATE_UNAVAILABLE)
for state
in states
171 self.
_attr_is_on_attr_is_on = self.
modemode(state == STATE_ON
for state
in states)
None __init__(self, str|None unique_id, str name, list[str] entity_ids, bool|None mode)
None async_turn_on(self, **Any kwargs)
None async_update_group_state(self)
_attr_extra_state_attributes
None async_turn_off(self, **Any kwargs)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
SwitchGroup async_create_preview_switch(HomeAssistant hass, str name, dict[str, Any] validated_config)