|
| 1 | +"""Zone runtime context exposed through the override sensor attributes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from types import SimpleNamespace |
| 6 | +import unittest |
| 7 | + |
| 8 | +from .helpers import ( |
| 9 | + NOW, |
| 10 | + FakeClimateManager, |
| 11 | + FakeHass, |
| 12 | + VelairScheduler, |
| 13 | + empty_week_schedule, |
| 14 | + normalize_schedule_data, |
| 15 | +) |
| 16 | +from .test_sensor_entities import sensor_module |
| 17 | + |
| 18 | + |
| 19 | +class ZoneContextTest(unittest.IsolatedAsyncioTestCase): |
| 20 | + def setUp(self) -> None: |
| 21 | + self.entity_id = "climate.salon" |
| 22 | + self.hass = FakeHass() |
| 23 | + self.hass.states[self.entity_id] = SimpleNamespace( |
| 24 | + state="cool", |
| 25 | + attributes={"temperature": 24, "current_temperature": 26}, |
| 26 | + ) |
| 27 | + self.data = normalize_schedule_data( |
| 28 | + { |
| 29 | + "zones": { |
| 30 | + self.entity_id: { |
| 31 | + "enabled": True, |
| 32 | + "schedule": empty_week_schedule(), |
| 33 | + } |
| 34 | + } |
| 35 | + }, |
| 36 | + [self.entity_id], |
| 37 | + ) |
| 38 | + self.data["zones"][self.entity_id]["schedule"]["tuesday"] = [ |
| 39 | + { |
| 40 | + "start": "00:00", |
| 41 | + "action": "set_temperature", |
| 42 | + "temperature": 24.0, |
| 43 | + "hvac_mode": "cool", |
| 44 | + } |
| 45 | + ] |
| 46 | + self.scheduler = VelairScheduler( |
| 47 | + self.hass, self.data, FakeClimateManager(), self._save |
| 48 | + ) |
| 49 | + self.scheduler._climate_manager.current_hvac_modes[self.entity_id] = "cool" |
| 50 | + |
| 51 | + async def _save(self) -> None: |
| 52 | + return None |
| 53 | + |
| 54 | + def test_context_before_any_delivery_is_compact(self) -> None: |
| 55 | + context = self.scheduler.get_zone_context(self.entity_id) |
| 56 | + self.assertEqual("default", context["schedule_source"]) |
| 57 | + self.assertEqual("automatic", context["control_mode"]) |
| 58 | + self.assertIn(context["runtime_state"], ("scheduled", "idle")) |
| 59 | + self.assertNotIn("last_applied_source", context) |
| 60 | + self.assertNotIn("manual_source", context) |
| 61 | + self.assertEqual({}, self.scheduler.get_zone_context("climate.unknown")) |
| 62 | + |
| 63 | + async def test_last_applied_target_is_recorded_with_its_source(self) -> None: |
| 64 | + await self.scheduler.async_apply_current_schedule(self.entity_id) |
| 65 | + context = self.scheduler.get_zone_context(self.entity_id) |
| 66 | + self.assertEqual("current_schedule", context["last_applied_source"]) |
| 67 | + self.assertEqual(24.0, context["last_applied_temperature"]) |
| 68 | + self.assertEqual("set_temperature", context["last_applied_action"]) |
| 69 | + self.assertEqual("cool", context["last_applied_hvac_mode"]) |
| 70 | + self.assertTrue(str(context["last_applied_at"]).startswith(str(NOW.year))) |
| 71 | + |
| 72 | + async def test_manual_adjustment_context_is_exposed(self) -> None: |
| 73 | + await self.scheduler.async_update_external_change_policy( |
| 74 | + self.entity_id, {"action": "until_resumed"} |
| 75 | + ) |
| 76 | + await self.scheduler.async_handle_external_climate_change( |
| 77 | + self.entity_id, |
| 78 | + changed_fields=["temperature"], |
| 79 | + previous={"temperature": 24.0}, |
| 80 | + current={"temperature": 20.0}, |
| 81 | + ) |
| 82 | + context = self.scheduler.get_zone_context(self.entity_id) |
| 83 | + self.assertEqual("manual", context["control_mode"]) |
| 84 | + self.assertEqual("external_change", context["manual_source"]) |
| 85 | + self.assertEqual("until_resumed", context["manual_policy"]) |
| 86 | + self.assertEqual(["temperature"], context["manual_changed_fields"]) |
| 87 | + self.assertIn("manual_since", context) |
| 88 | + self.assertNotIn("manual_until", context) |
| 89 | + |
| 90 | + await self.scheduler.async_resume_automatic_control(self.entity_id) |
| 91 | + context = self.scheduler.get_zone_context(self.entity_id) |
| 92 | + self.assertEqual("automatic", context["control_mode"]) |
| 93 | + self.assertNotIn("manual_source", context) |
| 94 | + |
| 95 | + def test_profile_context_is_exposed(self) -> None: |
| 96 | + data = normalize_schedule_data( |
| 97 | + { |
| 98 | + "zones": { |
| 99 | + self.entity_id: { |
| 100 | + "enabled": True, |
| 101 | + "schedule": empty_week_schedule(), |
| 102 | + } |
| 103 | + }, |
| 104 | + "profiles": [ |
| 105 | + { |
| 106 | + "key": "away", |
| 107 | + "name": "Away", |
| 108 | + "zones": { |
| 109 | + self.entity_id: { |
| 110 | + "behavior": "schedule", |
| 111 | + "schedule": empty_week_schedule(), |
| 112 | + } |
| 113 | + }, |
| 114 | + } |
| 115 | + ], |
| 116 | + "global_": {"mode": "auto", "active_profile_ids": ["away"]}, |
| 117 | + }, |
| 118 | + [self.entity_id], |
| 119 | + ) |
| 120 | + scheduler = VelairScheduler(self.hass, data, FakeClimateManager(), self._save) |
| 121 | + context = scheduler.get_zone_context(self.entity_id) |
| 122 | + self.assertEqual("away", context["effective_profile_id"]) |
| 123 | + self.assertEqual("Away", context["effective_profile_name"]) |
| 124 | + self.assertEqual("profile", context["schedule_source"]) |
| 125 | + |
| 126 | + def test_override_sensor_merges_context_attributes(self) -> None: |
| 127 | + scheduler = SimpleNamespace( |
| 128 | + get_zone_override_status=lambda entity_id: {"state": "none"}, |
| 129 | + get_zone_context=lambda entity_id: { |
| 130 | + "runtime_state": "scheduled", |
| 131 | + "control_mode": "automatic", |
| 132 | + "schedule_source": "default", |
| 133 | + "last_applied_source": "scheduled_event", |
| 134 | + "last_applied_temperature": 24.0, |
| 135 | + "ignored_key": "dropped", |
| 136 | + }, |
| 137 | + ) |
| 138 | + entry = SimpleNamespace( |
| 139 | + runtime_data=SimpleNamespace( |
| 140 | + scheduler=scheduler, |
| 141 | + temperature_unit="°C", |
| 142 | + configured_entities=["climate.living_room"], |
| 143 | + ), |
| 144 | + entry_id="entry", |
| 145 | + options={}, |
| 146 | + data={}, |
| 147 | + ) |
| 148 | + sensor = sensor_module.ZoneOverrideStateSensor(entry, "climate.living_room") |
| 149 | + attributes = sensor.extra_state_attributes |
| 150 | + self.assertEqual("scheduled", attributes["runtime_state"]) |
| 151 | + self.assertEqual("automatic", attributes["control_mode"]) |
| 152 | + self.assertEqual(24.0, attributes["last_applied_temperature"]) |
| 153 | + self.assertNotIn("ignored_key", attributes) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + unittest.main() |
0 commit comments