Class: Ecobee::Thermostat
- Inherits:
-
Hash
- Object
- Hash
- Ecobee::Thermostat
- Defined in:
- lib/ecobee/thermostat.rb
Constant Summary collapse
- DEFAULT_SELECTION_ARGS =
{ includeRuntime: true, includeExtendedRuntime: true, includeElectricity: true, includeSettings: true, includeLocation: true, includeProgram: true, includeEvents: true, includeDevice: true, includeTechnician: true, includeUtility: true, includeAlerts: true, includeWeather: true, includeOemConfig: true, includeEquipmentStatus: true, includeNotificationSettings: true, includeVersion: true, includeSensors: true }
Instance Attribute Summary collapse
-
#auto_refresh ⇒ Object
readonly
Returns the value of attribute auto_refresh.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
Instance Method Summary collapse
- #celsius? ⇒ Boolean
- #cool_range(with_delta: false) ⇒ Object
- #desired_cool ⇒ Object
- #desired_cool=(temp) ⇒ Object
- #desired_fan_mode ⇒ Object
- #desired_fan_mode=(fan) ⇒ Object
- #desired_heat ⇒ Object
- #desired_heat=(temp) ⇒ Object
- #desired_range ⇒ Object
- #dump(pretty: true) ⇒ Object
- #heat_cool_min_delta ⇒ Object
- #heat_range(with_delta: false) ⇒ Object
- #humidity ⇒ Object
- #index ⇒ Object
-
#initialize(auto_refresh: 0, fake_index: nil, index: 0, fake_max_index: 0, selection: nil, selection_args: {}, token: nil) ⇒ Thermostat
constructor
A new instance of Thermostat.
- #max_index ⇒ Object
- #mode ⇒ Object
- #mode=(mode) ⇒ Object
- #model ⇒ Object
- #my_selection ⇒ Object
- #name ⇒ Object
- #refresh ⇒ Object
- #set_hold(cool_hold_temp: unitize(self[:runtime][:desiredCool]), fan: nil, heat_hold_temp: unitize(self[:runtime][:desiredHeat]), hold_type: 'nextTransition') ⇒ Object
- #set_mode(mode) ⇒ Object
- #temperature ⇒ Object
- #to_range(low, high) ⇒ Object
- #to_sym(obj = self) ⇒ Object
- #un_unitize(value) ⇒ Object
-
#unitize(value) ⇒ Object
converts Ecobee farenheit * 10 integer input, returns farenheit or celsius output rounded to nearest .5.
- #update(functions: nil, thermostat: nil) ⇒ Object
Constructor Details
#initialize(auto_refresh: 0, fake_index: nil, index: 0, fake_max_index: 0, selection: nil, selection_args: {}, token: nil) ⇒ Thermostat
Returns a new instance of Thermostat.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ecobee/thermostat.rb', line 29 def initialize( auto_refresh: 0, fake_index: nil, index: 0, fake_max_index: 0, selection: nil, selection_args: {}, token: nil ) # TODO: add auto-refresh thread handling @auto_refresh = auto_refresh raise ArgumentError.new('No token: specified') unless token @http = token.http @fake_index = fake_index @fake_max_index = fake_max_index @index = index @selection ||= Ecobee::Selection( DEFAULT_SELECTION_ARGS.merge(selection_args) ) refresh end |
Instance Attribute Details
#auto_refresh ⇒ Object (readonly)
Returns the value of attribute auto_refresh.
27 28 29 |
# File 'lib/ecobee/thermostat.rb', line 27 def auto_refresh @auto_refresh end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
27 28 29 |
# File 'lib/ecobee/thermostat.rb', line 27 def http @http end |
Instance Method Details
#celsius? ⇒ Boolean
52 53 54 |
# File 'lib/ecobee/thermostat.rb', line 52 def celsius? self[:settings][:useCelsius] end |
#cool_range(with_delta: false) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/ecobee/thermostat.rb', line 56 def cool_range(with_delta: false) if with_delta low_range = [unitize(self[:settings][:coolRangeLow]), desired_heat + heat_cool_min_delta].max else low_range = unitize(self[:settings][:coolRangeLow]) end to_range(low_range, unitize(self[:settings][:coolRangeHigh])) end |
#desired_cool ⇒ Object
74 75 76 |
# File 'lib/ecobee/thermostat.rb', line 74 def desired_cool unitize(self[:runtime][:desiredCool]) end |
#desired_cool=(temp) ⇒ Object
78 79 80 |
# File 'lib/ecobee/thermostat.rb', line 78 def desired_cool=(temp) set_hold(cool_hold_temp: temp) end |
#desired_fan_mode ⇒ Object
82 83 84 |
# File 'lib/ecobee/thermostat.rb', line 82 def desired_fan_mode self[:runtime][:desiredFanMode] end |
#desired_fan_mode=(fan) ⇒ Object
86 87 88 |
# File 'lib/ecobee/thermostat.rb', line 86 def desired_fan_mode=(fan) set_hold(fan: fan) end |
#desired_heat ⇒ Object
90 91 92 |
# File 'lib/ecobee/thermostat.rb', line 90 def desired_heat unitize(self[:runtime][:desiredHeat]) end |
#desired_heat=(temp) ⇒ Object
94 95 96 97 |
# File 'lib/ecobee/thermostat.rb', line 94 def desired_heat=(temp) # need celcius un_unitize set_hold(heat_hold_temp: temp) end |
#desired_range ⇒ Object
99 100 101 |
# File 'lib/ecobee/thermostat.rb', line 99 def desired_range to_range(desired_heat, desired_cool) end |
#dump(pretty: true) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/ecobee/thermostat.rb', line 66 def dump(pretty: true) if pretty self.select { |k, v| k.is_a? Symbol }.pretty_inspect else self.select { |k, v| k.is_a? Symbol } end end |
#heat_cool_min_delta ⇒ Object
103 104 105 |
# File 'lib/ecobee/thermostat.rb', line 103 def heat_cool_min_delta unitize(self[:settings][:heatCoolMinDelta]) end |
#heat_range(with_delta: false) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/ecobee/thermostat.rb', line 107 def heat_range(with_delta: false) if with_delta high_range = [unitize(self[:settings][:heatRangeHigh]), desired_cool - heat_cool_min_delta].min else high_range = unitize(self[:settings][:heatRangeHigh]) end to_range(unitize(self[:settings][:heatRangeLow]), high_range) end |
#humidity ⇒ Object
117 118 119 |
# File 'lib/ecobee/thermostat.rb', line 117 def humidity self[:runtime][:actualHumidity] end |
#index ⇒ Object
121 122 123 |
# File 'lib/ecobee/thermostat.rb', line 121 def index @fake_index || @index end |
#max_index ⇒ Object
125 126 127 |
# File 'lib/ecobee/thermostat.rb', line 125 def max_index [@fake_index || 0, @max_index, @fake_max_index].max end |
#mode ⇒ Object
129 130 131 |
# File 'lib/ecobee/thermostat.rb', line 129 def mode self[:settings][:hvacMode] end |
#mode=(mode) ⇒ Object
133 134 135 |
# File 'lib/ecobee/thermostat.rb', line 133 def mode=(mode) set_mode(mode) end |
#model ⇒ Object
137 138 139 |
# File 'lib/ecobee/thermostat.rb', line 137 def model Ecobee::Model(self[:modelNumber]) end |
#my_selection ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/ecobee/thermostat.rb', line 141 def my_selection { 'selection' => { 'selectionType' => 'thermostats', 'selectionMatch' => self[:identifier] } } end |
#name ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/ecobee/thermostat.rb', line 150 def name if @fake_index "Fake No. #{@fake_index}" else self[:name] end end |
#refresh ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ecobee/thermostat.rb', line 158 def refresh response = @http.get(arg: :thermostat, options: @selection) if @index + 1 > response['thermostatList'].length raise ThermostatError.new('No such thermostat') end @max_index = response['thermostatList'].length - 1 list = response['thermostatList'][@index] self.replace list.merge(to_sym(list)) end |
#set_hold(cool_hold_temp: unitize(self[:runtime][:desiredCool]), fan: nil, heat_hold_temp: unitize(self[:runtime][:desiredHeat]), hold_type: 'nextTransition') ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ecobee/thermostat.rb', line 169 def set_hold( cool_hold_temp: unitize(self[:runtime][:desiredCool]), fan: nil, heat_hold_temp: unitize(self[:runtime][:desiredHeat]), hold_type: 'nextTransition' ) params = { 'holdType' => 'nextTransition', 'coolHoldTemp' => un_unitize(cool_hold_temp), 'heatHoldTemp' => un_unitize(heat_hold_temp) } params.merge!({ 'fan' => fan }) if fan update(functions: [{ 'type' => 'setHold', 'params' => params }]) end |
#set_mode(mode) ⇒ Object
184 185 186 |
# File 'lib/ecobee/thermostat.rb', line 184 def set_mode(mode) update(thermostat: { 'settings' => { 'hvacMode' => mode } }) end |
#temperature ⇒ Object
188 189 190 |
# File 'lib/ecobee/thermostat.rb', line 188 def temperature unitize(self[:runtime][:actualTemperature]) end |
#to_range(low, high) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/ecobee/thermostat.rb', line 192 def to_range(low, high) if celsius? ((low * 2).round..(high * 2).round).to_a .map { |deg| deg / 2.0 } else (low.round..high.round).to_a end end |
#to_sym(obj = self) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ecobee/thermostat.rb', line 201 def to_sym(obj = self) if obj.is_a? Hash Hash[obj.map do |key, val| if key.is_a? String [key.to_sym, to_sym(val)] else [key, to_sym(val)] end end] elsif obj.is_a? Array obj.map { |item| to_sym(item) } else obj end end |
#un_unitize(value) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/ecobee/thermostat.rb', line 217 def un_unitize(value) if celsius? (((value.to_f * 9/5) + 32) * 10).round else (value.to_f * 10).round end end |
#unitize(value) ⇒ Object
converts Ecobee farenheit * 10 integer input, returns
farenheit or celsius output rounded to nearest .5
227 228 229 230 231 232 233 234 |
# File 'lib/ecobee/thermostat.rb', line 227 def unitize(value) if celsius? celsius = (value.to_f / 10.0 - 32) * 5/9 (celsius / 5).round(1) * 5 else value.to_f / 10.0 end end |
#update(functions: nil, thermostat: nil) ⇒ Object
236 237 238 239 240 241 242 243 244 |
# File 'lib/ecobee/thermostat.rb', line 236 def update( functions: nil, thermostat: nil ) body = my_selection body.merge!({ 'functions' => functions }) if functions body.merge!({ 'thermostat' => thermostat }) if thermostat @http.post(arg: :thermostat, body: body) end |