wizrb 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +62 -0
  5. data/doc/devices.md +135 -0
  6. data/doc/lighting.md +186 -0
  7. data/doc/power.md +37 -0
  8. data/exe/wizrb +5 -0
  9. data/lib/wizrb/cli.rb +79 -0
  10. data/lib/wizrb/lighting/discover.rb +40 -0
  11. data/lib/wizrb/lighting/events/set_brightness_event.rb +27 -0
  12. data/lib/wizrb/lighting/events/set_cold_white_event.rb +27 -0
  13. data/lib/wizrb/lighting/events/set_color_temp_event.rb +29 -0
  14. data/lib/wizrb/lighting/events/set_rgb_event.rb +29 -0
  15. data/lib/wizrb/lighting/events/set_scene_event.rb +22 -0
  16. data/lib/wizrb/lighting/events/set_speed_event.rb +27 -0
  17. data/lib/wizrb/lighting/events/set_warm_white_event.rb +27 -0
  18. data/lib/wizrb/lighting/group.rb +37 -0
  19. data/lib/wizrb/lighting/products/dimable_light.rb +33 -0
  20. data/lib/wizrb/lighting/products/light.rb +55 -0
  21. data/lib/wizrb/lighting/products/rgb_light.rb +58 -0
  22. data/lib/wizrb/lighting/products/tunable_light.rb +39 -0
  23. data/lib/wizrb/lighting/scenes/scene.rb +68 -0
  24. data/lib/wizrb/lighting/scenes/spooky_scene.rb +61 -0
  25. data/lib/wizrb/lighting/state.rb +45 -0
  26. data/lib/wizrb/lighting.rb +41 -0
  27. data/lib/wizrb/power/discover.rb +34 -0
  28. data/lib/wizrb/power/group.rb +10 -0
  29. data/lib/wizrb/power/products/smart_plug.rb +17 -0
  30. data/lib/wizrb/shared/connection.rb +76 -0
  31. data/lib/wizrb/shared/discover.rb +116 -0
  32. data/lib/wizrb/shared/events/base.rb +22 -0
  33. data/lib/wizrb/shared/events/power_event.rb +22 -0
  34. data/lib/wizrb/shared/events/reboot_event.rb +15 -0
  35. data/lib/wizrb/shared/events/refresh_event.rb +15 -0
  36. data/lib/wizrb/shared/events/reset_event.rb +15 -0
  37. data/lib/wizrb/shared/group.rb +68 -0
  38. data/lib/wizrb/shared/products/device.rb +105 -0
  39. data/lib/wizrb/shared/state.rb +32 -0
  40. data/lib/wizrb/version.rb +5 -0
  41. data/lib/wizrb.rb +32 -0
  42. metadata +45 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c51e1b9f14e73417fc63de9201782216077be5d2d5d6ad879df87b20bc12bf7f
4
- data.tar.gz: ba57f4a5987fabf04bb5fe3b885ff63290b8246d7ce24fba9e197f2bd794a730
3
+ metadata.gz: 93c20b34451939421c8be549770476540295a279b27c1218028c404eee376c78
4
+ data.tar.gz: 98c458a2589d357984ed03ad873c466ce7cc16869fc82ef0634ec9a963bbcaed
5
5
  SHA512:
