Add a state attribute enum to group and remove unused constants (#188738)

This commit is contained in:
epenet
2026-10-26 15:26:36 +00:00
committed by GitHub
parent 0fb10c8bf6
commit 942e8eab4f
18 changed files with 104 additions and 62 deletions
@@ -13,7 +13,6 @@ from homeassistant.components.binary_sensor import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_CLASS,
CONF_ENTITIES,
CONF_NAME,
@@ -30,6 +29,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Binary Sensor Group"
@@ -120,7 +120,9 @@ class BinarySensorGroup(GroupEntity, BinarySensorEntity):
super().__init__()
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
self._device_class = device_class
self.mode = any
+4 -1
View File
@@ -26,6 +26,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Button group"
@@ -108,7 +109,9 @@ class ButtonGroup(GroupEntity, ButtonEntity):
"""Initialize a button group."""
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
@override
+5 -2
View File
@@ -13,7 +13,7 @@ from homeassistant.components.calendar import (
CalendarEvent,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, CONF_ENTITIES, CONF_NAME, CONF_UNIQUE_ID
from homeassistant.const import CONF_ENTITIES, CONF_NAME, CONF_UNIQUE_ID
from homeassistant.core import Event, EventStateReportedData, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity_component import EntityComponent
@@ -25,6 +25,7 @@ from homeassistant.helpers.event import async_track_state_report_event
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Calendar group"
@@ -118,7 +119,9 @@ class CalendarGroup(GroupEntity, CalendarEntity):
"""Initialize a calendar group."""
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
self._member_events: list[CalendarEvent] = []
+8 -8
View File
@@ -20,11 +20,6 @@ DATA_COMPONENT: HassKey[EntityComponent[Group]] = HassKey(DOMAIN)
REG_KEY: HassKey[GroupIntegrationRegistry] = HassKey(f"{DOMAIN}_registry")
GROUP_ORDER: HassKey[int] = HassKey("group_order")
ATTR_ADD_ENTITIES = "add_entities"
ATTR_REMOVE_ENTITIES = "remove_entities"
ATTR_AUTO = "auto"
ATTR_ENTITIES = "entities"
SERVICE_SET = "set"
SERVICE_REMOVE = "remove"
@@ -43,9 +38,6 @@ PLATFORMS = [
Platform.SWITCH,
Platform.VALVE,
]
ATTR_OBJECT_ID = "object_id"
ATTR_ORDER = "order"
ATTR_ALL = "all"
class GroupService(StrEnum):
@@ -66,3 +58,11 @@ class GroupServiceArgument(StrEnum):
NAME = "name"
OBJECT_ID = "object_id"
REMOVE_ENTITIES = "remove_entities"
class GroupEntityStateAttribute(StrEnum):
"""State attributes for group entities."""
AUTO = "auto"
ENTITY_ID = "entity_id"
ORDER = "order"
+4 -1
View File
@@ -32,6 +32,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
from .util import reduce_attribute
@@ -121,7 +122,9 @@ class CoverGroup(GroupEntity, CoverEntity):
}
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entities}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entities
}
self._attr_unique_id = unique_id
@callback
+20 -6
View File
@@ -6,7 +6,6 @@ import logging
from typing import Any, override
from homeassistant.const import (
ATTR_ENTITY_ID,
STATE_OFF,
STATE_ON,
EntityCapabilityAttribute,
@@ -27,7 +26,13 @@ from homeassistant.helpers.entity import Entity, async_generate_entity_id
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import async_track_state_change_event
from .const import ATTR_AUTO, ATTR_ORDER, DATA_COMPONENT, DOMAIN, GROUP_ORDER, REG_KEY
from .const import (
DATA_COMPONENT,
DOMAIN,
GROUP_ORDER,
REG_KEY,
GroupEntityStateAttribute,
)
from .registry import GroupIntegrationRegistry, SingleStateType
ENTITY_ID_FORMAT = DOMAIN + ".{}"
@@ -41,7 +46,7 @@ class GroupEntity(Entity):
"""Representation of a Group of entities."""
_unrecorded_attributes = frozenset(
{ATTR_ENTITY_ID, EntityCapabilityAttribute.GROUP_ENTITIES}
{GroupEntityStateAttribute.ENTITY_ID, EntityCapabilityAttribute.GROUP_ENTITIES}
)
_attr_should_poll = False
@@ -148,7 +153,13 @@ class GroupEntity(Entity):
class Group(Entity):
"""Track a group of entity ids."""
_unrecorded_attributes = frozenset({ATTR_ENTITY_ID, ATTR_ORDER, ATTR_AUTO})
_unrecorded_attributes = frozenset(
{
GroupEntityStateAttribute.ENTITY_ID,
GroupEntityStateAttribute.ORDER,
GroupEntityStateAttribute.AUTO,
}
)
_attr_should_poll = False
tracking: tuple[str, ...]
@@ -278,9 +289,12 @@ class Group(Entity):
@override
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes for the group."""
data = {ATTR_ENTITY_ID: self.tracking, ATTR_ORDER: self._order}
data: dict[str, Any] = {
GroupEntityStateAttribute.ENTITY_ID: self.tracking,
GroupEntityStateAttribute.ORDER: self._order,
}
if self.created_by_service:
data[ATTR_AUTO] = True
data[GroupEntityStateAttribute.AUTO] = True
return data
+4 -2
View File
@@ -14,7 +14,6 @@ from homeassistant.components.event import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_ENTITIES,
CONF_NAME,
CONF_UNIQUE_ID,
@@ -31,6 +30,7 @@ from homeassistant.helpers.entity_platform import (
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Event group"
@@ -113,7 +113,9 @@ class EventGroup(GroupEntity, EventEntity):
"""Initialize an event group."""
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
self._attr_event_types = []
+4 -1
View File
@@ -35,6 +35,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
from .util import attribute_equal, most_frequent_attribute, reduce_attribute
@@ -115,7 +116,9 @@ class FanGroup(GroupEntity, FanEntity):
self._speed_count = 100
self._is_on: bool | None = False
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entities}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entities
}
self._attr_unique_id = unique_id
@property
+4 -1
View File
@@ -39,6 +39,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
from .util import find_state_attributes, mean_circle, mean_tuple, reduce_attribute
@@ -146,7 +147,9 @@ class LightGroup(GroupEntity, LightEntity):
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
self.mode = any
if mode:
+4 -2
View File
@@ -14,7 +14,6 @@ from homeassistant.components.lock import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_ENTITIES,
CONF_NAME,
CONF_UNIQUE_ID,
@@ -30,6 +29,7 @@ from homeassistant.helpers.entity_platform import (
from homeassistant.helpers.group import GenericGroup
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Lock Group"
@@ -117,7 +117,9 @@ class LockGroup(GroupEntity, LockEntity):
self._attr_supported_features = LockEntityFeature.OPEN
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
@callback
@@ -43,6 +43,8 @@ from homeassistant.helpers.entity_platform import (
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
KEY_ANNOUNCE = "announce"
KEY_CLEAR_PLAYLIST = "clear_playlist"
KEY_ENQUEUE = "enqueue"
@@ -112,7 +114,7 @@ def async_create_preview_media_player(
class MediaPlayerGroup(MediaPlayerEntity):
"""Representation of a Media Group."""
_unrecorded_attributes = frozenset({ATTR_ENTITY_ID})
_unrecorded_attributes = frozenset({GroupEntityStateAttribute.ENTITY_ID})
_attr_available: bool = False
_attr_should_poll = False
@@ -260,7 +262,7 @@ class MediaPlayerGroup(MediaPlayerEntity):
@override
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes for the media group."""
return {ATTR_ENTITY_ID: self._entities}
return {GroupEntityStateAttribute.ENTITY_ID: self._entities}
@override
async def async_clear_playlist(self) -> None:
+4 -1
View File
@@ -30,6 +30,7 @@ from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
CONF_SERVICES = "services"
@@ -167,7 +168,9 @@ class NotifyGroup(GroupEntity, NotifyEntity):
"""Initialize a NotifyGroup."""
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
@override
+5 -3
View File
@@ -27,7 +27,6 @@ from homeassistant.components.sensor import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_CLASS,
CONF_ENTITIES,
CONF_NAME,
@@ -57,7 +56,7 @@ from homeassistant.helpers.issue_registry import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import CONF_IGNORE_NON_NUMERIC, DOMAIN
from .const import CONF_IGNORE_NON_NUMERIC, DOMAIN, GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Sensor Group"
@@ -489,7 +488,10 @@ class SensorGroup(GroupEntity, SensorEntity):
@override
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the sensor."""
return {ATTR_ENTITY_ID: self._entity_ids, **self._extra_state_attribute}
return {
GroupEntityStateAttribute.ENTITY_ID: self._entity_ids,
**self._extra_state_attribute,
}
@property
@override
+4 -1
View File
@@ -29,6 +29,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
DEFAULT_NAME = "Switch Group"
@@ -120,7 +121,9 @@ class SwitchGroup(GroupEntity, SwitchEntity):
self._entity_ids = entity_ids
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entity_ids
}
self._attr_unique_id = unique_id
self.mode = any
if mode:
+4 -1
View File
@@ -32,6 +32,7 @@ from homeassistant.helpers.entity_platform import (
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import GroupEntityStateAttribute
from .entity import GroupEntity
from .util import reduce_attribute
@@ -117,7 +118,9 @@ class ValveGroup(GroupEntity, ValveEntity):
}
self._attr_name = name
self._attr_extra_state_attributes = {ATTR_ENTITY_ID: entities}
self._attr_extra_state_attributes = {
GroupEntityStateAttribute.ENTITY_ID: entities
}
self._attr_unique_id = unique_id
@callback
+10 -16
View File
@@ -4,13 +4,7 @@ All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from homeassistant.components.group import DOMAIN, SERVICE_REMOVE, SERVICE_SET
from homeassistant.components.group.const import (
ATTR_ADD_ENTITIES,
ATTR_ENTITIES,
ATTR_OBJECT_ID,
)
from homeassistant.const import ATTR_ICON, ATTR_NAME, SERVICE_RELOAD
from homeassistant.components.group import DOMAIN, GroupService, GroupServiceArgument
from homeassistant.core import HomeAssistant, callback
@@ -22,7 +16,7 @@ def reload(hass: HomeAssistant) -> None:
@callback
def async_reload(hass: HomeAssistant) -> None:
"""Reload the automation from config."""
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_RELOAD))
hass.async_create_task(hass.services.async_call(DOMAIN, GroupService.RELOAD))
def set_group(
@@ -58,20 +52,20 @@ def async_set_group(
data = {
key: value
for key, value in (
(ATTR_OBJECT_ID, object_id),
(ATTR_NAME, name),
(ATTR_ENTITIES, entity_ids),
(ATTR_ICON, icon),
(ATTR_ADD_ENTITIES, add),
(GroupServiceArgument.OBJECT_ID, object_id),
(GroupServiceArgument.NAME, name),
(GroupServiceArgument.ENTITIES, entity_ids),
(GroupServiceArgument.ICON, icon),
(GroupServiceArgument.ADD_ENTITIES, add),
)
if value is not None
}
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_SET, data))
hass.async_create_task(hass.services.async_call(DOMAIN, GroupService.SET, data))
@callback
def async_remove(hass: HomeAssistant, object_id: str) -> None:
"""Remove a user group."""
data = {ATTR_OBJECT_ID: object_id}
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_REMOVE, data))
data = {GroupServiceArgument.OBJECT_ID: object_id}
hass.async_create_task(hass.services.async_call(DOMAIN, GroupService.REMOVE, data))
+9 -9
View File
@@ -18,7 +18,7 @@ from pytest_homeassistant.common import (
from homeassistant.components import group
from homeassistant.components.group import DOMAIN
from homeassistant.components.group.const import ATTR_AUTO, ATTR_ORDER
from homeassistant.components.group.const import GroupEntityStateAttribute
from homeassistant.components.group.registry import GroupIntegrationRegistry
from homeassistant.components.lock import LockState
from homeassistant.const import (
@@ -253,7 +253,7 @@ async def test_monitor_group(hass: HomeAssistant) -> None:
group_state = hass.states.get(test_group.entity_id)
assert group_state.state == STATE_ON
assert group_state.attributes.get(ATTR_AUTO)
assert group_state.attributes.get(GroupEntityStateAttribute.AUTO)
async def test_group_turns_off_if_all_off(hass: HomeAssistant) -> None:
@@ -1008,16 +1008,16 @@ async def test_setup(hass: HomeAssistant) -> None:
assert {test_group.entity_id, "light.bowl"} == set(
group_state.attributes["entity_id"]
)
assert group_state.attributes.get(ATTR_AUTO) is None
assert group_state.attributes.get(GroupEntityStateAttribute.AUTO) is None
assert group_state.attributes.get(ATTR_ICON) == "mdi:work"
assert group_state.attributes.get(ATTR_ORDER) == 3
assert group_state.attributes.get(GroupEntityStateAttribute.ORDER) == 3
group_state = hass.states.get(f"{group.DOMAIN}.test_group")
assert group_state.state == STATE_UNKNOWN
assert set(group_state.attributes["entity_id"]) == {"sensor.happy", "hello.world"}
assert group_state.attributes.get(ATTR_AUTO) is None
assert group_state.attributes.get(GroupEntityStateAttribute.AUTO) is None
assert group_state.attributes.get(ATTR_ICON) is None
assert group_state.attributes.get(ATTR_ORDER) == 0
assert group_state.attributes.get(GroupEntityStateAttribute.ORDER) == 0
async def test_service_group_services(hass: HomeAssistant) -> None:
@@ -1112,7 +1112,7 @@ async def test_service_group_set_group_remove_group(hass: HomeAssistant) -> None
group_state = hass.states.get("group.user_test_group")
assert group_state
assert group_state.attributes[ATTR_AUTO]
assert group_state.attributes[GroupEntityStateAttribute.AUTO]
assert group_state.attributes["friendly_name"] == "Test"
common.async_set_group(hass, "user_test_group", entity_ids=["test.entity_bla1"])
@@ -1120,7 +1120,7 @@ async def test_service_group_set_group_remove_group(hass: HomeAssistant) -> None
group_state = hass.states.get("group.user_test_group")
assert group_state
assert group_state.attributes[ATTR_AUTO]
assert group_state.attributes[GroupEntityStateAttribute.AUTO]
assert group_state.attributes["friendly_name"] == "Test"
assert list(group_state.attributes["entity_id"]) == ["test.entity_bla1"]
@@ -1135,7 +1135,7 @@ async def test_service_group_set_group_remove_group(hass: HomeAssistant) -> None
group_state = hass.states.get("group.user_test_group")
assert group_state
assert group_state.attributes[ATTR_AUTO]
assert group_state.attributes[GroupEntityStateAttribute.AUTO]
assert group_state.attributes["friendly_name"] == "Test2"
assert group_state.attributes["icon"] == "mdi:camera"
assert sorted(group_state.attributes["entity_id"]) == sorted(
+3 -3
View File
@@ -7,7 +7,7 @@ from pytest_homeassistant.common import async_fire_time_changed
from pytest_homeassistant.components.recorder.common import async_wait_recording_done
from homeassistant.components import group
from homeassistant.components.group.const import ATTR_AUTO, ATTR_ORDER
from homeassistant.components.group.const import GroupEntityStateAttribute
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON
@@ -50,7 +50,7 @@ async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant)
for entity_states in states.values():
for state in entity_states:
if split_entity_id(state.entity_id)[0] == group.DOMAIN:
assert ATTR_AUTO not in state.attributes
assert GroupEntityStateAttribute.AUTO not in state.attributes
assert ATTR_ENTITY_ID not in state.attributes
assert ATTR_ORDER not in state.attributes
assert GroupEntityStateAttribute.ORDER not in state.attributes
assert ATTR_FRIENDLY_NAME in state.attributes