ruby_home 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests.yml +41 -0
  3. data/CHANGELOG.md +13 -0
  4. data/README.md +47 -84
  5. data/examples/air_purifier.rb +67 -0
  6. data/examples/air_quality_sensor.rb +120 -0
  7. data/examples/battery_service.rb +59 -0
  8. data/examples/carbon_dioxide_sensor.rb +79 -0
  9. data/examples/carbon_monoxide_sensor.rb +79 -0
  10. data/examples/contact_sensor.rb +67 -0
  11. data/examples/door.rb +49 -0
  12. data/examples/fan.rb +31 -0
  13. data/examples/fan_v2.rb +69 -0
  14. data/examples/garage_door_opener.rb +72 -0
  15. data/examples/heater_cooler.rb +93 -0
  16. data/examples/humidifier_dehumidifier.rb +89 -0
  17. data/examples/humidity_sensor.rb +63 -0
  18. data/examples/leak_sensor.rb +67 -0
  19. data/examples/light_sensor.rb +65 -0
  20. data/examples/lightbulb.rb +32 -0
  21. data/examples/lock_mechanism.rb +35 -0
  22. data/examples/motion_sensor.rb +67 -0
  23. data/examples/occupancy_sensor.rb +67 -0
  24. data/examples/outlet.rb +30 -0
  25. data/examples/security_system.rb +54 -0
  26. data/examples/smoke_sensor.rb +67 -0
  27. data/examples/switch.rb +17 -0
  28. data/examples/television.rb +78 -0
  29. data/examples/temperature_sensor.rb +63 -0
  30. data/examples/thermostat.rb +114 -0
  31. data/examples/window.rb +49 -0
  32. data/examples/window_covering.rb +73 -0
  33. data/lib/ruby_home.rb +0 -1
  34. data/lib/ruby_home/characteristic.rb +7 -1
  35. data/lib/ruby_home/config/categories.yml +39 -0
  36. data/lib/ruby_home/config/characteristics.yml +479 -3
  37. data/lib/ruby_home/config/services.yml +79 -4
  38. data/lib/ruby_home/configuration.rb +19 -0
  39. data/lib/ruby_home/dns/service.rb +2 -0
  40. data/lib/ruby_home/dns/text_record.rb +1 -1
  41. data/lib/ruby_home/errors.rb +4 -0
  42. data/lib/ruby_home/factories/characteristic_factory.rb +1 -0
  43. data/lib/ruby_home/factories/templates/characteristic_template.rb +1 -1
  44. data/lib/ruby_home/factories/templates/service_template.rb +1 -1
  45. data/lib/ruby_home/hap/server_handler.rb +4 -13
  46. data/lib/ruby_home/hap/values/uint8_value.rb +11 -1
  47. data/lib/ruby_home/http/controllers/application_controller.rb +10 -3
  48. data/lib/ruby_home/http/controllers/pair_setups_controller.rb +3 -3
  49. data/lib/ruby_home/http/controllers/pair_verifies_controller.rb +10 -12
  50. data/lib/ruby_home/http/controllers/pairings_controller.rb +1 -1
  51. data/lib/ruby_home/http/serializers/characteristic_serializer.rb +13 -5
  52. data/lib/ruby_home/http/serializers/service_serializer.rb +11 -1
  53. data/lib/ruby_home/http/services/verify_finish_service.rb +56 -0
  54. data/lib/ruby_home/http/services/verify_srp_service.rb +32 -41
  55. data/lib/ruby_home/persistable.rb +4 -12
  56. data/lib/ruby_home/service.rb +3 -0
  57. data/lib/ruby_home/version.rb +1 -1
  58. data/rubyhome.gemspec +7 -7
  59. data/sbin/service_generator.rb +12 -2
  60. metadata +65 -39
  61. data/.travis.yml +0 -31
