Class: HTCC::Thermostat

Inherits:
Object
  • Object
show all
Defined in:
lib/htcc/thermostat.rb

Defined Under Namespace

Classes: FanError, HoldError, SystemError, TemperatureError

Constant Summary collapse

SYSTEM_MODES =
i[emergency_heat heat off cool auto]
FAN_MODES =
i[auto on circulate schedule]
HOLD_TYPES =
i[none temporary permanent]
EQUIPMENT_OUTPUT_STATUS =
i[off heating cooling fan_running]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info, client) ⇒ Thermostat



12
13
14
15
16
17
# File 'lib/htcc/thermostat.rb', line 12

def initialize(info, client)
  @info = info
  @client = client
  @refresh = true
  @status = {}
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

Instance Method Details

#connected?Boolean



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

def connected?
  get_status
  @status['deviceLive'] && !@status['communicationLost']
end

#cool_setpointObject

Cooling temperature setting



97
98
99
100
# File 'lib/htcc/thermostat.rb', line 97

def cool_setpoint
  get_status
  @status['latestData']['uiData']['CoolSetpoint']
end

#cool_setpoint=(temp) ⇒ Object



102
103
104
105
106
# File 'lib/htcc/thermostat.rb', line 102

def cool_setpoint=(temp)
  raise_min_setpoint(min_cool_setpoint, temp) if temp < min_cool_setpoint
  raise_max_setpoint(max_cool_setpoint, temp) if temp > max_cool_setpoint
  change_setting(cool_setpoint: temp, hold: :temporary)
end

#cool_setpoint_rangeObject



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

def cool_setpoint_range
  [min_cool_setpoint, max_cool_setpoint]
end

#current_temperatureObject

Current ambient temperature



86
87
88
89
# File 'lib/htcc/thermostat.rb', line 86

def current_temperature
  get_status
  @status['latestData']['uiData']['DispTemperature']
end

#fan_modeObject



73
74
75
76
# File 'lib/htcc/thermostat.rb', line 73

def fan_mode
  get_status
  FAN_MODES[@status['latestData']['fanData']['fanMode']]
end

#fan_mode=(mode) ⇒ Object



78
79
80
81
82
83
# File 'lib/htcc/thermostat.rb', line 78

def fan_mode=(mode)
  unless fan_modes.index(mode)
    raise FanError.new("Unknown mode: #{mode.inspect}. Allowed modes: #{fan_modes.inspect}")
  end
  change_setting(fan_mode: mode)
end

#fan_modesObject



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/htcc/thermostat.rb', line 165

def fan_modes
  return @fan_modes if @fan_modes

  get_status if @status.empty?
  allowed_modes = [
    @status['latestData']['fanData']['fanModeAutoAllowed'],
    @status['latestData']['fanData']['fanModeOnAllowed'],
    @status['latestData']['fanData']['fanModeCirculateAllowed'],
    @status['latestData']['fanData']['fanModeFollowScheduleAllowed']
  ]
  @fan_modes = FAN_MODES.select.with_index { |_, i| allowed_modes[i] }
end

#fan_running?Boolean



68
69
70
71
# File 'lib/htcc/thermostat.rb', line 68

def fan_running?
  get_status
  @status['latestData']['fanData']['fanIsRunning']
end

#has_fan?Boolean



61
62
63
64
65
66
# File 'lib/htcc/thermostat.rb', line 61

def has_fan?
  return @has_fan unless @has_fan.nil?

  get_status if @status.empty?
  @has_fan = @status['latestData']['hasFan']
end

#heat_setpointObject

Heating temperature setting



113
114
115
116
# File 'lib/htcc/thermostat.rb', line 113

def heat_setpoint
  get_status
  @status['latestData']['uiData']['HeatSetpoint']
end

#heat_setpoint=(temp) ⇒ Object



118
119
120
121
122
# File 'lib/htcc/thermostat.rb', line 118

