Class: Sensi::Thermostat

Inherits:
HashToObject show all
Defined in:
lib/sensi/thermostat.rb

Constant Summary

Constants inherited from HashToObject

HashToObject::TEST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HashToObject

#add, #add_var, #contains_hash, #convert

Constructor Details

#initialize(login, password, json = nil) ⇒ Thermostat

Returns a new instance of Thermostat.



23
24
25
26
# File 'lib/sensi/thermostat.rb', line 23

def initialize(, password, json = nil)
	@account = Sensi::Account.new(, password)
	@thermostat_connection = Sensi::ThermostatConnection.new(@account)
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



10
11
12
# File 'lib/sensi/thermostat.rb', line 10

def 
  @account
end

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/sensi/thermostat.rb', line 10

def response
  @response
end

#thermostat_connectionObject

Returns the value of attribute thermostat_connection.



10
11
12
# File 'lib/sensi/thermostat.rb', line 10

def thermostat_connection
  @thermostat_connection
end

Instance Method Details

#active_modeObject



112
113
114
# File 'lib/sensi/thermostat.rb', line 112

def active_mode
	self.m.a.operational_status.running.mode
end

#active_timeObject



116
117
118
119
120
# File 'lib/sensi/thermostat.rb', line 116

def active_time
	self.m.a.operational_status.running.time
rescue 
	"0"
end

#aux_on?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/sensi/thermostat.rb', line 180

def aux_on?
	system_mode == 'Aux'
end

#connectObject



28
29
30
31
32
# File 'lib/sensi/thermostat.rb', line 28

def connect
	@account.
	
	connect_to_device
end

#connect_to_device(connection_attempt = 3) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sensi/thermostat.rb', line 43

def connect_to_device(connection_attempt = 3)
	attempt = 1
	while not connected_to_device? and attempt < connection_attempt
		@thermostat_connection.connect
		@thermostat_connection.initialize_polling(self.icd)
		@response = @thermostat_connection.start_polling
		convert(@response)
		attempt += 1
	end

	connected_to_device?
end

#connected_to_device?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/sensi/thermostat.rb', line 56

def connected_to_device?
	self.m.respond_to?(:m)
rescue
	false
end

#cool_on?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/sensi/thermostat.rb', line 176

def cool_on?
	system_mode == 'Cool'
end

#disconnectObject



34
35
36
37
# File 'lib/sensi/thermostat.rb', line 34

def disconnect
	@thermostat_connection.stop_polling
	@account.logout
end

#equipmentObject



104
105
106
# File 'lib/sensi/thermostat.rb', line 104

def equipment
	self.m.a.capabilities.outdoor_equipment
end

#from_json!(string) ⇒ Object



303
304
305
306
307
# File 'lib/sensi/thermostat.rb', line 303

def from_json!(string)
    JSON.load(string).each do |var, val|
        self.instance_variable_set var, val
    end
end

#get_account_infoObject



39
40
41
# File 'lib/sensi/thermostat.rb', line 39

def 
	convert(@thermostat_connection.get_info)
end

#heat_on?Boolean

def mode(mode) @thermostat_connection.set(self.icd, ‘SetSystemMode’, mode.to_s.capitalize) end

Returns:

  • (Boolean)


172
173
174
# File 'lib/sensi/thermostat.rb', line 172

def heat_on?
	system_mode == 'Heat'
end

#humidityObject



137
138
139
# File 'lib/sensi/thermostat.rb', line 137

def humidity
	self.m.a.operational_status.humidity
end

#set(args) ⇒ Object

def set(mode: nil, temp: 70, scale: :F, fan: :auto, schedule: :off)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sensi/thermostat.rb', line 146

def set(args)
	# case mode
	# when :heat
	# 	@thermostat_connection.set(self.icd, 'SetSystemMode', mode.to_s.capitalize) unless system_mode == mode.to_s.capitalize
	# when :cool
	# 	@thermostat_connection.set(self.icd, 'SetSystemMode', mode.to_s.capitalize) unless system_cool_on?
	# end
	args.each do |k, v|
		case k
		when :mode
			# puts "#{system_mode}:#{v.to_s.capitalize}"
			return update_system_mode v
		when :fan
			return update_fan v
		when :temp
			return update_temp v
		when :schedule
			@thermostat_connection.set(self.icd, 'SetScheduleMode', v.to_s.capitalize) unless system_schedule == args
		end
	end
end

#settingsObject



108
109
110
# File 'lib/sensi/thermostat.rb', line 108

def settings
	self.m.a.environment_controls
end

#system_active?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/sensi/thermostat.rb', line 209

def system_active?
	active_mode != 'Off'
end

#system_auto_on?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/sensi/thermostat.rb', line 205

def system_auto_on?
	system_mode =- 'Auto'
end

#system_aux_on?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/sensi/thermostat.rb', line 201

def system_aux_on?
	system_mode == 'Aux'
end

#system_cool_on?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/sensi/thermostat.rb', line 197

def system_cool_on?
	system_mode == 'Cool'
end

#system_fanObject



122
123
124
# File 'lib/sensi/thermostat.rb', line 122

def system_fan
	self.m.a.environment_controls.fan_mode
end

#system_fan_off?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/sensi/thermostat.rb', line 238

