Validate in split_entity_id (#66835)

This commit is contained in:
Paulus Schoutsen 2022-02-18 16:11:17 -08:00 committed by GitHub
parent 3bf2be1765
commit 1bbc1f5f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 14 deletions

View File

@ -71,7 +71,7 @@ GROUP_BY_MINUTES = 15
EMPTY_JSON_OBJECT = "{}"
UNIT_OF_MEASUREMENT_JSON = '"unit_of_measurement":'
HA_DOMAIN_ENTITY_ID = f"{HA_DOMAIN}."
HA_DOMAIN_ENTITY_ID = f"{HA_DOMAIN}._"
CONFIG_SCHEMA = vol.Schema(
{DOMAIN: INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA}, extra=vol.ALLOW_EXTRA
@ -598,7 +598,7 @@ def _keep_event(hass, event, entities_filter):
if domain is None:
return False
return entities_filter is None or entities_filter(f"{domain}.")
return entities_filter is None or entities_filter(f"{domain}._")
def _augment_data_with_context(

View File

@ -141,9 +141,12 @@ TIMEOUT_EVENT_START = 15
_LOGGER = logging.getLogger(__name__)
def split_entity_id(entity_id: str) -> list[str]:
def split_entity_id(entity_id: str) -> tuple[str, str]:
"""Split a state entity ID into domain and object ID."""
return entity_id.split(".", 1)
domain, _, object_id = entity_id.partition(".")
if not domain or not object_id:
raise ValueError(f"Invalid entity ID {entity_id}")
return domain, object_id
VALID_ENTITY_ID = re.compile(r"^(?!.+__)(?!_)[\da-z_]+(?<!_)\.(?!_)[\da-z_]+(?<!_)$")

View File

@ -1220,9 +1220,9 @@ def test_entity_registry_items():
async def test_deprecated_disabled_by_str(hass, registry, caplog):
"""Test deprecated str use of disabled_by converts to enum and logs a warning."""
entry = registry.async_get_or_create(
"light",
"hue",
"5678",
domain="light.kitchen",
platform="hue",
unique_id="5678",
disabled_by=er.RegistryEntryDisabler.USER.value,
)
@ -1233,9 +1233,9 @@ async def test_deprecated_disabled_by_str(hass, registry, caplog):
async def test_deprecated_entity_category_str(hass, registry, caplog):
"""Test deprecated str use of entity_category converts to enum and logs a warning."""
entry = er.RegistryEntry(
"light",
"hue",
"5678",
entity_id="light.kitchen",
unique_id="5678",
platform="hue",
entity_category="diagnostic",
)
@ -1246,9 +1246,9 @@ async def test_deprecated_entity_category_str(hass, registry, caplog):
async def test_invalid_entity_category_str(hass, registry, caplog):
"""Test use of invalid entity category."""
entry = er.RegistryEntry(
"light",
"hue",
"5678",
entity_id="light.kitchen",
unique_id="5678",
platform="hue",
entity_category="invalid",
)

View File

@ -49,7 +49,17 @@ PST = dt_util.get_time_zone("America/Los_Angeles")
def test_split_entity_id():
"""Test split_entity_id."""
assert ha.split_entity_id("domain.object_id") == ["domain", "object_id"]
assert ha.split_entity_id("domain.object_id") == ("domain", "object_id")
with pytest.raises(ValueError):
ha.split_entity_id("")
with pytest.raises(ValueError):
ha.split_entity_id(".")
with pytest.raises(ValueError):
ha.split_entity_id("just_domain")
with pytest.raises(ValueError):
ha.split_entity_id("empty_object_id.")
with pytest.raises(ValueError):
ha.split_entity_id(".empty_domain")
def test_async_add_hass_job_schedule_callback():