"""
Discover all DS18B20 sensors visible to Linux.
"""

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


def main() -> None:
    """Print detected DS18B20 IDs and current temperatures."""
    devices = discover_ds18b20()

    if not devices:
        print("No DS18B20 detected.")
        print(
            "Check GPIO4 wiring and enable the Raspberry Pi "
            "1-Wire interface."
        )
        return

    print(
        f"Detected {len(devices)} DS18B20 sensor(s):"
    )

    for index, device_id in enumerate(
        devices,
        start=1,
    ):
        sensor = DS18B20TemperatureSensor(
            device_id=device_id,
        )

        try:
            temperature = (
                sensor.read_temperature_c()
            )
            value = f"{temperature:.3f} °C"
        except Exception as error:
            value = f"ERROR: {error}"

        print(
            f"  AUTO_{index} = "
            f"{device_id} -> {value}"
        )


if __name__ == "__main__":
    main()
