2022-05-09 11:16:23 +00:00
|
|
|
"""Tests for 1-Wire sensors."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2024-07-01 10:09:11 +00:00
|
|
|
from collections.abc import Generator
|
2023-02-03 14:11:54 +00:00
|
|
|
from copy import deepcopy
|
2023-04-27 11:33:30 +00:00
|
|
|
import logging
|
2023-02-03 14:11:54 +00:00
|
|
|
from unittest.mock import MagicMock, _patch_dict, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2025-01-10 10:53:31 +00:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
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
|
|
|
|
2025-01-10 10:53:31 +00:00
|
|
|
from homeassistant.components.onewire.onewirehub import _DEVICE_SCAN_INTERVAL
|
2023-04-27 11:33:30 +00:00
|
|
|
from homeassistant.const import Platform
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2025-01-09 10:45:29 +00:00
|
|
|
from homeassistant.helpers import 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
|
|
|
|
2025-01-10 10:53:31 +00:00
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
2025-01-08 08:33:04 +00:00
|
|
|
|
2020-10-14 08:19:12 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-06 15:41:37 +00:00
|
|
|
def override_platforms() -> Generator[None]:
|
2021-10-18 17:16:53 +00:00
|
|
|
"""Override PLATFORMS."""
|
2025-01-09 10:22:08 +00:00
|
|
|
with patch("homeassistant.components.onewire._PLATFORMS", [Platform.SENSOR]):
|
2021-10-18 17:16:53 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2025-01-09 10:45:29 +00:00
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
2022-05-09 11:16:23 +00:00
|
|
|
async def test_sensors(
|
2021-11-09 17:30:05 +00:00
|
|
|
hass: HomeAssistant,
|
2025-01-08 08:33:04 +00:00
|
|
|
config_entry: MockConfigEntry,
|
2021-11-09 17:30:05 +00:00
|
|
|
owproxy: MagicMock,
|
2023-02-09 09:43:45 +00:00
|
|
|
entity_registry: er.EntityRegistry,
|
2023-04-22 15:22:05 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2023-01-03 16:32:39 +00:00
|
|
|
) -> None:
|
2025-01-10 10:53:31 +00:00
|
|
|
"""Test for 1-Wire sensor entities."""
|
2025-01-09 10:45:29 +00:00
|
|
|
setup_owproxy_mock_devices(owproxy, MOCK_OWPROXY_DEVICES.keys())
|
2023-04-22 15:22:05 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2021-10-19 07:10:26 +00:00
|
|
|
|
2025-01-09 10:45:29 +00:00
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
2023-02-03 14:11:54 +00:00
|
|
|
|
|
|
|
|
2025-01-10 10:53:31 +00:00
|
|
|
@pytest.mark.parametrize("device_id", ["12.111111111111"])
|
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
|
|
|
async def test_sensors_delayed(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: MockConfigEntry,
|
|
|
|
owproxy: MagicMock,
|
|
|
|
device_id: str,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
) -> None:
|
|
|
|
"""Test for delayed 1-Wire sensor entities."""
|
|
|
|
setup_owproxy_mock_devices(owproxy, [])
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
|
|
|
|
assert not er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
|
|
|
|
|
|
|
|
setup_owproxy_mock_devices(owproxy, [device_id])
|
|
|
|
freezer.tick(_DEVICE_SCAN_INTERVAL)
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
await hass.async_block_till_done(wait_background_tasks=True)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
|
|
|
|
== 2
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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,
|
2025-01-08 08:33:04 +00:00
|
|
|
config_entry: MockConfigEntry,
|
2023-02-09 09:43:45 +00:00
|
|
|
owproxy: MagicMock,
|
|
|
|
device_id: str,
|
|
|
|
entity_registry: er.EntityRegistry,
|
2023-04-27 11:33:30 +00:00
|
|
|
caplog: pytest.LogCaptureFixture,
|
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]
|
2025-01-08 14:14:51 +00:00
|
|
|
mock_device[ATTR_INJECT_READS]["/TAI8570/temperature"] = [OwnetError]
|
|
|
|
mock_device[ATTR_INJECT_READS]["/TAI8570/pressure"] = [OwnetError]
|
2023-02-03 14:11:54 +00:00
|
|
|
|
|
|
|
with _patch_dict(MOCK_OWPROXY_DEVICES, mock_devices):
|
2025-01-08 14:14:51 +00:00
|
|
|
setup_owproxy_mock_devices(owproxy, [device_id])
|
2023-02-03 14:11:54 +00:00
|
|
|
|
2023-04-27 11:33:30 +00:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert entity_registry.entities.get("sensor.12_111111111111_temperature") is None
|
|
|
|
assert "unreachable sensor /12.111111111111/TAI8570/temperature" in caplog.text
|
2023-02-03 14:11:54 +00:00
|
|
|
|
2023-04-27 11:33:30 +00:00
|
|
|
assert entity_registry.entities.get("sensor.12_111111111111_pressure") is None
|
|
|
|
assert "unreachable sensor /12.111111111111/TAI8570/pressure" in caplog.text
|