Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up validate_entity_id #32137

Merged
merged 6 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
import os
import pathlib
import re
import threading
from time import monotonic
from types import MappingProxyType
Expand Down Expand Up @@ -63,7 +64,7 @@
ServiceNotFound,
Unauthorized,
)
from homeassistant.util import location, slugify
from homeassistant.util import location
from homeassistant.util.async_ import fire_coroutine_threadsafe, run_callback_threadsafe
import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem
Expand Down Expand Up @@ -103,12 +104,15 @@ def split_entity_id(entity_id: str) -> List[str]:
return entity_id.split(".", 1)


VALID_ENTITY_ID = re.compile(r"^(?!.+__)(?!_)[\da-z_]+(?<!_)\.(?!_)[\da-z_]+(?<!_)$")


def valid_entity_id(entity_id: str) -> bool:
"""Test if an entity ID is a valid format.

Format: <domain>.<entity> where both are slugs.
"""
return "." in entity_id and slugify(entity_id) == entity_id.replace(".", "_", 1)
return VALID_ENTITY_ID.match(entity_id) is not None


def valid_state(state: str) -> bool:
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/scripts/benchmark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,12 @@ def yield_events(event):
list(logbook.humanify(None, yield_events(event)))

return timer() - start


@benchmark
async def valid_entity_id(hass):
"""Run valid entity ID a million times."""
start = timer()
for _ in range(10 ** 6):
core.valid_entity_id("light.kitchen")
return timer() - start
31 changes: 31 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,3 +1206,34 @@ async def service_handler(call):

await hass.services.async_call("test_domain", "test_service", blocking=True)
assert len(runs) == 3


def test_valid_entity_id():
"""Test valid entity ID."""
for invalid in [
"_light.kitchen",
".kitchen",
".light.kitchen",
"light_.kitchen",
"light._kitchen",
"light.",
"light.kitchen__ceiling",
"light.kitchen_yo_",
"light.kitchen.",
"Light.kitchen",
"light.Kitchen",
"lightkitchen",
]:
assert not ha.valid_entity_id(invalid), invalid

for valid in [
"1.a",
"1light.kitchen",
"a.1",
"a.a",
"input_boolean.hello_world_0123",
"light.1kitchen",
"light.kitchen",
"light.something_yoo",
]:
assert ha.valid_entity_id(valid), valid