Class: TenHsServer::Device

Inherits:
Adapter
  • Object
show all
Defined in:
lib/ten_hs_server/device.rb

Overview

Adapter for TenHsServer devices endpoint, which returns information for each of the devices in Homeseer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name = nil, type = nil, room = nil, floor = nil) ⇒ Device

Returns a new instance of Device.



8
9
10
11
12
13
14
# File 'lib/ten_hs_server/device.rb', line 8

def initialize id, name=nil, type=nil, room=nil, floor=nil
  @id = id
  @_name = name
  @_type = type
  @_room = room
  @_floor = floor
end

Instance Attribute Details

#_floorObject (readonly)

Returns the value of attribute _floor.



6
7
8
# File 'lib/ten_hs_server/device.rb', line 6

def _floor
  @_floor
end

#_nameObject (readonly)

Returns the value of attribute _name.



6
7
8
# File 'lib/ten_hs_server/device.rb', line 6

def _name
  @_name
end

#_roomObject (readonly)

Returns the value of attribute _room.



6
7
8
# File 'lib/ten_hs_server/device.rb', line 6

def _room
  @_room
end

#_typeObject (readonly)

Returns the value of attribute _type.



6
7
8
# File 'lib/ten_hs_server/device.rb', line 6

def _type
  @_type
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/ten_hs_server/device.rb', line 6

def id
  @id
end

Class Method Details

.allObject

Load all devices.

Returns an array of Device instances.



90
91
92
93
94
95
96
97
98
99
# File 'lib/ten_hs_server/device.rb', line 90

def all
  response = get "?t=99&f=GetDevices"
  devices = parse_devices response.body

  devices.map! do |device|
    new(device[:id], device[:name], device[:type], device[:location], device[:floor])
  end

  devices
end

.find(id) ⇒ Object

Load a single device.

id - An string describing the device

Returns a hash describing the device.



106
107
108
109
110
111
# File 'lib/ten_hs_server/device.rb', line 106

def find id
  id = URI::encode(id)
  response = get "?t=99&f=GetDevice&d=#{id}"

  parse_device response.body
end

.off(id) ⇒ Object

Turn off a device.

id - An string describing the device

Returns a true or false describing the status of the device false = off true = on



148
149
150
151
152
153
# File 'lib/ten_hs_server/device.rb', line 148

def off id
  id = URI::encode(id)
  response = get "?t=99&f=DeviceOff&d=#{id}"

  parse_toggle_device response.body
end

.on(id) ⇒ Object

Turn on a device.

id - An string describing the device

Returns a true or false describing the status of the device false = off true = on



134
135
136
137
138
139
# File 'lib/ten_hs_server/device.rb', line 134

def on id
  id = URI::encode(id)
  response = get "?t=99&f=DeviceOn&d=#{id}"

  parse_toggle_device response.body
end

.toggle(id) ⇒ Object

Toggle a device.

id - An string describing the device

Returns a true or false describing the status of the device false = off true = on



120
121
122
123
124
125
# File 'lib/ten_hs_server/device.rb', line 120

def toggle id
  id = URI::encode(id)
  response = get "?t=99&f=ToggleDevice&d=#{id}"

  parse_toggle_device response.body
end

.value(id, value) ⇒ Object

Set device value.

id - An string describing the device value - The value to give the device



160
161
162
163
164
165
166
# File 'lib/ten_hs_server/device.rb', line 160

def value(id, value)
  id = URI::encode(id)
  value = URI::encode(value.to_s)
  response = get "?t=99&f=SetDeviceValue&d=#{id}&a=#{value}"

  parse_set_device_value response.body
end

Instance Method Details

#dim(value) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ten_hs_server/device.rb', line 33

def dim value
  if value == 0
    off
  else
    on
    self.class.value(id, value)
  end
end

#dimmableObject



67
68
69
70
# File 'lib/ten_hs_server/device.rb', line 67

def dimmable
  # Boolean that
  query[:dim]
end

#floorObject



63
64
65
# File 'lib/ten_hs_server/device.rb', line 63

def floor
  _floor ? _floor : query[:floor]
end

#nameObject



59
60
61
# File 'lib/ten_hs_server/device.rb', line 59

def name
  _name ? _name : query[:name]
end

#offObject



29
30
31
# File 'lib/ten_hs_server/device.rb', line 29

def off
  self.class.off(id)
end

#off?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/ten_hs_server/device.rb', line 47

def off?
  query[:status] == 3 ? true : false
end

#onObject



25
26
27
# File 'lib/ten_hs_server/device.rb', line 25

def on
  self.class.on(id)
end

#on?Boolean

Properties

Returns:

  • (Boolean)


43
44
45
# File 'lib/ten_hs_server/device.rb', line 43

def on?
  query[:status] == 2 ? true : false
end

#queryObject



16
17
18
# File 'lib/ten_hs_server/device.rb', line 16

def query
  @query || @query = self.class.find(id)
end

#roomObject



55
56
57
# File 'lib/ten_hs_server/device.rb', line 55

def room
  _room ? _room : query[:location]
end

#statusObject



72
73
74
75
76
77
78
# File 'lib/ten_hs_server/device.rb', line 72

def status
  # Status of device
  # 2 On
  # 3 Off
  # 4 Dimmed
  query[:status]
end

#toggleObject

Methods



21
22
23
# File 'lib/ten_hs_server/device.rb', line 21

def toggle
  self.class.toggle(id)
end

#typeObject



51
52
53
# File 'lib/ten_hs_server/device.rb', line 51

def type
  _type ? _type : query[:type]
end

#valueObject



80
81
82
83
# File 'lib/ten_hs_server/device.rb', line 80

def value
  # For dimmable devices this is a value between 0 and 100
  query[:value]
end