@@ -82,14 +82,23 @@ class ThinkEcoAPI {
8282 async auth ( ) {
8383 if ( Date . now ( ) - this . lastLogin > LOGIN_FREQUENCY ) {
8484 this . log ( 'api' , 'logging in...' ) ;
85- await this . session . post (
86- { uri : 'https://mymodlet.com/Account/Login' ,
87- form : { 'loginForm.Email' : this . username ,
88- 'loginForm.Password' : this . password ,
89- 'loginForm.RememberMe' : 'True' ,
90- 'ReturnUrl' : '/smartac' } ,
91- followRedirect : false ,
92- simple : false } ) ;
85+
86+ await this . session . post ( {
87+ uri : 'https://web.mymodlet.com/Account/Login' ,
88+ body : {
89+ // ThinkEco uses a strangely formatted payload:
90+ // stringified JSON inside a 'data' JSON object.
91+ data : JSON . stringify ( {
92+ 'Email' : this . username ,
93+ 'Password' : this . password ,
94+ } ) ,
95+ } ,
96+ followRedirect : false ,
97+ simple : false ,
98+ json : true ,
99+ jar : true ,
100+ } ) ;
101+
93102 this . lastLogin = Date . now ( ) ;
94103 }
95104 }
@@ -107,24 +116,29 @@ class ThinkEcoAPI {
107116 this . log ( 'api' , 'updating thermostat status...' ) ;
108117 await this . auth ( ) ;
109118 const statusTxt =
110- await this . session . post ( 'https://mymodlet.com/SmartAC/UserSettingsTable' ) ;
119+ await this . session . get ( 'https://web.mymodlet.com/Devices/UpdateData' ) ;
120+
121+ // statusTxt is a quoted string of JSON, e.g. "{\"SmartACs\": ... }"
122+ let status = JSON . parse ( JSON . parse ( statusTxt ) ) ;
123+ status . SmartACs . forEach ( ac => {
124+ // Find the corresponding device for name.
125+ const modletId = ac . modlet . modletId ;
126+ const device = status . Devices . find ( item => item . modletId == modletId ) ;
127+ const id = device . deviceId ;
111128
112- // status is a quoted blob of HTML...
113- const status = JSON . parse ( '{"response":' + statusTxt + '}' ) ;
114- const $ = cheerio . load ( status . response ) ;
115- $ ( '#appName' ) . has ( '.drSetTemp' ) . each ( ( i , e ) => {
116- const id = $ ( '.drSetTemp' , e ) . attr ( 'id' ) ;
117129 let thermostat = this . thermostats . get ( id ) ;
118130 if ( ! thermostat ) {
119131 thermostat = new Thermostat ( this , id ) ;
120132 this . thermostats . set ( id , thermostat ) ;
121133 }
122- thermostat . name = $ ( e ) . children ( ) . first ( ) . text ( ) ;
123- thermostat . targetTemp = parseInt ( $ ( 'option:checked' , $ ( e ) . parent ( ) ) . val ( ) ) ;
124- thermostat . currentTemp = parseInt ( $ ( '#currentTemperature' , $ ( e ) . parent ( ) ) . text ( ) ) ;
125- thermostat . powerOn = $ ( '#deviceAction' , $ ( e ) . parent ( ) ) . children ( 'a' ) . is ( '.Off' ) ;
126- thermostat . broken = $ ( '#deviceAction' , $ ( e ) . parent ( ) ) . children ( 'button' ) . is ( ':disabled' ) ;
134+
135+ thermostat . name = device . deviceName ;
136+ thermostat . targetTemp = ac . thermostat . targetTemperature ;
137+ thermostat . currentTemp = ac . thermostat . currentTemperature ;
138+ thermostat . powerOn = ac . modlet . isOn ;
139+ thermostat . broken = ! ( ac . modlet . hasThermostat && ac . thermostat . thermostatIsOTA ) ;
127140 } ) ;
141+
128142 this . lastUpdate = Date . now ( ) ;
129143 }
130144 this . lock . release ( ) ;
@@ -135,13 +149,22 @@ class ThinkEcoAPI {
135149 // returns a boolean indicating if mymodlet.com told us if it was successful
136150 async pushUpdate ( thermostat ) {
137151 await this . auth ( ) ;
138- const r = await this . session . post (
139- { uri : 'https://mymodlet.com/SmartAC/UserSettings' ,
140- body : { 'applianceId' : thermostat . id ,
141- 'targetTemperature' : '' + thermostat . targetTemp ,
142- 'thermostated' : thermostat . powerOn } ,
143- json : true } ) ;
144- return r . Success ;
152+ const r = await this . session . post ( {
153+ uri : 'https://web.mymodlet.com/Devices/UserSettingsUpdate' ,
154+ body : {
155+ data : JSON . stringify ( {
156+ 'DeviceId' : thermostat . id ,
157+ 'TargetTemperature' : String ( thermostat . targetTemp ) ,
158+ 'IsThermostated' : thermostat . powerOn
159+ } ) ,
160+ } ,
161+ json : true
162+ } ) ;
163+
164+ // Returned as a quoted string of JSON, so need to decode again...
165+ const parsedResponse = JSON . parse ( r ) ;
166+
167+ return parsedResponse . data . status . IsError === false ;
145168 }
146169}
147170
0 commit comments