class Sensi::Thermostat

Attributes

account[RW]
response[RW]
thermostat_connection[RW]

Public Class Methods

new(login, password, json = nil) click to toggle source
# File lib/sensi/thermostat.rb, line 23
def initialize(login, password, json = nil)
        @account = Sensi::Account.new(login, password)
        @thermostat_connection = Sensi::ThermostatConnection.new(@account)
end

Public Instance Methods

active_mode() click to toggle source
# File lib/sensi/thermostat.rb, line 112
def active_mode
        self.m.a.operational_status.running.mode
end
active_time() click to toggle source
# File lib/sensi/thermostat.rb, line 116
def active_time
        self.m.a.operational_status.running.time
rescue 
        "0"
end
aux_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 180
def aux_on?
        system_mode == 'Aux'
end
connect() click to toggle source
# File lib/sensi/thermostat.rb, line 28
def connect
        @account.login
        get_account_info
        connect_to_device
end
connect_to_device(connection_attempt = 3) click to toggle source
# 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?() click to toggle source
# File lib/sensi/thermostat.rb, line 56
def connected_to_device?
        self.m.respond_to?(:m)
rescue
        false
end
cool_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 176
def cool_on?
        system_mode == 'Cool'
end
disconnect() click to toggle source
# File lib/sensi/thermostat.rb, line 34
def disconnect
        @thermostat_connection.stop_polling
        @account.logout
end
equipment() click to toggle source
# File lib/sensi/thermostat.rb, line 104
def equipment
        self.m.a.capabilities.outdoor_equipment
end
from_json!(string) click to toggle source
# 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_info() click to toggle source
# File lib/sensi/thermostat.rb, line 39
def get_account_info
        convert(@thermostat_connection.get_info)
end
heat_on?() click to toggle source

def mode(mode)

@thermostat_connection.set(self.icd, 'SetSystemMode', mode.to_s.capitalize)

end

# File lib/sensi/thermostat.rb, line 172
def heat_on?
        system_mode == 'Heat'
end
humidity() click to toggle source
# File lib/sensi/thermostat.rb, line 137
def humidity
        self.m.a.operational_status.humidity
end
set(args) click to toggle source

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

# 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
settings() click to toggle source
# File lib/sensi/thermostat.rb, line 108
def settings
        self.m.a.environment_controls
end
system_active?() click to toggle source
# File lib/sensi/thermostat.rb, line 209
def system_active?
        active_mode != 'Off'
end
system_auto_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 205
def system_auto_on?
        system_mode =- 'Auto'
end
system_aux_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 201
def system_aux_on?
        system_mode == 'Aux'
end
system_cool_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 197
def system_cool_on?
        system_mode == 'Cool'
end
system_fan() click to toggle source
# File lib/sensi/thermostat.rb, line 122
def system_fan
        self.m.a.environment_controls.fan_mode
end
system_fan_off?() click to toggle source
# File lib/sensi/thermostat.rb, line 238
def system_fan_off?
        system_fan == 'Auto'
end
system_fan_on?() click to toggle source

def system_cool_setpoint

settings.cool_setpoint

end

# File lib/sensi/thermostat.rb, line 234
def system_fan_on?
        system_fan == 'On'
end
system_heat_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 193
def system_heat_on?
        system_mode == 'Heat'
end
system_mode() click to toggle source
# File lib/sensi/thermostat.rb, line 141
def system_mode
        self.m.a.environment_controls.system_mode
end
system_modes() click to toggle source
# File lib/sensi/thermostat.rb, line 100
def system_modes
        self.m.a.capabilities.system_modes
end
system_off?() click to toggle source
# File lib/sensi/thermostat.rb, line 189
def system_off?
        system_mode == 'Off'
end
system_on?() click to toggle source
# File lib/sensi/thermostat.rb, line 185
def system_on?
        system_mode != 'Off'
end
system_temperature(type: nil) click to toggle source
# 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) click to toggle source
# 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_json() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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, scale.to_s.capitalize) 
        when 'Cool'
                return true if system_temperature(type: :cool) == temperature
                result = @thermostat_connection.set(self.icd, 'SetCool', temperature.to_s, scale.to_s.capitalize) 
        when 'Off'

        when 'Auto'

        when 'Aux'

        end
        update
        result
end
valid_response?() click to toggle source
# 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