@@ -0,0 +1,59 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ battery_service = RubyHome::ServiceFactory.create(:battery_service,
5
+ status_low_battery: 0, # required
6
+ charging_state: 0, # required
7
+ battery_level: 20, # required
8
+ name: "battery service", # optional
9
+ )
10
+
11
+ battery_service.status_low_battery.after_update do |status_low_battery|
12
+ if status_low_battery == 0
13
+ puts "battery service status low battery is battery level normal"
14
+ elsif status_low_battery == 1
15
+ puts "battery service status low battery is battery level low"
16
+ end
17
+ end
18
+
19
+ battery_service.charging_state.after_update do |charging_state|
20
+ if charging_state == 0
21
+ puts "battery service charging state is not charging"
22
+ elsif charging_state == 1
23
+ puts "battery service charging state is charging"
24
+ elsif charging_state == 2
25
+ puts "battery service charging state is not chargeable"
26
+ end
27
+ end
28
+
29
+ battery_service.battery_level.after_update do |battery_level|
30
+ puts "battery service battery level #{battery_level}"
31
+
32
+ if battery_level < 10 && battery_service.status_low_battery != 1
33
+ battery_service.status_low_battery = 1
34
+ end
35
+
36
+ if battery_level > 11 && battery_service.status_low_battery != 0
37
+ battery_service.status_low_battery = 0
38
+ end
39
+ end
40
+
41
+ Thread.new do
42
+ sleep 30
43
+
44
+ loop do
45
+ battery_service.charging_state = 0
46
+ while battery_service.battery_level > 0
47
+ battery_service.battery_level -= 1
48
+ sleep 1
49
+ end
50
+
51
+ battery_service.charging_state = 1
52
+ while battery_service.battery_level < 101
53
+ battery_service.battery_level += 1
54
+ sleep 1
55
+ end
56
+ end
57
+ end
58
+
59
+ RubyHome.run
@@ -0,0 +1,79 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ carbon_dioxide_sensor = RubyHome::ServiceFactory.create(:carbon_dioxide_sensor,
5
+ carbon_dioxide_detected: 0, # required
6
+ name: "carbon dioxide sensor", # optional
7
+ carbon_dioxide_peak_level: 0, # optional
8
+ carbon_dioxide_level: 0, # optional
9
+ status_tampered: 0, # optional
10
+ status_low_battery: 0, # optional
11
+ status_fault: 0, # optional
12
+ status_active: true # optional
13
+ )
14
+
15
+ carbon_dioxide_sensor.carbon_dioxide_detected.after_update do |carbon_dioxide_detected|
16
+ if carbon_dioxide_detected == 0
17
+ puts "carbon dioxide sensor carbon dioxide detected CO2 levels normal"
18
+ elsif carbon_dioxide_detected == 1
19
+ puts "carbon dioxide sensor carbon dioxide detected CO2 levels abnormal"
20
+ end
21
+ end
22
+
23
+ carbon_dioxide_sensor.carbon_dioxide_peak_level.after_update do |carbon_dioxide_peak_level|
24
+ puts "carbon dioxide sensor carbon_dioxide_peak_level #{carbon_dioxide_peak_level}"
25
+ end
26
+
27
+ carbon_dioxide_sensor.carbon_dioxide_level.after_update do |carbon_dioxide_level|
28
+ puts "carbon dioxide sensor carbon_dioxide_level #{carbon_dioxide_level}"
29
+ end
30
+
31
+ carbon_dioxide_sensor.status_tampered.after_update do |status_tampered|
32
+ if status_tampered == 0
33
+ puts "carbon dioxide sensor status_tampered not tampered"
34
+ elsif status_tampered == 1
35
+ puts "carbon dioxide sensor status_tampered tampered"
36
+ end
37
+ end
38
+
39
+ carbon_dioxide_sensor.status_low_battery.after_update do |status_low_battery|
40
+ if status_low_battery == 0
41
+ puts "carbon dioxide sensor low battery is battery level normal"
42
+ elsif status_low_battery == 1
43
+ puts "carbon dioxide sensor low battery is battery level low"
44
+ end
45
+ end
46
+
47
+ carbon_dioxide_sensor.status_fault.after_update do |status_fault|
48
+ if status_fault == 0
49
+ puts "carbon dioxide sensor status_fault no fault"
50
+ elsif status_fault == 1
51
+ puts "carbon dioxide sensor status_fault general fault"
52
+ end
53
+ end
54
+
55
+ carbon_dioxide_sensor.status_active.after_update do |active|
56
+ if active
57
+ puts "carbon dioxide sensor is active"
58
+ else
59
+ puts "carbon dioxide sensor is inactive"
60
+ end
61
+ end
62
+
63
+ Thread.new do
64
+ sleep 30
65
+
66
+ loop do
67
+ carbon_dioxide_sensor.carbon_dioxide_detected = (0..1).to_a.sample
68
+ carbon_dioxide_sensor.carbon_dioxide_peak_level = (0..100000).to_a.sample
69
+ carbon_dioxide_sensor.carbon_dioxide_level = (0..100000).to_a.sample
70
+ carbon_dioxide_sensor.status_tampered = (0..1).to_a.sample
71
+ carbon_dioxide_sensor.status_low_battery = (0..1).to_a.sample
72
+ carbon_dioxide_sensor.status_fault = (0..1).to_a.sample
73
+ carbon_dioxide_sensor.status_active = [true, false].to_a.sample
74
+
75
+ sleep 10
76
+ end
77
+ end
78
+
79
+ RubyHome.run
@@ -0,0 +1,79 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ carbon_monoxide_sensor = RubyHome::ServiceFactory.create(:carbon_monoxide_sensor,
5
+ carbon_monoxide_detected: 0, # required
6
+ name: "carbon monoxide sensor", # optional
7
+ carbon_monoxide_peak_level: 0, # optional
8
+ carbon_monoxide_level: 0, # optional
9
+ status_tampered: 0, # optional
10
+ status_low_battery: 0, # optional
11
+ status_fault: 0, # optional
12
+ status_active: true # optional
13
+ )
14
+
15
+ carbon_monoxide_sensor.carbon_monoxide_detected.after_update do |carbon_monoxide_detected|
16
+ if carbon_monoxide_detected == 0
17
+ puts "carbon monoxide sensor carbon monoxide detected CO2 levels normal"
18
+ elsif carbon_monoxide_detected == 1
19
+ puts "carbon monoxide sensor carbon monoxide detected CO2 levels abnormal"
20
+ end
21
+ end
22
+
23
+ carbon_monoxide_sensor.carbon_monoxide_peak_level.after_update do |carbon_monoxide_peak_level|
24
+ puts "carbon monoxide sensor carbon_monoxide_peak_level #{carbon_monoxide_peak_level}"
25
+ end
26
+
27
+ carbon_monoxide_sensor.carbon_monoxide_level.after_update do |carbon_monoxide_level|
28
+ puts "carbon monoxide sensor carbon_monoxide_level #{carbon_monoxide_level}"
29
+ end
30
+
31
+ carbon_monoxide_sensor.status_tampered.after_update do |status_tampered|
32
+ if status_tampered == 0
33
+ puts "carbon monoxide sensor status_tampered not tampered"
34
+ elsif status_tampered == 1
35
+ puts "carbon monoxide sensor status_tampered tampered"
36
+ end
37
+ end
38
+
39
+ carbon_monoxide_sensor.status_low_battery.after_update do |status_low_battery|
40
+ if status_low_battery == 0
41
+ puts "carbon monoxide sensor low battery is battery level normal"
42
+ elsif status_low_battery == 1
43
+ puts "carbon monoxide sensor low battery is battery level low"
44
+ end
45
+ end
46
+
47
+ carbon_monoxide_sensor.status_fault.after_update do |status_fault|
48
+ if status_fault == 0
49
+ puts "carbon monoxide sensor status_fault no fault"
50
+ elsif status_fault == 1
51
+ puts "carbon monoxide sensor status_fault general fault"
52
+ end
53
+ end
54
+
55
+ carbon_monoxide_sensor.status_active.after_update do |active|
56
+ if active
57
+ puts "carbon monoxide sensor is active"
58
+ else
59
+ puts "carbon monoxide sensor is inactive"
60
+ end
61
+ end
62
+
63
+ Thread.new do
64
+ sleep 30
65
+
66
+ loop do
67
+ carbon_monoxide_sensor.carbon_monoxide_detected = (0..1).to_a.sample
68
+ carbon_monoxide_sensor.carbon_monoxide_peak_level = (0..100).to_a.sample
69
+ carbon_monoxide_sensor.carbon_monoxide_level = (0..100).to_a.sample
70
+ carbon_monoxide_sensor.status_tampered = (0..1).to_a.sample
71
+ carbon_monoxide_sensor.status_low_battery = (0..1).to_a.sample
72
+ carbon_monoxide_sensor.status_fault = (0..1).to_a.sample
73
+ carbon_monoxide_sensor.status_active = [true, false].to_a.sample
74
+
75
+ sleep 10
76
+ end
77
+ end
78
+
79
+ RubyHome.run
@@ -0,0 +1,67 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ contact_sensor = RubyHome::ServiceFactory.create(:contact_sensor,
5
+ contact_sensor_state: 0, # required
6
+ name: "contact sensor", # optional
7
+ status_low_battery: 0, # optional
8
+ status_tampered: 0, # optional
9
+ status_fault: 0, # optional
10
+ status_active: true # optional
11
+ )
12
+
13
+ contact_sensor.contact_sensor_state.after_update do |contact_sensor_state|
14
+ if contact_sensor_state == 0
15
+ puts "contact sensor contact sensor state contact detected"
16
+ elsif contact_sensor_state == 1
17
+ puts "contact sensor contact sensor state contact not detected"
18
+ end
19
+ end
20
+
21
+ contact_sensor.status_low_battery.after_update do |status_low_battery|
22
+ if status_low_battery == 0
23
+ puts "contact sensor low battery is battery level normal"
24
+ elsif status_low_battery == 1
25
+ puts "contact sensor low battery is battery level low"
26
+ end
27
+ end
28
+
29
+ contact_sensor.status_tampered.after_update do |status_tampered|
30
+ if status_tampered == 0
31
+ puts "contact sensor status_tampered not tampered"
32
+ elsif status_tampered == 1
33
+ puts "contact sensor status_tampered tampered"
34
+ end
35
+ end
36
+
37
+ contact_sensor.status_fault.after_update do |status_fault|
38
+ if status_fault == 0
39
+ puts "contact sensor status_fault no fault"
40
+ elsif status_fault == 1
41
+ puts "contact sensor status_fault general fault"
42
+ end
43
+ end
44
+
45
+ contact_sensor.status_active.after_update do |active|
46
+ if active
47
+ puts "contact sensor is active"
48
+ else
49
+ puts "contact sensor is inactive"
50
+ end
51
+ end
52
+
53
+ Thread.new do
54
+ sleep 30
55
+
56
+ loop do
57
+ contact_sensor.contact_sensor_state = (0..1).to_a.sample
58
+ contact_sensor.status_low_battery = (0..1).to_a.sample
59
+ contact_sensor.status_tampered = (0..1).to_a.sample
60
+ contact_sensor.status_fault = (0..1).to_a.sample
61
+ contact_sensor.status_active = [true, false].to_a.sample
62
+
63
+ sleep 10
64
+ end
65
+ end
66
+
67
+ RubyHome.run
@@ -0,0 +1,49 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ door = RubyHome::ServiceFactory.create(:door,
5
+ target_position: 0, # required
6
+ position_state: 1, # required
7
+ current_position: 0, # required
8
+ name: "door", # optional
9
+ obstruction_detected: false, # optional
10
+ )
11
+
12
+ door.target_position.after_update do |target_position|
13
+ puts "door target position #{target_position}"
14
+
15
+ if target_position < door.current_position
16
+ door.position_state = 0
17
+ elsif target_position > door.current_position
18
+ door.position_state = 1
19
+ end
20
+
21
+ sleep 1
22
+
23
+ door.current_position = target_position
24
+ door.position_state = 2
25
+ end
26
+
27
+ position_state_values = {
28
+ 0 => 'Decreasing',
29
+ 1 => 'Increasing',
30
+ 2 => 'Stopped'
31
+ }
32
+ door.position_state.after_update do |position_state|
33
+ state = position_state_values[position_state]
34
+ puts "door position state #{state}"
35
+ end
36
+
37
+ door.current_position.after_update do |current_position|
38
+ puts "door current position #{current_position}"
39
+ end
40
+
41
+ door.obstruction_detected.after_update do |obstruction_detected|
42
+ if obstruction_detected
43
+ puts "door obstruction detected"
44
+ else
45
+ puts "door no obstruction detected"
46
+ end
47
+ end
48
+
49
+ RubyHome.run
@@ -0,0 +1,31 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ fan = RubyHome::ServiceFactory.create(:fan,
5
+ on: false, # required
6
+ name: "fan", # optional
7
+ rotation_speed: 50, # optional
8
+ rotation_direction: 0 # optional
9
+ )
10
+
11
+ fan.on.after_update do |on|
12
+ if on
13
+ puts "fan is on"
14
+ else
15
+ puts "fan is off"
16
+ end
17
+ end
18
+
19
+ fan.rotation_speed.after_update do |rotation_speed|
20
+ puts "fan is spinning at #{rotation_speed} speed"
21
+ end
22
+
23
+ fan.rotation_direction.after_update do |rotation_direction|
24
+ if rotation_direction == 0
25
+ puts "fan rotating clockwise"
26
+ elsif rotation_direction == 1
27
+ puts "fan rotating counter clockwise"
28
+ end
29
+ end
30
+
31
+ RubyHome.run
@@ -0,0 +1,69 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ fan = RubyHome::ServiceFactory.create(:fan_v2,
5
+ active: 1, # required
6
+ name: "fan", # optional
7
+ swing_mode: 1, # optional
8
+ rotation_speed: 0, # optional
9
+ rotation_direction: 0, # optional
10
+ lock_physical_controls: 0, # optional
11
+ target_fan_state: 0, # optional
12
+ current_fan_state: 0 # optional
13
+ )
14
+
15
+ fan.active.after_update do |active|
16
+ if active == 0
17
+ puts "fan is inactive"
18
+ elsif active == 1
19
+ puts "fan is active"
20
+ end
21
+ end
22
+
23
+ fan.swing_mode.after_update do |swing_mode|
24
+ if swing_mode == 0
25
+ puts "fan swing is disabled"
26
+ elsif swing_mode == 1
27
+ puts "fan swing is enabled"
28
+ end
29
+ end
30
+
31
+ fan.rotation_speed.after_update do |rotation_speed|
32
+ puts "fan is spinning at #{rotation_speed} speed"
33
+ end
34
+
35
+ fan.rotation_direction.after_update do |rotation_direction|
36
+ if rotation_direction == 0
37
+ puts "fan rotating clockwise"
38
+ elsif rotation_direction == 1
39
+ puts "fan rotating counter clockwise"
40
+ end
41
+ end
42
+
43
+ fan.lock_physical_controls.after_update do |lock_physical_controls|
44
+ if lock_physical_controls == 0
45
+ puts "fan control lock disabled"
46
+ elsif lock_physical_controls == 1
47
+ puts "fan control lock enabled"
48
+ end
49
+ end
50
+
51
+ fan.target_fan_state.after_update do |target_fan_state|
52
+ if target_fan_state == 0
53
+ puts "fan target fan state manual"
54
+ elsif target_fan_state == 1
55
+ puts "fan target fan state auto"
56
+ end
57
+ end
58
+
59
+ fan.current_fan_state.after_update do |current_fan_state|
60
+ if current_fan_state == 0
61
+ puts "fan current fan state inactive"
62
+ elsif current_fan_state == 1
63
+ puts "fan current fan state idle"
64
+ elsif current_fan_state == 2
65
+ puts "fan current fan state blowing air"
66
+ end
67
+ end
68
+
69
+ RubyHome.run
@@ -0,0 +1,72 @@
1
+ require 'ruby_home'
2
+
3
+ accessory_information = RubyHome::ServiceFactory.create(:accessory_information)
4
+ garage_door_opener = RubyHome::ServiceFactory.create(:garage_door_opener,
5
+ obstruction_detected: false, # required
6
+ target_door_state: 1, # required
7
+ current_door_state: 1, # required
8
+ name: "garage door opener", # optional
9
+ lock_target_state: 1, # optional
10
+ lock_current_state: 1, # optional
11
+ )
12
+
13
+ garage_door_opener.obstruction_detected.after_update do |obstruction_detected|
14
+ if obstruction_detected
15
+ puts "garage door opener obstruction detected"
16
+ else
17
+ puts "garage door opener no obstruction detected"
18
+ end
19
+ end
20
+
21
+ target_door_state_values = {
22
+ 0 => 'Open',
23
+ 1 => 'Closed',
24
+ }
25
+ garage_door_opener.target_door_state.after_update do |target_door_state|
26
+ state = target_door_state_values[target_door_state]
27
+ puts "garage door opener target door state is #{state}"
28
+
29
+ if target_door_state == 0
30
+ garage_door_opener.current_door_state = 2
31
+ sleep 1
32
+ garage_door_opener.current_door_state = 0
33
+ elsif target_door_state == 1
34
+ garage_door_opener.current_door_state = 3
35
+ sleep 1
36
+ garage_door_opener.current_door_state = 1
37
+ end
38
+ end
39
+
40
+ current_door_state_values = {
41
+ 0 => 'Open',
42
+ 1 => 'Closed',
43
+ 2 => 'Opening',
44
+ 3 => 'Closing',
45
+ 4 => 'Stopped'
46
+ }
47
+ garage_door_opener.current_door_state.after_update do |current_door_state|
48
+ state = current_door_state_values[current_door_state]
49
+ puts "garage door opener current door state door state is #{state}"
50
+ end
51
+
52
+ garage_door_opener.lock_target_state.after_update do |lock_target_state|
53
+ if lock_target_state == 0
54
+ puts "garage door opener lock target state is unsecured"
55
+ elsif lock_target_state == 1
56
+ puts "garage door opener lock target state is secured"
57
+ end
58
+ end
59
+
60
+ garage_door_opener.lock_current_state.after_update do |lock_current_state|
61
+ if lock_current_state == 0
62
+ puts "garage door opener lock current state is unsecured"
63
+ elsif lock_current_state == 1
64
+ puts "garage door opener lock current state is secured"
65
+ elsif lock_current_state == 2
66
+ puts "garage door opener lock current state is jammed"
67
+ elsif lock_current_state == 3
68
+ puts "garage door opener lock current state is unknown"
69
+ end
70
+ end
71
+
72
+ RubyHome.run