134 lines
3.6 KiB
Python
134 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from app.ha.discovery import EntityRole, classify_entity, discover_entities
|
|
from app.ha.models import HaEntitySummary
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("entity", "role", "learnable"),
|
|
[
|
|
(
|
|
HaEntitySummary(
|
|
entity_id="sensor.temperature",
|
|
domain="sensor",
|
|
device_class="temperature",
|
|
state_class="measurement",
|
|
unit_of_measurement="°C",
|
|
),
|
|
EntityRole.MEASUREMENT,
|
|
True,
|
|
),
|
|
(
|
|
HaEntitySummary(
|
|
entity_id="binary_sensor.motion",
|
|
domain="binary_sensor",
|
|
device_class="motion",
|
|
),
|
|
EntityRole.BINARY_CONTEXT,
|
|
True,
|
|
),
|
|
(
|
|
HaEntitySummary(entity_id="person.simon", domain="person"),
|
|
EntityRole.CONTEXT,
|
|
True,
|
|
),
|
|
(
|
|
HaEntitySummary(entity_id="light.living_room", domain="light"),
|
|
EntityRole.ACTUATOR,
|
|
False,
|
|
),
|
|
(
|
|
HaEntitySummary(entity_id="camera.driveway", domain="camera"),
|
|
EntityRole.UNSUPPORTED,
|
|
False,
|
|
),
|
|
],
|
|
)
|
|
def test_classify_entity(
|
|
entity: HaEntitySummary,
|
|
role: EntityRole,
|
|
learnable: bool,
|
|
) -> None:
|
|
result = classify_entity(entity)
|
|
assert result.role is role
|
|
assert result.learnable is learnable
|
|
|
|
|
|
def test_discovery_filters_domain_and_learnable() -> None:
|
|
entities = [
|
|
HaEntitySummary(
|
|
entity_id="sensor.temperature",
|
|
domain="sensor",
|
|
device_class="temperature",
|
|
),
|
|
HaEntitySummary(entity_id="sensor.status", domain="sensor"),
|
|
HaEntitySummary(
|
|
entity_id="binary_sensor.motion",
|
|
domain="binary_sensor",
|
|
device_class="motion",
|
|
),
|
|
]
|
|
|
|
result = discover_entities(entities, domains={" SENSOR "}, learnable=True)
|
|
|
|
assert [item.entity_id for item in result] == ["sensor.temperature"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("entity", "category"),
|
|
[
|
|
(
|
|
HaEntitySummary(entity_id="climate.bad", domain="climate"),
|
|
"heating",
|
|
),
|
|
(
|
|
HaEntitySummary(entity_id="lock.front_door", domain="lock"),
|
|
"lock",
|
|
),
|
|
(
|
|
HaEntitySummary(entity_id="input_boolean.sleep_mode", domain="input_boolean"),
|
|
"helper",
|
|
),
|
|
(
|
|
HaEntitySummary(entity_id="media_player.tv", domain="media_player"),
|
|
"media_tv",
|
|
),
|
|
(
|
|
HaEntitySummary(
|
|
entity_id="sensor.brightness",
|
|
domain="sensor",
|
|
device_class="illuminance",
|
|
),
|
|
"brightness",
|
|
),
|
|
(
|
|
HaEntitySummary(
|
|
entity_id="binary_sensor.motion",
|
|
domain="binary_sensor",
|
|
device_class="motion",
|
|
),
|
|
"presence_motion",
|
|
),
|
|
],
|
|
)
|
|
def test_classify_entity_categories(entity: HaEntitySummary, category: str) -> None:
|
|
assert classify_entity(entity).category == category
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"entity",
|
|
[
|
|
HaEntitySummary(entity_id="automation.lights", domain="automation"),
|
|
HaEntitySummary(entity_id="update.core", domain="update"),
|
|
],
|
|
)
|
|
def test_classify_excludes_non_actuator_management_entities(
|
|
entity: HaEntitySummary,
|
|
) -> None:
|
|
result = classify_entity(entity)
|
|
|
|
assert result.role is EntityRole.UNSUPPORTED
|
|
assert result.learnable is False
|