"""
Tests for the DS18B20 Sentinel driver.
"""

from pathlib import Path

from sentinel.sensors.ds18b20 import (
    DS18B20TemperatureSensor,
    discover_ds18b20,
)


def test_ds18b20_parses_temperature(
    tmp_path: Path,
) -> None:
    device_id = "28-000000000001"
    sensor_dir = tmp_path / device_id
    sensor_dir.mkdir()

    (sensor_dir / "w1_slave").write_text(
        "aa bb cc dd ee ff gg hh ii : crc=ii YES\n"
        "aa bb cc dd ee ff gg hh ii t=23125\n",
        encoding="utf-8",
    )

    sensor = DS18B20TemperatureSensor(
        device_id=device_id,
        w1_root=tmp_path,
    )

    assert sensor.read_temperature_c() == 23.125


def test_discover_ds18b20(
    tmp_path: Path,
) -> None:
    for device_id in (
        "28-000000000002",
        "28-000000000001",
    ):
        sensor_dir = tmp_path / device_id
        sensor_dir.mkdir()
        (sensor_dir / "w1_slave").write_text(
            "test",
            encoding="utf-8",
        )

    assert discover_ds18b20(
        w1_root=tmp_path,
    ) == [
        "28-000000000001",
        "28-000000000002",
    ]
