| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- """Unit tests for sensor property logic.
- Bypasses HA entity setup by using object.__new__ and injecting a mock
- coordinator — valid because every tested property only reads self.coordinator.data.
- """
- from datetime import date, datetime, time, timezone
- from unittest.mock import MagicMock
- import pytest
- from custom_components.room_cal_ha.coordinator import CoordinatorData
- from custom_components.room_cal_ha.models import ApiStatusData, ReservationData
- from custom_components.room_cal_ha.sensor import (
- ApiHealthSensor,
- GuestNameSensor,
- LastRefreshSensor,
- ReservationDateTimeSensor,
- ReservationStrSensor,
- ReservationTimeSensor,
- WiFiKeySensor,
- )
- from custom_components.room_cal_ha.const import STORE_CURRENT, STORE_NEXT
- # ---------------------------------------------------------------------------
- # Helpers
- # ---------------------------------------------------------------------------
- def _coordinator(
- current=None,
- nxt=None,
- current_ts=None,
- next_ts=None,
- api_status=None,
- ):
- coord = MagicMock()
- coord.data = CoordinatorData(
- current=current or ReservationData(),
- next=nxt or ReservationData(),
- current_last_refresh=current_ts,
- next_last_refresh=next_ts,
- api_status=api_status,
- )
- return coord
- def _sensor(cls, coordinator, slot=STORE_CURRENT, field="guest_name", **extra):
- """Create a sensor instance without running __init__."""
- s = object.__new__(cls)
- s.coordinator = coordinator
- s._slot = slot
- s._field = field
- for k, v in extra.items():
- setattr(s, k, v)
- return s
- SAMPLE = ReservationData(
- guest_name="Alice",
- check_in_date=date(2026, 7, 1),
- check_in_time=time(14, 0),
- check_out_date=date(2026, 7, 5),
- check_out_time=time(12, 0),
- wifi_ssid="MyNet",
- wifi_key="hunter2",
- door_code="0472",
- )
- # ---------------------------------------------------------------------------
- # ReservationStrSensor
- # ---------------------------------------------------------------------------
- def test_str_sensor_returns_field_value():
- coord = _coordinator(current=SAMPLE)
- s = _sensor(ReservationStrSensor, coord, field="guest_name")
- assert s.native_value == "Alice"
- def test_str_sensor_reads_next_slot():
- nxt = ReservationData(guest_name="Bob")
- coord = _coordinator(nxt=nxt)
- s = _sensor(ReservationStrSensor, coord, slot=STORE_NEXT, field="guest_name")
- assert s.native_value == "Bob"
- def test_str_sensor_none_when_field_is_none():
- res = ReservationData(guest_name=None)
- coord = _coordinator(current=res)
- s = _sensor(ReservationStrSensor, coord, field="guest_name")
- assert s.native_value is None
- # ---------------------------------------------------------------------------
- # GuestNameSensor
- # ---------------------------------------------------------------------------
- def test_guest_name_single_word():
- coord = _coordinator(current=ReservationData(guest_name="Alice"))
- s = _sensor(GuestNameSensor, coord, field="guest_name")
- attrs = s.extra_state_attributes
- assert attrs["first_name"] == "Alice"
- assert attrs["first_abbr"] == "A"
- assert attrs["last_name"] is None
- assert attrs["last_abbr"] is None
- assert attrs["public"] == "Alice"
- def test_guest_name_two_words():
- coord = _coordinator(current=ReservationData(guest_name="ZiB Guest"))
- s = _sensor(GuestNameSensor, coord, field="guest_name")
- attrs = s.extra_state_attributes
- assert attrs["first_name"] == "ZiB"
- assert attrs["first_abbr"] == "Z"
- assert attrs["last_name"] == "Guest"
- assert attrs["last_abbr"] == "G"
- assert attrs["public"] == "ZiB G."
- def test_guest_name_three_words_last_is_final():
- coord = _coordinator(current=ReservationData(guest_name="ZiB Middle Guest"))
- s = _sensor(GuestNameSensor, coord, field="guest_name")
- attrs = s.extra_state_attributes
- assert attrs["first_name"] == "ZiB Middle"
- assert attrs["last_name"] == "Guest"
- assert attrs["last_abbr"] == "G"
- assert attrs["public"] == "ZiB Middle G."
- def test_guest_name_none_returns_no_attrs():
- coord = _coordinator(current=ReservationData(guest_name=None))
- s = _sensor(GuestNameSensor, coord, field="guest_name")
- assert s.extra_state_attributes is None
- # ---------------------------------------------------------------------------
- # ReservationTimeSensor
- # ---------------------------------------------------------------------------
- def test_time_sensor_returns_iso_string():
- coord = _coordinator(current=SAMPLE)
- s = _sensor(ReservationTimeSensor, coord, field="check_in_time")
- assert s.native_value == "14:00:00"
- def test_time_sensor_none_when_time_is_none():
- res = ReservationData(check_in_time=None)
- coord = _coordinator(current=res)
- s = _sensor(ReservationTimeSensor, coord, field="check_in_time")
- assert s.native_value is None
- # ---------------------------------------------------------------------------
- # ReservationDateTimeSensor
- # ---------------------------------------------------------------------------
- def test_datetime_sensor_native_value_is_date():
- coord = _coordinator(current=SAMPLE)
- s = _sensor(ReservationDateTimeSensor, coord, field="check_in_date")
- s._time_field = "check_in_time"
- assert s.native_value == date(2026, 7, 1)
- def test_datetime_sensor_attrs_contain_expected_keys():
- coord = _coordinator(current=SAMPLE)
- s = _sensor(ReservationDateTimeSensor, coord, field="check_in_date")
- s._time_field = "check_in_time"
- attrs = s.extra_state_attributes
- assert attrs is not None
- for key in ("datetime", "date", "weekday", "month", "time_abbr", "hour"):
- assert key in attrs, f"missing key: {key}"
- def test_datetime_sensor_attrs_none_when_date_missing():
- res = ReservationData(check_in_date=None, check_in_time=time(14, 0))
- coord = _coordinator(current=res)
- s = _sensor(ReservationDateTimeSensor, coord, field="check_in_date")
- s._time_field = "check_in_time"
- assert s.extra_state_attributes is None
- def test_datetime_sensor_attrs_none_when_time_missing():
- res = ReservationData(check_in_date=date(2026, 7, 1), check_in_time=None)
- coord = _coordinator(current=res)
- s = _sensor(ReservationDateTimeSensor, coord, field="check_in_date")
- s._time_field = "check_in_time"
- assert s.extra_state_attributes is None
- # ---------------------------------------------------------------------------
- # WiFiKeySensor
- # ---------------------------------------------------------------------------
- def test_wifi_key_sensor_native_value():
- coord = _coordinator(current=SAMPLE)
- s = _sensor(WiFiKeySensor, coord, field="wifi_key")
- assert s.native_value == "hunter2"
- def test_wifi_key_sensor_qr_attribute():
- coord = _coordinator(current=SAMPLE)
- s = _sensor(WiFiKeySensor, coord, field="wifi_key")
- attrs = s.extra_state_attributes
- assert attrs is not None
- assert attrs["qr"] == "WIFI:S:MyNet;T:WPA;P:hunter2;;"
- def test_wifi_key_sensor_no_attrs_when_ssid_missing():
- res = ReservationData(wifi_ssid=None, wifi_key="hunter2")
- coord = _coordinator(current=res)
- s = _sensor(WiFiKeySensor, coord, field="wifi_key")
- assert s.extra_state_attributes is None
- def test_wifi_key_sensor_no_attrs_when_key_missing():
- res = ReservationData(wifi_ssid="MyNet", wifi_key=None)
- coord = _coordinator(current=res)
- s = _sensor(WiFiKeySensor, coord, field="wifi_key")
- assert s.extra_state_attributes is None
- # ---------------------------------------------------------------------------
- # LastRefreshSensor
- # ---------------------------------------------------------------------------
- def test_last_refresh_sensor_current():
- ts = datetime(2026, 6, 22, 10, 0, tzinfo=timezone.utc)
- coord = _coordinator(current_ts=ts)
- s = _sensor(LastRefreshSensor, coord)
- s._slot = STORE_CURRENT
- assert s.native_value == ts
- def test_last_refresh_sensor_next():
- ts = datetime(2026, 6, 22, 12, 0, tzinfo=timezone.utc)
- coord = _coordinator(next_ts=ts)
- s = _sensor(LastRefreshSensor, coord)
- s._slot = STORE_NEXT
- assert s.native_value == ts
- def test_last_refresh_sensor_none_before_any_fetch():
- coord = _coordinator()
- s = _sensor(LastRefreshSensor, coord)
- s._slot = STORE_CURRENT
- assert s.native_value is None
- # ---------------------------------------------------------------------------
- # ApiHealthSensor
- # ---------------------------------------------------------------------------
- def _api_sensor(coordinator):
- s = object.__new__(ApiHealthSensor)
- s.coordinator = coordinator
- return s
- def test_api_health_ok():
- status = ApiStatusData(status="OK", services={"booking": "OK", "calendar": "OK"})
- coord = _coordinator(api_status=status)
- s = _api_sensor(coord)
- assert s.native_value == "OK"
- def test_api_health_degraded_on_warn():
- status = ApiStatusData(status="Warn", services={"booking": "Warn", "calendar": "OK"})
- coord = _coordinator(api_status=status)
- s = _api_sensor(coord)
- assert s.native_value == "Degraded"
- def test_api_health_none_when_no_status():
- coord = _coordinator(api_status=None)
- s = _api_sensor(coord)
- assert s.native_value is None
- def test_api_health_services_as_attributes():
- svc = {"booking": "OK", "calendar": "Warn"}
- status = ApiStatusData(status="Warn", services=svc)
- coord = _coordinator(api_status=status)
- s = _api_sensor(coord)
- assert s.extra_state_attributes == svc
- def test_api_health_no_attrs_when_services_empty():
- status = ApiStatusData(status="OK", services={})
- coord = _coordinator(api_status=status)
- s = _api_sensor(coord)
- assert s.extra_state_attributes is None
- def test_api_health_no_attrs_when_no_status():
- coord = _coordinator(api_status=None)
- s = _api_sensor(coord)
- assert s.extra_state_attributes is None
|