2022-05-09 11:16:23 +00:00
|
|
|
"""Tests for 1-Wire sensors."""
|
2023-01-03 16:32:39 +00:00
|
|
|
from collections.abc import Generator
|
2023-02-03 14:11:54 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from unittest.mock import MagicMock, _patch_dict, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2023-02-03 14:11:54 +00:00
|
|
|
from pyownet.protocol import OwnetError
|
2020-12-07 01:09:32 +00:00
|
|
|
import pytest
|
2023-04-22 15:22:05 +00:00
|
|
|
from syrupy.assertion import SnapshotAssertion
|
2020-12-07 01:09:32 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-03 14:11:54 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-02-09 09:43:45 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
2020-10-14 08:19:12 +00:00
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
from . import setup_owproxy_mock_devices
|
|
|
|
from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES
|
2020-12-07 01:09:32 +00:00
|
|
|
|
2020-10-14 08:19:12 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2023-01-03 16:32:39 +00:00
|
|
|
def override_platforms() -> Generator[None, None, None]:
|
2021-10-18 17:16:53 +00:00
|
|
|
"""Override PLATFORMS."""
|
2021-12-03 17:25:22 +00:00
|
|
|
with patch("homeassistant.components.onewire.PLATFORMS", [Platform.SENSOR]):
|
2021-10-18 17:16:53 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
async def test_sensors(
|
2021-11-09 17:30:05 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
owproxy: MagicMock,
|
|
|
|
device_id: str,
|
2023-02-09 09:43:45 +00:00
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
entity_registry: er.EntityRegistry,
|
2023-04-22 15:22:05 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2023-01-03 16:32:39 +00:00
|
|
|
) -> None:
|
2023-04-22 15:22:05 +00:00
|
|
|
"""Test for 1-Wire sensors."""
|
2021-12-03 17:25:22 +00:00
|
|
|
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
2023-04-22 15:22:05 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Ensure devices are correctly registered
|
|
|
|
device_entries = dr.async_entries_for_config_entry(
|
|
|
|
device_registry, config_entry.entry_id
|
|
|
|
)
|
|
|
|
assert device_entries == snapshot
|
2021-04-01 13:06:47 +00:00
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
# Ensure entities are correctly registered
|
|
|
|
entity_entries = er.async_entries_for_config_entry(
|
|
|
|
entity_registry, config_entry.entry_id
|
|
|
|
)
|
|
|
|
assert entity_entries == snapshot
|
2021-10-19 07:10:26 +00:00
|
|
|
|
2021-12-03 17:25:22 +00:00
|
|
|
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
2023-04-22 15:22:05 +00:00
|
|
|
# Some entities are disabled, enable them and reload before checking states
|
|
|
|
for ent in entity_entries:
|
|
|
|
entity_registry.async_update_entity(ent.entity_id, **{"disabled_by": None})
|
2021-10-19 07:10:26 +00:00
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
# Ensure entity states are correct
|
|
|
|
states = [hass.states.get(ent.entity_id) for ent in entity_entries]
|
|
|
|
assert states == snapshot
|
2023-02-03 14:11:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("device_id", ["12.111111111111"])
|
|
|
|
async def test_tai8570_sensors(
|
2023-02-09 09:43:45 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
owproxy: MagicMock,
|
|
|
|
device_id: str,
|
|
|
|
entity_registry: er.EntityRegistry,
|
2023-02-03 14:11:54 +00:00
|
|
|
) -> None:
|
|
|
|
"""The DS2602 is often used without TAI8570.
|
|
|
|
|
|
|
|
The sensors should be ignored.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_devices = deepcopy(MOCK_OWPROXY_DEVICES)
|
|
|
|
mock_device = mock_devices[device_id]
|
|
|
|
mock_device[ATTR_INJECT_READS].append(OwnetError)
|
|
|
|
mock_device[ATTR_INJECT_READS].append(OwnetError)
|
|
|
|
|
|
|
|
with _patch_dict(MOCK_OWPROXY_DEVICES, mock_devices):
|
|
|
|
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
|
|
|
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
expected_entities = mock_device[Platform.SENSOR]
|
|
|
|
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 None
|