2121)
2222
2323
24- async def test_pressure_health_low_pressure (hass , coordinator , entry , domains , data , options , config ):
24+ def _get_pressure_entity_id (hass , entry ):
25+ registry = er .async_get (hass )
26+ return registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
27+
28+
29+ async def test_pressure_health_low_pressure (hass , coordinator , entry , domains , data , options , config , monkeypatch ):
30+ current_time = 0.0
31+
32+ def fake_timestamp ():
33+ return current_time
34+
35+ monkeypatch .setattr (sat_binary_sensor , "timestamp" , fake_timestamp )
36+
2537 await coordinator .async_set_boiler_pressure (0.6 )
2638 coordinator .async_update_listeners ()
2739 await hass .async_block_till_done ()
2840
29- registry = er .async_get (hass )
30- entity_id = registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
31- state = hass .states .get (entity_id )
41+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
3242 assert state is not None
43+ assert state .state == "off"
44+
45+ current_time = 130.0
46+ await coordinator .async_set_boiler_pressure (0.6 )
47+ coordinator .async_update_listeners ()
48+ await hass .async_block_till_done ()
49+
50+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
3351 assert state .state == "on"
3452
3553
@@ -38,9 +56,7 @@ async def test_pressure_health_normal_pressure(hass, coordinator, entry, domains
3856 coordinator .async_update_listeners ()
3957 await hass .async_block_till_done ()
4058
41- registry = er .async_get (hass )
42- entity_id = registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
43- state = hass .states .get (entity_id )
59+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
4460 assert state is not None
4561 assert state .state == "off"
4662
@@ -53,23 +69,14 @@ def fake_timestamp():
5369
5470 monkeypatch .setattr (sat_binary_sensor , "timestamp" , fake_timestamp )
5571
56- await coordinator .async_set_boiler_pressure (1.8 )
57- coordinator .async_update_listeners ()
58- await hass .async_block_till_done ()
59-
60- current_time = 1800.0
61- await coordinator .async_set_boiler_pressure (1.6 )
62- coordinator .async_update_listeners ()
63- await hass .async_block_till_done ()
64-
65- current_time = 3600.0
66- await coordinator .async_set_boiler_pressure (1.2 )
67- coordinator .async_update_listeners ()
68- await hass .async_block_till_done ()
72+ for i in range (60 ):
73+ current_time = float (i * 10 )
74+ pressure = 1.8 - (i * 0.01 )
75+ await coordinator .async_set_boiler_pressure (pressure )
76+ coordinator .async_update_listeners ()
77+ await hass .async_block_till_done ()
6978
70- registry = er .async_get (hass )
71- entity_id = registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
72- state = hass .states .get (entity_id )
79+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
7380 assert state is not None
7481 assert state .state == "on"
7582 assert state .attributes ["pressure_drop_rate_bar_per_hour" ] is not None
@@ -94,9 +101,7 @@ def fake_timestamp():
94101 coordinator .async_update_listeners ()
95102 await hass .async_block_till_done ()
96103
97- registry = er .async_get (hass )
98- entity_id = registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
99- state = hass .states .get (entity_id )
104+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
100105 assert state is not None
101106 assert state .state == "off"
102107 assert state .attributes ["pressure_drop_rate_bar_per_hour" ] is None
@@ -119,9 +124,7 @@ def fake_timestamp():
119124 coordinator .async_update_listeners ()
120125 await hass .async_block_till_done ()
121126
122- registry = er .async_get (hass )
123- entity_id = registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
124- state = hass .states .get (entity_id )
127+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
125128 assert state is not None
126129 assert state .state == "on"
127130
@@ -141,6 +144,7 @@ def fake_timestamp():
141144 "last_pressure" : 1.4 ,
142145 "last_pressure_timestamp" : 100.0 ,
143146 "last_seen_pressure_timestamp" : 100.0 ,
147+ "smoothed_pressure" : 1.4 ,
144148 }
145149
146150 async def fake_async_get_last_state (self ):
@@ -155,8 +159,95 @@ async def fake_async_get_last_state(self):
155159 coordinator .async_update_listeners ()
156160 await hass .async_block_till_done ()
157161
158- registry = er .async_get (hass )
159- entity_id = registry .async_get_entity_id ("binary_sensor" , "sat" , f"{ entry .entry_id } -pressure-health" )
160- state = hass .states .get (entity_id )
162+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
161163 assert state is not None
162164 assert state .attributes ["last_pressure" ] == 1.4
165+ assert state .attributes ["smoothed_pressure" ] is not None
166+
167+
168+ async def test_pressure_health_normal_oscillations_no_false_positive (hass , coordinator , entry , domains , data , options , config , monkeypatch ):
169+ current_time = 0.0
170+
171+ def fake_timestamp ():
172+ return current_time
173+
174+ monkeypatch .setattr (sat_binary_sensor , "timestamp" , fake_timestamp )
175+
176+ for i in range (60 ):
177+ current_time = float (i * 30 )
178+ pressure = 2.1 + (0.2 if i % 2 == 0 else - 0.2 )
179+ await coordinator .async_set_boiler_pressure (pressure )
180+ coordinator .async_update_listeners ()
181+ await hass .async_block_till_done ()
182+
183+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
184+ assert state .state == "off" , f"False positive at iteration { i } , pressure={ pressure } "
185+
186+
187+ async def test_pressure_health_confirmation_delay_resets (hass , coordinator , entry , domains , data , options , config , monkeypatch ):
188+ current_time = 0.0
189+
190+ def fake_timestamp ():
191+ return current_time
192+
193+ monkeypatch .setattr (sat_binary_sensor , "timestamp" , fake_timestamp )
194+
195+ await coordinator .async_set_boiler_pressure (0.6 )
196+ coordinator .async_update_listeners ()
197+ await hass .async_block_till_done ()
198+
199+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
200+ assert state .state == "off"
201+
202+ current_time = 100.0
203+ await coordinator .async_set_boiler_pressure (0.6 )
204+ coordinator .async_update_listeners ()
205+ await hass .async_block_till_done ()
206+
207+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
208+ assert state .state == "off"
209+
210+ current_time = 110.0
211+ await coordinator .async_set_boiler_pressure (1.5 )
212+ coordinator .async_update_listeners ()
213+ await hass .async_block_till_done ()
214+
215+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
216+ assert state .state == "off"
217+
218+ current_time = 120.0
219+ await coordinator .async_set_boiler_pressure (0.6 )
220+ coordinator .async_update_listeners ()
221+ await hass .async_block_till_done ()
222+
223+ current_time = 250.0
224+ await coordinator .async_set_boiler_pressure (0.6 )
225+ coordinator .async_update_listeners ()
226+ await hass .async_block_till_done ()
227+
228+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
229+ assert state .state == "on"
230+
231+
232+ async def test_pressure_health_high_pressure_with_delay (hass , coordinator , entry , domains , data , options , config , monkeypatch ):
233+ current_time = 0.0
234+
235+ def fake_timestamp ():
236+ return current_time
237+
238+ monkeypatch .setattr (sat_binary_sensor , "timestamp" , fake_timestamp )
239+
240+ await coordinator .async_set_boiler_pressure (3.0 )
241+ coordinator .async_update_listeners ()
242+ await hass .async_block_till_done ()
243+
244+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
245+ assert state .state == "off"
246+
247+ current_time = 130.0
248+ await coordinator .async_set_boiler_pressure (3.0 )
249+ coordinator .async_update_listeners ()
250+ await hass .async_block_till_done ()
251+
252+ state = hass .states .get (_get_pressure_entity_id (hass , entry ))
253+ assert state .state == "on"
0 commit comments