6
- metadata.gz: a3eda1571b3e1df2c6fbf0a08b887bde531119b6e7994499bb33af7ec7a6953cbdc9128d4d89d70cd547f8d250785ab2b9ba5be4f9dc27640c0df628dcff5d79
7
- data.tar.gz: 2c1909523c7856016f1e10e47e42ab60a1d3b9bf0a71d6749d8286a11648695060cdac9d51925f6b68595b68cb1ae519eb024c80935eba4ea9f643cb6b5bc3cf
6
+ metadata.gz: dfcda0e85d20c77bc3f841bbb362c97401d9a2c469a10a79f9c73df91347b507cbcc368e30d027d7ddd5b4caa232913331ca47bdc248b4ff21f50a140c85cbf9
7
+ data.tar.gz: b21b5f60f1b98ab1f0a5e70f4fac966548f3fdaf24c84f1e7dab058770243d58a76f808ac335f2b3eaf581fd1c22ff6ca1d504315471809bb0c117ab72b32d51
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-25
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Bert McCutchen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Wizrb
2
+
3
+ ***Pronounced like "Wizard"***
4
+
5
+ This is a hobby project for getting the most out of Philips WiZ devices. Currently this project only supports Philips WiZ lights and plugs. If you would like to support this project, donations to buy new types of Philips WiZ accessories or coffee will be much appreciated!
6
+
7
+ [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/M4M76DVZR)
8
+
9
+ #### Compatability Notice
10
+
11
+ Compatability may vary since I only own [Philips WiZ Bulb A19 E26 (Full Color)](https://www.wizconnected.com/en-CA/consumer/products/046677556136/) bulbs and a [LED Strip Starter kit 2m (Full Color)](https://www.wizconnected.com/en-CA/consumer/products/046677560805/) to test with.
12
+
13
+ Testers with other (supported) Philips WiZ devices are welcome!
14
+
15
+ #### Tested Devices:
16
+
17
+ * [Bulb A19 E26 (Full Color)](https://www.wizconnected.com/en-CA/consumer/products/046677556136/)
18
+ * [Filament amber ST64 E27 (Dimmable)](https://www.wizconnected.com/en/consumer/products/8718699787332/)
19
+ * [LED Strip Starter kit 2m (Full Color)](https://www.wizconnected.com/en-CA/consumer/products/046677560805/)
20
+ * [Smart Plug (Type B)](https://www.wizconnected.com/en-CA/consumer/products/046677603090/)
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'wizrb'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle install
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install wizrb
37
+
38
+ ## Usage
39
+
40
+ Most of the documentation on how to use this gem is located within [Wizrb Devices](doc/devices.md).
41
+
42
+ Documentation for specific device types:
43
+ * [Lighting Devices](doc/lighting.md)
44
+ * [Power Devices](doc/power.md)
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bert-mccutchen/wizrb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/bert-mccutchen/wizrb/blob/master/CODE_OF_CONDUCT.md).
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
59
+
60
+ ## Code of Conduct
61
+
62
+ Everyone interacting in the Wizrb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bert-mccutchen/wizrb/blob/master/CODE_OF_CONDUCT.md).
data/doc/devices.md ADDED
@@ -0,0 +1,135 @@
1
+ # Wizrb Devices
2
+
3
+ All devices inherit from a single class `Wizrb::Shared::Devices`. These device classes contain features that are shared among the Phillips WiZ device lineup.
4
+
5
+ * [Connecting To Devices](#connecting-to-devices)
6
+ * [Individual Devices](#individual-devices)
7
+ * [Finding Devices](#finding-devices)
8
+ * [Interacting With Devices](#interacting-with-devices)
9
+ * [Reading State](#reading-state)
10
+ * [Power Control](#power-control)
11
+ * [Information / Administration](#information--administration)
12
+ * [Manually Dispatching Events](#manually-dispatching-events)
13
+ * [Individual Devices](#individual-devices)
14
+ * [Device Groups](#device-groups)
15
+ * [Dispatching Multiple Events](#dispatching-multiple-events)
16
+ * [Available Device Events](#available-device-events)
17
+
18
+ ## Connecting To Devices
19
+
20
+ #### Individual Devices
21
+ ```ruby
22
+ # Connect to device @ 127.0.0.1
23
+ device = Wizrb::Shared::Products::Device.new(ip: '127.0.0.1')
24
+
25
+ # Connect to device @ 127.0.0.1:38899
26
+ device = Wizrb::Shared::Products::Device.new(ip: '127.0.0.1', port: 38899)
27
+ ```
28
+
29
+ #### Finding Devices
30
+ ```ruby
31
+ # Finding all devices:
32
+ group = Wizrb::Shared::Discover.all
33
+ # => Wizrb::Shared::Group
34
+
35
+ # Finding all devices in home by ID:
36
+ group = Wizrb::Shared::Discover.home(1234)
37
+ # => Wizrb::Shared::Group
38
+
39
+ # Finding all devices in room by ID:
40
+ group = Wizrb::Shared::Discover.room(1234)
41
+ # => Wizrb::Shared::Group
42
+ ```
43
+
44
+ ## Interacting With Devices
45
+
46
+ #### Reading State
47
+ ```ruby
48
+ device = Wizrb::Shared::Products::Device.new(ip: '127.0.0.1')
49
+
50
+ device.state
51
+ # => Wizrb::Shared::State
52
+
53
+ device.state.power
54
+ # => true
55
+ ```
56
+
57
+ #### Power Control
58
+ ```ruby
59
+ device = Wizrb::Shared::Products::Device.new(ip: '127.0.0.1')
60
+
61
+ device.power_on
62
+ device.power_off
63
+ device.power_switch
64
+ ```
65
+
66
+ #### Information / Administration
67
+ ```ruby
68
+ device = Wizrb::Shared::Products::Device.new(ip: '127.0.0.1')
69
+
70
+ device.system_config
71
+ device.model_config
72
+ device.user_config
73
+ device.module_name
74
+
75
+ device.reboot # Reboots the device.
76
+ device.reset # Factory resets the device.
77
+ device.refresh # Refreshes known device state.
78
+ ```
79
+
80
+ ## Manually Dispatching Events
81
+
82
+ #### Individual Devices
83
+ ```ruby
84
+ device = Wizrb::Shared::Products::Device.new(ip: '127.0.0.1')
85
+
86
+ # Turn the device on.
87
+ device.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(true))
88
+
89
+ # Turn the device off.
90
+ device.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(false))
91
+ ```
92
+
93
+ #### Device Groups
94
+ ```ruby
95
+ group = Wizrb::Shared::Discover.all
96
+
97
+ # Turn on all devices.
98
+ group.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(true))
99
+
100
+ # Turn off all devices.
101
+ group.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(false))
102
+
103
+ # Turn on half of the devices.
104
+ # Groups implement Enumerable to iterate over the devices within.
105
+ group.each_with_index.each do |device, index|
106
+ next unless index % 2 == 0
107
+ device.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(true))
108
+ end
109
+ ```
110
+
111
+ #### Dispatching Multiple Events
112
+ Events are merged when dispatching multiples to reduce chatter.
113
+
114
+ ```ruby
115
+ # Notice the pluralization of dispatch_event.
116
+ group.dispatch_events(
117
+ Wizrb::Shared::Events::PowerEvent.new(true),
118
+ Wizrb::Shared::Events::RebootEvent.new
119
+ )
120
+ ```
121
+
122
+ #### Available Device Events
123
+ ```ruby
124
+ # Boolean ON - OFF
125
+ Wizrb::Shared::Events::PowerEvent.new(true)
126
+
127
+ # No arguments
128
+ Wizrb::Shared::Events::RebootEvent.new
129
+
130
+ # No arguments
131
+ Wizrb::Shared::Events::RefreshEvent.new
132
+
133
+ # No arguments
134
+ Wizrb::Shared::Events::ResetEvent.new
135
+ ```
data/doc/lighting.md ADDED
@@ -0,0 +1,186 @@
1
+ # Wizrb Lighting Devices
2
+
3
+ Lighting devices build onto the standard Wizrb Device feature set, providing the capability to create elaborate lighting setups.
4
+
5
+ * [Connecting To Lighting Devices](#connecting-to-lighting-devices)
6
+ * [Individual Lighting Devices](#individual-lighting-devices)
7
+ * [Finding Lighting Devices](#finding-lighting-devices)
8
+ * [Interacting With Lighting Devices](#interacting-with-lighting-devices)
9
+ * [Reading State](#reading-state)
10
+ * [Lighting State Control](#lighting-state-control)
11
+ * [Available Lighting Events](#available-lighting-events)
12
+ * [Scenes](#scenes)
13
+ * [Creating Custom Scenes](#creating-custom-scenes)
14
+ * [Using Custom Scenes](#using-custom-scenes)
15
+
16
+ ## Connecting To Lighting Devices
17
+
18
+ #### Individual Lighting Devices
19
+ ```ruby
20
+ # Connect to light @ 127.0.0.1
21
+ light = Wizrb::Lighting::Products::Light.new(ip: '127.0.0.1')
22
+
23
+ # Connect to light @ 127.0.0.1:38899
24
+ light = Wizrb::Lighting::Products::Light.new(ip: '127.0.0.1', port: 38899)
25
+ ```
26
+
27
+ #### Finding Lighting Devices
28
+ ```ruby
29
+ # Finding all devices:
30
+ group = Wizrb::Lighting::Discover.all
31
+ # => Wizrb::Lighting::Group
32
+
33
+ # Finding all devices in home by ID:
34
+ group = Wizrb::Lighting::Discover.home(1234)
35
+ # => Wizrb::Lighting::Group
36
+
37
+ # Finding all devices in room by ID:
38
+ group = Wizrb::Lighting::Discover.room(1234)
39
+ # => Wizrb::Lighting::Group
40
+ ```
41
+
42
+ ## Interacting With Lighting Devices
43
+ Rrefer to ["Interacting With Devices"](devices.md#interacting-with-devices) in the Wizrb Devices documentation for the standard set of device interactions.
44
+
45
+ #### Reading State
46
+ ```ruby
47
+ light = Wizrb::Lighting::Products::Light.new(ip: '127.0.0.1')
48
+
49
+ light.state
50
+ # => Wizrb::Lighting::State
51
+
52
+ light.state.power
53
+ # => true
54
+
55
+ light.state.cold_white
56
+ # => 255
57
+
58
+ light.state.color_temp
59
+ # => 3200
60
+
61
+ light.state.brightness
62
+ # => 100
63
+
64
+ light.state.rgb
65
+ # => { red: 255, green: 255, blue: 255 }
66
+
67
+ light.state.scene
68
+ # => :party
69
+
70
+ light.state.speed
71
+ # => 200
72
+
73
+ light.state.warm_white
74
+ # => 255
75
+ ```
76
+
77
+ #### Lighting State Control
78
+ ```ruby
79
+ light = Wizrb::Lighting::Products::Light.new(ip: '127.0.0.1')
80
+
81
+ # Integer 10 - 100
82
+ light.brightness(100)
83
+
84
+ # Integer 1 - 255
85
+ light.cold_white(255)
86
+
87
+ # Integer 1000 - 10000 in increments of 100
88
+ light.color_temp(3200)
89
+
90
+ # Integers 0 - 255
91
+ light.rgb(255, 255, 255)
92
+
93
+ # Any key in Wizrb::Lighting::SCENES
94
+ light.scene(:party)
95
+
96
+ # Integer 10 - 200
97
+ light.speed(200)
98
+
99
+ # Integer 1 - 255
100
+ light.warm_white(255)
101
+ ```
102
+
103
+ #### Available Lighting Events
104
+ ```ruby
105
+ # Integer 10 - 100
106
+ Wizrb::Lighting::Events::SetBrightnessEvent.new(100)
107
+
108
+ # Integer 1 - 255
109
+ Wizrb::Lighting::Events::SetColdWhiteEvent.new(255)
110
+
111
+ # Integer 1000 - 10000 in increments of 100
112
+ Wizrb::Lighting::Events::SetColorTempEvent.new(3200)
113
+
114
+ # Integers 0 - 255
115
+ Wizrb::Lighting::Events::SetRgbEvent.new(255, 255, 255)
116
+
117
+ # Any key in Wizrb::Lighting::SCENES
118
+ Wizrb::Lighting::Events::SetSceneEvent.new(:party)
119
+
120
+ # Integer 10 - 200
121
+ Wizrb::Lighting::Events::SetSpeedEvent.new(200)
122
+
123
+ # Integer 1 - 255
124
+ Wizrb::Lighting::Events::SetWarmWhiteEvent.new(255)
125
+ ```
126
+
127
+ ## Scenes
128
+
129
+ #### Creating Custom Scenes
130
+ You can create custom scenes that inherit from `Wizrb::Lighting::Scenes::Scene`.
131
+ ```ruby
132
+ # frozen_string_literal: true
133
+
134
+ class BurglarScene < Wizrb::Lighting::Scenes::Scene
135
+ RED_STATE_EVENTS = [
136
+ Wizrb::Lighting::Events::SetBrightnessEvent.new(100),
137
+ Wizrb::Lighting::Events::SetRgbEvent.new(255, 0, 0)
138
+ ].freeze
139
+
140
+ BLUE_STATE_EVENTS = [
141
+ Wizrb::Lighting::Events::SetBrightnessEvent.new(100),
142
+ Wizrb::Lighting::Events::SetRgbEvent.new(0, 0, 255)
143
+ ].freeze
144
+
145
+ def initialize(group)
146
+ super(group)
147
+ @toggle = true
148
+ end
149
+
150
+ protected
151
+
152
+ # Do something before your scene starts.
153
+ def before_start
154
+ @group.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(true))
155
+ end
156
+
157
+ # Called on loop until scene stopped.
158
+ def step
159
+ if @toggle
160
+ @group.dispatch_events(*RED_STATE_EVENTS)
161
+ else
162
+ @group.dispatch_events(*BLUE_STATE_EVENTS)
163
+ end
164
+
165
+ @toggle = !@toggle
166
+ sleep(1)
167
+ end
168
+
169
+ # Do something after your scene stops.
170
+ def after_stop
171
+ @group.dispatch_event(Wizrb::Shared::Events::PowerEvent.new(false))
172
+ end
173
+ end
174
+ ```
175
+
176
+ #### Using Custom Scenes
177
+ ```ruby
178
+ group = Wizrb::Lighting::Discover.all
179
+ scene = BurglarScene.new(group)
180
+
181
+ # Start the scene.
182
+ scene.start
183
+
184
+ # Stop the scene.
185
+ scene.stop
186
+ ```
data/doc/power.md ADDED
@@ -0,0 +1,37 @@
1
+ # Wizrb Power Devices
2
+
3
+ Power devices do not extend beyond the features of standard Wizrb Devices. For more information refer to the [Wizrb Devices](devices.md) documentation.
4
+
5
+ * [Connecting To Power Devices](#connecting-to-power-devices)
6
+ * [Individual Power Devices](#individual-power-devices)
7
+ * [Finding Power Devices](#finding-power-devices)
8
+ * [Interacting With Power Devices](#interacting-with-power-devices)
9
+
10
+ ## Connecting To Power Devices
11
+
12
+ #### Individual Power Devices
13
+ ```ruby
14
+ # Connect to plug @ 127.0.0.1
15
+ plug = Wizrb::Power::Products::SmartPlug.new(ip: '127.0.0.1')
16
+
17
+ # Connect to plug @ 127.0.0.1:38899
18
+ plug = Wizrb::Power::Products::SmartPlug.new(ip: '127.0.0.1', port: 38899)
19
+ ```
20
+
21
+ #### Finding Power Devices
22
+ ```ruby
23
+ # Finding all devices:
24
+ group = Wizrb::Power::Discover.all
25
+ # => Wizrb::Power::Group
26
+
27
+ # Finding all devices in home by ID:
28
+ group = Wizrb::Power::Discover.home(1234)
29
+ # => Wizrb::Power::Group
30
+
31
+ # Finding all devices in room by ID:
32
+ group = Wizrb::Power::Discover.room(1234)
33
+ # => Wizrb::Power::Group
34
+ ```
35
+
36
+ ## Interacting With Power Devices
37
+ Rrefer to ["Interacting With Devices"](devices.md#interacting-with-devices) in the Wizrb Devices documentation.
data/exe/wizrb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'wizrb/cli'
5
+ Wizrb::CLI.start
data/lib/wizrb/cli.rb ADDED
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require 'wizrb'
5
+
6
+ module Wizrb
7
+ class CLI < Thor
8
+ include Thor::Actions
9
+
10
+ package_name 'Wizrb'
11
+
12
+ desc 'scene [SCENE]', 'Start a scene on all lights'
13
+ method_option :scene, aliases: '-s', type: :string, required: true
14
+ def scene
15
+ scene_type = load_scene
16
+ return unless scene_type
17
+
18
+ group = find_lights
19
+ return unless group
20
+
21
+ scene = scene_type.new(group)
22
+ start(scene)
23
+ prompt_stop
24
+ stop(scene)
25
+ end
26
+
27
+ private
28
+
29
+ def scene_class(string)
30
+ name = string.split('_').map(&:capitalize).join
31
+ Object.const_get("Wizrb::Lighting::Scenes::#{name}Scene")
32
+ rescue NameError
33
+ nil
34
+ end
35
+
36
+ def load_scene
37
+ say('Loading scene... ')
38
+ scene_type = scene_class(options[:scene])
39
+
40
+ if scene_type
41
+ say('DONE', :green)
42
+ else
43
+ say('INVALID SCENE', :red)
44
+ end
45
+
46
+ scene_type
47
+ end
48
+
49
+ def find_lights
50
+ say('Finding lights... ')
51
+ group = Wizrb::Lighting::Discover.all(wait: 10)
52
+
53
+ if group.count == 0
54
+ say('NONE FOUND', :red)
55
+ return nil
56
+ else
57
+ say("#{group.count} FOUND", :green)
58
+ end
59
+
60
+ group
61
+ end
62
+
63
+ def start(scene)
64
+ say('Starting scene... ')
65
+ scene.start
66
+ say('DONE', :green)
67
+ end
68
+
69
+ def prompt_stop
70
+ nil until yes?('Would you like to stop? [y, yes]')
71
+ end
72
+
73
+ def stop(scene)
74
+ say('Stopping scene... ')
75
+ scene.stop
76
+ say('DONE', :green)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../shared/discover'
4
+ require_relative 'group'
5
+ require_relative 'products/dimable_light'
6
+ require_relative 'products/tunable_light'
7
+ require_relative 'products/rgb_light'
8
+
9
+ module Wizrb
10
+ module Lighting
11
+ class Discover < Wizrb::Shared::Discover
12
+ private
13
+
14
+ def parse_response(data, addr)
15
+ response = JSON.parse(data)
16
+ return unless response.dig('result', 'success') && addr[1] && addr[2]
17
+
18
+ resolve_device(ip: addr[2], port: addr[1])
19
+ rescue StandardError
20
+ nil
21
+ end
22
+
23
+ def resolve_device(ip:, port: 38_899)
24
+ module_name = Wizrb::Shared::Products::Device.new(ip: ip, port: port).module_name
25
+
26
+ if module_name.include?(Wizrb::Lighting::Products::RgbLight::MODULE_NAME_IDENTIFIER)
27
+ Wizrb::Lighting::Products::RgbLight.new(ip: ip, port: port)
28
+ elsif module_name.include?(Wizrb::Lighting::Products::TunableLight::MODULE_NAME_IDENTIFIER)
29
+ Wizrb::Lighting::Products::TunableLight.new(ip: ip, port: port)
30
+ elsif module_name.include?(Wizrb::Lighting::Products::DimableLight::MODULE_NAME_IDENTIFIER)
31
+ Wizrb::Lighting::Products::DimableLight.new(ip: ip, port: port)
32
+ end
33
+ end
34
+
35
+ def group_devices
36
+ Wizrb::Lighting::Group.new(devices: @devices)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../shared/events/base'
4
+
5
+ module Wizrb
6
+ module Lighting
7
+ module Events
8
+ class SetBrightnessEvent < Wizrb::Shared::Events::Base
9
+ MIN_VALUE = 10
10
+ MAX_VALUE = 100
11
+
12
+ def initialize(value)
13
+ validate!(value)
14
+ super(method: 'setState', params: { dimming: value.to_i })
15
+ end
16
+
17
+ private
18
+
19
+ def validate!(value)
20
+ return if value && value >= MIN_VALUE && value <= MAX_VALUE
21
+
22
+ raise ArgumentError, "Brightness must be an integer between #{MIN_VALUE} and #{MAX_VALUE}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../shared/events/base'
4
+
5
+ module Wizrb
6
+ module Lighting
7
+ module Events
8
+ class SetColdWhiteEvent < Wizrb::Shared::Events::Base
9
+ MIN_VALUE = 1
10
+ MAX_VALUE = 255
11
+
12
+ def initialize(value)
13
+ validate!(value)
14
+ super(method: 'setState', params: { c: value })
15
+ end
16
+
17
+ private
18
+
19
+ def validate!(value)
20
+ return if value && value >= MIN_VALUE && value <= MAX_VALUE
21
+
22
+ raise ArgumentError, "Cold white must be between #{MIN_VALUE} and #{MAX_VALUE}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../shared/events/base'
4
+
5
+ module Wizrb
6
+ module Lighting
7
+ module Events
8
+ class SetColorTempEvent < Wizrb::Shared::Events::Base
9
+ MIN_VALUE = 1000
10
+ MAX_VALUE = 12_000
11
+
12
+ def initialize(value)
13
+ validate!(value)
14
+ super(method: 'setState', params: { temp: value })
15
+ end
16
+
17
+ private
18
+
19
+ def validate!(value)
20
+ if !value || value < MIN_VALUE || value > MAX_VALUE
21
+ raise ArgumentError, "Temperature must be between #{MIN_VALUE} and #{MAX_VALUE} kelvin"
22
+ end
23
+
24
+ raise ArgumentError, 'Temperature must be divisible by 100' unless value % 100 == 0
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end