Speed up group setup (#38048)
This commit is contained in:
parent
6e87c2ad3e
commit
4a5a09a0e9
2 changed files with 31 additions and 1 deletions
|
@ -39,6 +39,7 @@ from homeassistant.loader import bind_hass
|
|||
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
DOMAIN = "group"
|
||||
GROUP_ORDER = "group_order"
|
||||
|
||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||
|
||||
|
@ -407,16 +408,23 @@ class Group(Entity):
|
|||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
hass.data.setdefault(GROUP_ORDER, 0)
|
||||
|
||||
group = Group(
|
||||
hass,
|
||||
name,
|
||||
order=len(hass.states.async_entity_ids(DOMAIN)),
|
||||
order=hass.data[GROUP_ORDER],
|
||||
icon=icon,
|
||||
user_defined=user_defined,
|
||||
entity_ids=entity_ids,
|
||||
mode=mode,
|
||||
)
|
||||
|
||||
# Keep track of the group order without iterating
|
||||
# every state in the state machine every time
|
||||
# we setup a new group
|
||||
hass.data[GROUP_ORDER] += 1
|
||||
|
||||
group.entity_id = async_generate_entity_id(
|
||||
ENTITY_ID_FORMAT, object_id or name, hass=hass
|
||||
)
|
||||
|
|
|
@ -490,3 +490,25 @@ async def test_service_group_set_group_remove_group(hass):
|
|||
|
||||
group_state = hass.states.get("group.user_test_group")
|
||||
assert group_state is None
|
||||
|
||||
|
||||
async def test_group_order(hass):
|
||||
"""Test that order gets incremented when creating a new group."""
|
||||
hass.states.async_set("light.bowl", STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"group",
|
||||
{
|
||||
"group": {
|
||||
"group_zero": {"entities": "light.Bowl", "icon": "mdi:work"},
|
||||
"group_one": {"entities": "light.Bowl", "icon": "mdi:work"},
|
||||
"group_two": {"entities": "light.Bowl", "icon": "mdi:work"},
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("group.group_zero").attributes["order"] == 0
|
||||
assert hass.states.get("group.group_one").attributes["order"] == 1
|
||||
assert hass.states.get("group.group_two").attributes["order"] == 2
|
||||
|
|
Loading…
Add table
Reference in a new issue