def system_fan_off?
	system_fan == 'Auto'
end

#system_fan_on?Boolean

def system_cool_setpoint settings.cool_setpoint end

Returns:

  • (Boolean)


234
235
236
# File 'lib/sensi/thermostat.rb', line 234

def system_fan_on?
	system_fan == 'On'
end

#system_heat_on?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/sensi/thermostat.rb', line 193

def system_heat_on?
	system_mode == 'Heat'
end

#system_modeObject



141
142
143
# File 'lib/sensi/thermostat.rb', line 141

def system_mode
	self.m.a.environment_controls.system_mode
end

#system_modesObject



100
101
102
# File 'lib/sensi/thermostat.rb', line 100

def system_modes
	self.m.a.capabilities.system_modes
end

#system_off?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/sensi/thermostat.rb', line 189

def system_off?
	system_mode == 'Off'
end

#system_on?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/sensi/thermostat.rb', line 185

def system_on?
	system_mode != 'Off'
end

#system_temperature(type: nil) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/sensi/thermostat.rb', line 213

def system_temperature(type: nil)
	type = system_mode.downcase.to_sym if type.nil?

	case type
	when :heat
		return settings.heat_setpoint.f
	when :cool
		return settings.cool_setpoint.f
	else
		raise ArgumentError, "Type :#{type.to_s} not valid."
	end
end

#temperature(scale = :F) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/sensi/thermostat.rb', line 126

def temperature(scale = :F)
	case scale
	when :F
		return self.m.a.operational_status.temperature.f 
	when :C
		return self.m.a.operational_status.temperature.c
	else
		raise ArgumentError, "Scale #{scale} is not valid."
	end
end

#to_jsonObject



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/sensi/thermostat.rb', line 291

def to_json
   	hash = {}
    self.instance_variables.each do |var|
    	if var.is_a? HashToObject
    		hash[var.to_s.delete("@")] = self.instance_variable_get var.to_json
    	else
        	hash[var.to_s.delete("@")] = self.instance_variable_get var
        end
    end
    hash.to_json
end

#update(attempts = 3) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sensi/thermostat.rb', line 69

def update(attempts = 3)
	@response = nil
	attempt = 0
	# debugger
	while attempt < attempts and not valid_response?
		@response = @thermostat_connection.poll
		attempt += 1
	end

	return false if @response.code != 200

	update_self(self, @response) if valid_response?
	!@response.timed_out? and valid_response?
end

#update_fan(state) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/sensi/thermostat.rb', line 252

def update_fan(state)
	if system_fan == state.to_s.capitalize
		return false
	else
		result = @thermostat_connection.set(self.icd, 'SetFanMode', state.to_s.capitalize) 
		update
		return result
	end
	# case state
	# when 'On'
	# 	return @thermostat_connection.set(self.icd, 'SetFanMode', state.to_s.capitalize) unless system_fan_on?
	# when 'Auto'
	# 	return @thermostat_connection.set(self.icd, 'SetFanMode', state.to_s.capitalize) unless system_fan_off?
	# else
	# 	raise StandarError, "#{v} is not a valid fan state."
	# end
end

#update_self(hto, response) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sensi/thermostat.rb', line 84

def update_self(hto, response)
	datums = (response.methods - Object.new.methods).reject!{|i| i.to_s =~ /=|\?|convert|contains_hash|add|json|message_id|groups_token/ }
	datums.each do |data|
		# debugger
		if hto.respond_to?(data) and  hto.send(data).class == Sensi::PollResponse and response.send(data).class == Sensi::PollResponse
			update_self(hto.send(data), response.send(data))
		elsif hto.respond_to?(data) and  hto.send(data).class == Sensi::HashToObject and response.send(data).class == Sensi::HashToObject
			update_self(hto.send(data), response.send(data))
		elsif hto.respond_to?(data) and response.send(data).class == Sensi::PollResponse
			hto.send((data.to_s + '=').to_sym, response.send(data))
		else 
			hto.add_var(data.to_s, response.send(data))
		end
	end
end

#update_system_mode(state) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'lib/sensi/thermostat.rb', line 242

def update_system_mode(state)
	if system_mode == state.to_s.capitalize
		return false
	else 
		result = @thermostat_connection.set(self.icd, 'SetSystemMode', state.to_s.capitalize)  
		update
		return result
	end
end

#update_temp(temperature, mode: nil, scale: :F) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/sensi/thermostat.rb', line 270

def update_temp(temperature, mode: nil, scale: :F)
	mode = system_mode if mode.nil?

	case mode
	when 'Heat'
		return true if system_temperature(type: :heat) == temperature
		result = @thermostat_connection.set(self.icd, 'SetHeat', temperature.to_s.capitalize, scale.to_s.capitalize) 
	when 'Cool'
		return true if system_temperature(type: :cool) == temperature
		result = @thermostat_connection.set(self.icd, 'SetCool', temperature.to_s.capitalize, scale.to_s.capitalize) 
	when 'Off'

	when 'Auto'

	when 'Aux'

	end
	update
	result
end

#valid_response?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/sensi/thermostat.rb', line 62

def valid_response?
	# debugger
	@response.respond_to?(:m) and @response.m.class == Sensi::HashToObject
rescue StandardError
	return false
end