mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Cleanup onewire tests (#92117)
This commit is contained in:
parent
58c5d98958
commit
7dfbfd45ae
@ -1,94 +1,14 @@
|
|||||||
"""Tests for 1-Wire integration."""
|
"""Tests for 1-Wire integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from types import MappingProxyType
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
from pyownet.protocol import ProtocolError
|
from pyownet.protocol import ProtocolError
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import Platform
|
||||||
ATTR_ENTITY_ID,
|
|
||||||
ATTR_IDENTIFIERS,
|
|
||||||
ATTR_MANUFACTURER,
|
|
||||||
ATTR_MODEL,
|
|
||||||
ATTR_NAME,
|
|
||||||
ATTR_STATE,
|
|
||||||
ATTR_VIA_DEVICE,
|
|
||||||
Platform,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
from homeassistant.helpers.device_registry import DeviceRegistry
|
|
||||||
from homeassistant.helpers.entity_registry import EntityRegistry, RegistryEntryDisabler
|
|
||||||
|
|
||||||
from .const import (
|
from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES
|
||||||
ATTR_DEFAULT_DISABLED,
|
|
||||||
ATTR_DEVICE_FILE,
|
|
||||||
ATTR_ENTITY_CATEGORY,
|
|
||||||
ATTR_INJECT_READS,
|
|
||||||
ATTR_UNIQUE_ID,
|
|
||||||
FIXED_ATTRIBUTES,
|
|
||||||
MOCK_OWPROXY_DEVICES,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def check_and_enable_disabled_entities(
|
|
||||||
entity_registry: EntityRegistry, expected_entities: MappingProxyType
|
|
||||||
) -> None:
|
|
||||||
"""Ensure that the expected_entities are correctly disabled."""
|
|
||||||
for expected_entity in expected_entities:
|
|
||||||
if expected_entity.get(ATTR_DEFAULT_DISABLED):
|
|
||||||
entity_id = expected_entity[ATTR_ENTITY_ID]
|
|
||||||
registry_entry = entity_registry.entities.get(entity_id)
|
|
||||||
assert registry_entry, f"{entity_id} not found in registry"
|
|
||||||
assert registry_entry.disabled
|
|
||||||
assert registry_entry.disabled_by is RegistryEntryDisabler.INTEGRATION
|
|
||||||
entity_registry.async_update_entity(entity_id, **{"disabled_by": None})
|
|
||||||
|
|
||||||
|
|
||||||
def check_device_registry(
|
|
||||||
device_registry: DeviceRegistry, expected_devices: list[MappingProxyType]
|
|
||||||
) -> None:
|
|
||||||
"""Ensure that the expected_devices are correctly registered."""
|
|
||||||
for expected_device in expected_devices:
|
|
||||||
registry_entry = device_registry.async_get_device(
|
|
||||||
expected_device[ATTR_IDENTIFIERS]
|
|
||||||
)
|
|
||||||
assert registry_entry is not None
|
|
||||||
assert registry_entry.identifiers == expected_device[ATTR_IDENTIFIERS]
|
|
||||||
assert registry_entry.manufacturer == expected_device[ATTR_MANUFACTURER]
|
|
||||||
assert registry_entry.name == expected_device[ATTR_NAME]
|
|
||||||
assert registry_entry.model == expected_device[ATTR_MODEL]
|
|
||||||
if expected_via_device := expected_device.get(ATTR_VIA_DEVICE):
|
|
||||||
assert registry_entry.via_device_id is not None
|
|
||||||
parent_entry = device_registry.async_get_device({expected_via_device})
|
|
||||||
assert parent_entry is not None
|
|
||||||
assert registry_entry.via_device_id == parent_entry.id
|
|
||||||
else:
|
|
||||||
assert registry_entry.via_device_id is None
|
|
||||||
|
|
||||||
|
|
||||||
def check_entities(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
entity_registry: EntityRegistry,
|
|
||||||
expected_entities: MappingProxyType,
|
|
||||||
) -> None:
|
|
||||||
"""Ensure that the expected_entities are correct."""
|
|
||||||
for expected_entity in expected_entities:
|
|
||||||
entity_id = expected_entity[ATTR_ENTITY_ID]
|
|
||||||
registry_entry = entity_registry.entities.get(entity_id)
|
|
||||||
assert registry_entry is not None, f"{entity_id} not found in registry"
|
|
||||||
assert registry_entry.entity_category == expected_entity.get(
|
|
||||||
ATTR_ENTITY_CATEGORY
|
|
||||||
)
|
|
||||||
assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID]
|
|
||||||
state = hass.states.get(entity_id)
|
|
||||||
assert state.state == expected_entity[ATTR_STATE]
|
|
||||||
assert state.attributes[ATTR_DEVICE_FILE] == expected_entity.get(
|
|
||||||
ATTR_DEVICE_FILE, registry_entry.unique_id
|
|
||||||
)
|
|
||||||
for attr in FIXED_ATTRIBUTES:
|
|
||||||
assert state.attributes.get(attr) == expected_entity.get(attr)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_owproxy_mock_devices(
|
def setup_owproxy_mock_devices(
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -8,15 +8,13 @@ from homeassistant.components.onewire.const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
INPUT_ENTRY_CLEAR_OPTIONS,
|
INPUT_ENTRY_CLEAR_OPTIONS,
|
||||||
INPUT_ENTRY_DEVICE_SELECTION,
|
INPUT_ENTRY_DEVICE_SELECTION,
|
||||||
|
MANUFACTURER_MAXIM,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
|
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.config_validation import ensure_list
|
|
||||||
|
|
||||||
from .const import MOCK_OWPROXY_DEVICES
|
|
||||||
|
|
||||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||||
|
|
||||||
@ -26,15 +24,13 @@ async def filled_device_registry(
|
|||||||
hass: HomeAssistant, config_entry: ConfigEntry, device_registry: dr.DeviceRegistry
|
hass: HomeAssistant, config_entry: ConfigEntry, device_registry: dr.DeviceRegistry
|
||||||
) -> dr.DeviceRegistry:
|
) -> dr.DeviceRegistry:
|
||||||
"""Fill device registry with mock devices."""
|
"""Fill device registry with mock devices."""
|
||||||
for device_details in MOCK_OWPROXY_DEVICES.values():
|
for key in ("28.111111111111", "28.222222222222", "28.222222222223"):
|
||||||
if infos := device_details.get("device_info"):
|
|
||||||
for info in ensure_list(infos):
|
|
||||||
device_registry.async_get_or_create(
|
device_registry.async_get_or_create(
|
||||||
config_entry_id=config_entry.entry_id,
|
config_entry_id=config_entry.entry_id,
|
||||||
identifiers=info["identifiers"],
|
identifiers={(DOMAIN, key)},
|
||||||
manufacturer=info["manufacturer"],
|
manufacturer=MANUFACTURER_MAXIM,
|
||||||
model=info["model"],
|
model="DS18B20",
|
||||||
name=info["name"],
|
name=key,
|
||||||
)
|
)
|
||||||
return device_registry
|
return device_registry
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user