light_me_up 0.2.2 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f637eabb588a28dd7f5582e8e750488a6ff28a857351b3cc382ce36a84885cac
4
- data.tar.gz: 69a40d452a72b23134fa62d56764f27f2a95a4e85120fd43aa0044a8023a3e22
3
+ metadata.gz: 39bab362a47cb5f1704bca0ee1d34a6e2324e3028f1d4ddd84f8d0481e1e760c
4
+ data.tar.gz: 6f397b96f59f316cb94ee1d8f0ba32a1e5cf6207b565e53ad4ce63c9d3395cb0
5
5
  SHA512:
6
- metadata.gz: 15b23d0d80fe5eb21bdbeaf1b525fb2882a66c09492d8701e54ca5d6f755e8049894341494556e97e1d6c95dad5af6ddd13a6a2515466ba2c18f2448ef1e57fe
7
- data.tar.gz: 0dd7599491f2157d6cc36fe473d2d4c8c9efb606a8eb277ab0933d7aa741ecc07f790c3b99d329b52098745f151e117a87fb938da4fd648541b548d38bb78c85
6
+ metadata.gz: 526ffbfeec68b717b54d04a0faaad6937c94254d92efe97adf6a77a19a77eb4a80e9d4d2d9d37394b7b67c9a6172d6b07f927b6ee2ac27b4dde9f854dfd9ecfb
7
+ data.tar.gz: 5a3e21a2acd5517ffa548411e47c1acf18e29f516d15434fef9e6e7020b48c72f5d3479950df93b1ae6b7a3d9fc8d69cf09830059ae6b115aab63791caecab52
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- light_me_up (0.2.2)
4
+ light_me_up (0.2.6)
5
5
  optimist (~> 3.0)
6
6
 
7
7
  GEM
data/bin/light-me-up CHANGED
@@ -30,10 +30,10 @@ end
30
30
  options = options.select { |_k, v| v }
31
31
  options[:on] = false if options.delete(:off)
32
32
  if options.delete(:defaults)
33
- options.reverse_merge!(
33
+ options = {
34
34
  brightness: ENV["ELGATO_DEFAULT_BRIGHTNESS"].to_i,
35
35
  temperature: ENV["ELGATO_DEFAULT_TEMPERATURE"].to_i
36
- )
36
+ }.merge(options)
37
37
  end
38
38
 
39
39
  api_client = LightMeUp::ApiClient.new(ip_address: options[:ip_address])
@@ -42,7 +42,8 @@ begin
42
42
  LightMeUp::SettingsUpdate.new(api_client, options).perform
43
43
  rescue LightMeUp::SettingsUpdate::ToggleIncompatible => e
44
44
  Optimist.die :toggle, e.message
45
- rescue LightMeUp::SettingsUpdate::NoOptionsGiven
45
+ rescue LightMeUp::SettingsUpdate::InvalidOptions => e
46
+ warn e.message
46
47
  Optimist.educate
47
48
  rescue LightMeUp::Error => e
48
49
  warn e.message
@@ -10,6 +10,8 @@ module LightMeUp
10
10
  OPEN_TIMEOUT = 2 # seconds
11
11
  READ_TIMEOUT = 2 # seconds
12
12
 
13
+ TEMPERATURE_RANGE = (143..344).freeze
14
+
13
15
  def initialize(ip_address:, port: DEFAULT_PORT)
14
16
  raise Error, "No ip_address specified." unless ip_address && ip_address != ""
15
17
 
@@ -22,14 +24,14 @@ module LightMeUp
22
24
  LightSerializer.deserialize(response).first
23
25
  end
24
26
 
25
- def toggle
27
+ def toggle(brightness: nil, temperature: nil)
26
28
  with_connection do |_http|
27
29
  current_status = status
28
30
 
29
31
  if current_status.on
30
- turn_light_off
32
+ update(on: false, brightness: brightness, temperature: temperature)
31
33
  else
32
- turn_light_on
34
+ update(on: true, brightness: brightness, temperature: temperature)
33
35
  end
34
36
  end
35
37
  end
@@ -5,7 +5,7 @@ module LightMeUp
5
5
  attr_reader :on, :brightness, :temperature
6
6
 
7
7
  BRIGHTNESS_RANGE = (0..100).freeze
8
- TEMPERATURE_RANGE = (143..344).freeze
8
+ TEMPERATURE_RANGE = (0..100).freeze
9
9
 
10
10
  class << self
11
11
  def max_brightness
@@ -13,7 +13,7 @@ module LightMeUp
13
13
  {
14
14
  on: on_as_int,
15
15
  brightness: light.brightness,
16
- temperature: light.temperature,
16
+ temperature: to_api_temperature(light.temperature),
17
17
  }.compact
18
18
  end
19
19
 
@@ -22,10 +22,27 @@ module LightMeUp
22
22
  Light.new(
23
23
  on: light["on"] == 1,
24
24
  brightness: light["brightness"],
25
- temperature: light["temperature"]
25
+ temperature: to_readable_temperature(light["temperature"])
26
26
  )
27
27
  end
28
28
  end
29
+
30
+ private
31
+
32
+ def to_readable_temperature(api_temperature)
33
+ scale_value(api_temperature, ApiClient::TEMPERATURE_RANGE, Light::TEMPERATURE_RANGE)
34
+ end
35
+
36
+ def to_api_temperature(readable_temperature)
37
+ scale_value(readable_temperature, Light::TEMPERATURE_RANGE, ApiClient::TEMPERATURE_RANGE)
38
+ end
39
+
40
+ def scale_value(value, src_range, dest_range)
41
+ return nil unless value
42
+
43
+ scale_ratio = dest_range.size.to_f / src_range.size.to_f
44
+ (dest_range.min + scale_ratio * (value - src_range.min)).round
45
+ end
29
46
  end
30
47
  end
31
48
  end
@@ -5,7 +5,7 @@ module LightMeUp
5
5
  attr_reader :api_client, :options
6
6
 
7
7
  class ToggleIncompatible < Error; end
8
- class NoOptionsGiven < Error; end
8
+ class InvalidOptions < Error; end
9
9
 
10
10
  def initialize(api_client, options)
11
11
  @api_client = api_client
@@ -13,21 +13,16 @@ module LightMeUp
13
13
  end
14
14
 
15
15
  def perform
16
- if options[:toggle]
17
- raise ToggleIncompatible, "is not compatible with setting other options" if settings_options.any?
16
+ settings_options = options.slice(:toggle, :on, :brightness, :temperature)
17
+ if settings_options.delete(:toggle)
18
+ raise ToggleIncompatible, "is not compatible with on or off" if settings_options.key?(:on)
18
19
 
19
- api_client.toggle
20
+ api_client.toggle(**settings_options)
20
21
  elsif settings_options.any?
21
- api_client.set(**settings_options)
22
+ api_client.update(**settings_options)
22
23
  else
23
- raise NoOptionsGiven, "provide at least one option"
24
+ raise InvalidOptions, "At least one option must be provided"
24
25
  end
25
26
  end
26
-
27
- private
28
-
29
- def settings_options
30
- @settings_options ||= options.slice(:on, :brightness, :temperature)
31
- end
32
27
  end
33
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LightMeUp
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_me_up
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter McCracken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-14 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: optimist