light_me_up 0.2.1 → 0.2.5

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: 94afbedabf39a83db771b9a256796bd9e13cfe80e5abdca791ae7b159e43dfeb
4
- data.tar.gz: 5d5fd8d91160cb31c185811381c01d78519f8a14d7aec8a406232216d4737a9d
3
+ metadata.gz: a8d795fa88a021f974ba2ef80b74326b1222f1f41a20ba71dbd4ea3be643f468
4
+ data.tar.gz: 92aec84e39354826ec8bc195e8286bd073b414f96bfe4c51ad437f3406f0972f
5
5
  SHA512:
6
- metadata.gz: c860756fc6ffca76099547d55ec864b38c33e43513d357731e2c724e77ad2b04a1f48d6d7fdeb6ad38ead151046a6f80bd47c8f83618f23d08b2dc22204eb78f
7
- data.tar.gz: d74ae4c379dae3c76d76f4fc4bc936b628999d137915f07c2f64bf55c491893b6b9bea859a9e8df79dbe97590fe965d7aed4c3f8469d3d578be93ecc9e8a71b7
6
+ metadata.gz: 2f7a7b3c45989710a289136eb7ef2f2bc6b2999bfa8951679bad760b7c7d3bae01e713ac57e57a365b113439f7cf783c201283c4f210e4917710b9b024518dda
7
+ data.tar.gz: 956f1e24553299defa116ff3107eab99d285ce02c44378d0f1ed436fefd334d4a39cec8e7c34da0c0846a5a36f7b039f55c3de8dcea6b5fdb4c7150da1487c19
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- light_me_up (0.2.1)
4
+ light_me_up (0.2.5)
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::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,25 @@ 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
+ scale_ratio = dest_range.size.to_f / src_range.size.to_f
42
+ (dest_range.min + scale_ratio * (value - src_range.min)).round
43
+ end
29
44
  end
30
45
  end
31
46
  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,23 +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
- end
21
-
22
- if settings_options.any?
23
- api_client.set(**settings_options)
20
+ api_client.toggle(**settings_options)
21
+ elsif settings_options.any?
22
+ api_client.update(**settings_options)
24
23
  else
25
- raise NoOptionsGiven, "provide at least one option"
24
+ raise InvalidOptions, "At least one option must be provided"
26
25
  end
27
26
  end
28
-
29
- private
30
-
31
- def settings_options
32
- @settings_options ||= options.slice(:on, :brightness, :temperature)
33
- end
34
27
  end
35
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LightMeUp
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.5"
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.1
4
+ version: 0.2.5
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