11from unittest import TestCase
2+
3+ import codecs
4+ from datetime import datetime
5+
26from eq3bt import Thermostat , TemperatureException
7+ from eq3bt .eq3btsmart import (PROP_NTFY_HANDLE , PROP_ID_QUERY ,
8+ PROP_INFO_QUERY , Mode )
9+
10+
11+ ID_RESPONSE = b'01780000807581626163606067659e'
12+ STATUS_RESPONSES = {
13+ 'auto' : b'020100000428' ,
14+ 'manual' : b'020101000428' ,
15+ 'window' : b'020110000428' ,
16+ 'away' : b'0201020004231d132e03' ,
17+ 'boost' : b'020104000428' ,
18+ 'low_batt' : b'020180000428' ,
19+ 'valve_at_22' : b'020100160428' ,
20+ }
21+
322
423class FakeConnection :
524 def __init__ (self , mac ):
6- pass
25+ self ._callbacks = {}
26+ self ._res = 'auto'
727
828 def set_callback (self , handle , cb ):
9- pass
29+ self ._callbacks [handle ] = cb
30+
31+ def set_status (self , key ):
32+ if key in STATUS_RESPONSES :
33+ self ._res = key
34+ else :
35+ raise ValueError ("Invalid key for status test response." )
36+
37+ def make_request (self , handle , value , timeout = 1 , with_response = True ):
38+ """Write a GATT Command without callback - not utf-8."""
39+ if with_response :
40+ cb = self ._callbacks .get (PROP_NTFY_HANDLE )
41+
42+ if value [0 ] == PROP_ID_QUERY :
43+ data = ID_RESPONSE
44+ elif value [0 ] == PROP_INFO_QUERY :
45+ data = STATUS_RESPONSES [self ._res ]
46+ else :
47+ return
48+ cb (codecs .decode (data , 'hex' ))
1049
1150
1251class TestThermostat (TestCase ):
@@ -28,8 +67,38 @@ def test_parse_schedule(self):
2867 def test_handle_notification (self ):
2968 self .fail ()
3069
70+ def test_query_id (self ):
71+ self .thermostat .query_id ()
72+ self .assertEqual (self .thermostat .firmware_version , 120 )
73+ self .assertEqual (self .thermostat .device_serial , "PEQ2130075" )
74+
3175 def test_update (self ):
32- self .fail ()
76+ th = self .thermostat
77+
78+ th ._conn .set_status ('auto' )
79+ th .update ()
80+ self .assertEqual (th .valve_state , 0 )
81+ self .assertEqual (th .mode , Mode .Auto )
82+ self .assertEqual (th .target_temperature , 20.0 )
83+ self .assertFalse (th .locked )
84+ self .assertFalse (th .low_battery )
85+ self .assertFalse (th .boost )
86+ self .assertFalse (th .window_open )
87+
88+ th ._conn .set_status ('manual' )
89+ th .update ()
90+ self .assertTrue (th .mode , Mode .Manual )
91+
92+ th ._conn .set_status ('away' )
93+ th .update ()
94+ self .assertEqual (th .mode , Mode .Away )
95+ self .assertEqual (th .target_temperature , 17.5 )
96+ self .assertEqual (th .away_end , datetime (2019 , 3 , 29 , 23 , 00 ))
97+
98+ th ._conn .set_status ('boost' )
99+ th .update ()
100+ self .assertTrue (th .boost )
101+ self .assertEqual (th .mode , Mode .Boost )
33102
34103 def test_query_schedule (self ):
35104 self .fail ()
@@ -53,10 +122,16 @@ def test_boost(self):
53122 self .fail ()
54123
55124 def test_valve_state (self ):
56- self .fail ()
125+ th = self .thermostat
126+ th ._conn .set_status ('valve_at_22' )
127+ th .update ()
128+ self .assertEqual (th .valve_state , 22 )
57129
58130 def test_window_open (self ):
59- self .fail ()
131+ th = self .thermostat
132+ th ._conn .set_status ('window' )
133+ th .update ()
134+ self .assertTrue (th .window_open )
60135
61136 def test_window_open_config (self ):
62137 self .fail ()
@@ -65,7 +140,10 @@ def test_locked(self):
65140 self .fail ()
66141
67142 def test_low_battery (self ):
68- self .fail ()
143+ th = self .thermostat
144+ th ._conn .set_status ('low_batt' )
145+ th .update ()
146+ self .assertTrue (th .low_battery )
69147
70148 def test_temperature_offset (self ):
71149 self .fail ()
0 commit comments