def heat_setpoint=(temp)
  raise_min_setpoint(min_heat_setpoint, temp) if temp < min_heat_setpoint
  raise_max_setpoint(max_heat_setpoint, temp) if temp > max_heat_setpoint
  change_setting(heat_setpoint: temp, hold: :temporary)
end

#heat_setpoint_rangeObject



124
125
126
# File 'lib/htcc/thermostat.rb', line 124

def heat_setpoint_range
  [min_heat_setpoint, max_heat_setpoint]
end

#holdObject



132
133
134
135
# File 'lib/htcc/thermostat.rb', line 132

def hold
  get_status
  HOLD_TYPES[@status['latestData']['uiData']['StatusHeat']] # Both status are the same
end

#hold=(mode) ⇒ Object



137
138
139
140
141
142
# File 'lib/htcc/thermostat.rb', line 137

def hold=(mode)
  unless HOLD_TYPES.index(mode)
    raise HoldError.new("Unknown mode: #{mode.inspect}. Allowed modes: #{HOLD_TYPES.inspect}")
  end
  change_setting(hold: mode)
end

#idObject



27
28
29
# File 'lib/htcc/thermostat.rb', line 27

def id
  @info['DeviceID']
end

#mac_addressObject



31
32
33
# File 'lib/htcc/thermostat.rb', line 31

def mac_address
  @info['MacID']
end

#nameObject



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

def name
  @info['Name']
end

#no_refresh(&block) ⇒ Object



178
179
180
181
182
183
# File 'lib/htcc/thermostat.rb', line 178

def no_refresh(&block)
  @refresh = false
  result = yield
  @refresh = true
  result
end

#output_statusObject



144
145
146
147
148
149
# File 'lib/htcc/thermostat.rb', line 144

def output_status
  get_status
  status = @status['latestData']['uiData']['EquipmentOutputStatus']
  status = no_refresh { fan_running? ? 3 : status } if status.zero?
  EQUIPMENT_OUTPUT_STATUS[@status['latestData']['uiData']['EquipmentOutputStatus']]
end

#resume_scheduleObject



128
129
130
# File 'lib/htcc/thermostat.rb', line 128

def resume_schedule
  change_setting(hold: :none)
end

#SchedulerObject



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

def Scheduler
  @scheduler ||= Scheduler.new(id, @client)
end

#SettingsObject



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

def Settings
  @settings ||= Settings.new(id, @client)
end

#status(refresh = false) ⇒ Object



44
45
46
47
# File 'lib/htcc/thermostat.rb', line 44

def status(refresh = false)
  get_status if @status.empty? || refresh
  @status
end

#system_modeObject



49
50
51
52
# File 'lib/htcc/thermostat.rb', line 49

def system_mode
  get_status
  SYSTEM_MODES[@status['latestData']['uiData']['SystemSwitchPosition']]
end

#system_mode=(mode) ⇒ Object



54
55
56
57
58
59
# File 'lib/htcc/thermostat.rb', line 54

def system_mode=(mode)
  unless system_modes.index(mode)
    raise SystemError.new("Unknown mode: #{mode.inspect}. Allowed modes: #{system_modes.inspect}")
  end
  change_setting(system_mode: mode)
end

#system_modesObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/htcc/thermostat.rb', line 151

def system_modes
  return @system_modes if @system_modes

  get_status if @status.empty?
  allowed_modes = [
    @status['latestData']['uiData']['SwitchEmergencyHeatAllowed'],
    @status['latestData']['uiData']['SwitchHeatAllowed'],
    @status['latestData']['uiData']['SwitchOffAllowed'],
    @status['latestData']['uiData']['SwitchCoolAllowed'],
    @status['latestData']['uiData']['SwitchAutoAllowed'],
  ]
  @system_modes = SYSTEM_MODES.select.with_index { |_, i| allowed_modes[i] }
end

#temperature_unitObject



91
92
93
94
# File 'lib/htcc/thermostat.rb', line 91

def temperature_unit
  get_status
  @status['latestData']['uiData']['DisplayUnits']
end