Class: Hiveline::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hiveline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/hiveline.rb', line 10

def initialize(username, password)
	@username = username
	@password = password
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/hiveline.rb', line 8

def password
  @password
end

#session_cacheObject

Returns the value of attribute session_cache.



8
9
10
# File 'lib/hiveline.rb', line 8

def session_cache
  @session_cache
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/hiveline.rb', line 8

def username
  @username
end

Instance Method Details

#api_sessionObject



15
16
17
# File 'lib/hiveline.rb', line 15

def api_session
	session[:ApiSession]
end

#get_devices(hub_id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hiveline.rb', line 78

def get_devices(hub_id)
	devices_url = "https://api.hivehome.com/v5/users/#{self.username}/hubs/#{hub_id}/devices"
	response = self.class.get(devices_url, {
		headers: {
			"Cookie" => "ApiSession=#{self.api_session}"	
		},
		follow_redirects: false
	})
	if response.code == 200
		JSON.parse(response.body)
	else
		nil
	end
end

#get_historyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hiveline.rb', line 58

def get_history
	# Just get the first device, ignore others
	hub_id = hub_ids[0]
	thermostat = get_thermostat hub_id
	thermostat_id = thermostat["id"]
	history_url = "https://api.hivehome.com/v5/users/#{self.username}/widgets/temperature/#{thermostat_id}/history?period=today"
	response = self.class.get(history_url, {
		headers: {
			"Cookie" => "ApiSession=#{self.api_session}",
			"Content-Type" => "application/json"
			},
		follow_redirects: false
	})
	if response.code == 200
		JSON.parse(response.body)
	else
		nil
	end
end

#get_temperatureObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hiveline.rb', line 42

def get_temperature
	weather_url = "https://api.hivehome.com/v5/users/#{self.username}/widgets/temperature"
	response = self.class.get(weather_url, {
		headers: {
			"Cookie" => "ApiSession=#{self.api_session}",
			"Content-Type" => "application/json"
		},
		follow_redirects: false
	})
	if response.code == 200
		JSON.parse(response.body)
	else
		nil
	end
end

#get_thermostat(hub_id) ⇒ Object



93
94
95
# File 'lib/hiveline.rb', line 93

def get_thermostat(hub_id)
	get_devices(hub_id).select { |device| device["type"] == "HAHVACThermostat" }.pop
end

#hub_idsObject



19
20
21
# File 'lib/hiveline.rb', line 19

def hub_ids
	session[:hubIds]
end

#sessionObject



23
24
25
# File 'lib/hiveline.rb', line 23

def session
	@session_cache ||= get_session
end

#set_temperature(temp) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hiveline.rb', line 27

def set_temperature(temp)
	heating_url = "https://api.hivehome.com/v5/users/#{self.username}/widgets/climate/targetTemperature"
	response = self.class.put(heating_url, {
		body: {
			temperature: temp,
			temperatureUnit: "C"
		},
		headers: {
			"Cookie" => "ApiSession=#{self.api_session}",
		},
		follow_redirects: false
	})
	response.code == 204
end