light_me_up 0.2.4 → 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: df08d345a1f2f4591c8b6b7584434d3a8db369297bc0b5ede94908a66238a300
4
- data.tar.gz: 5e894436da15580b8e6e93a8065442fe3c555b269205453957e3ae0fa336b0c2
3
+ metadata.gz: a8d795fa88a021f974ba2ef80b74326b1222f1f41a20ba71dbd4ea3be643f468
4
+ data.tar.gz: 92aec84e39354826ec8bc195e8286bd073b414f96bfe4c51ad437f3406f0972f
5
5
  SHA512:
6
- metadata.gz: 6af5bd3f911e5a204d9801a31240ee2d71367e025f9ae9989ff61917dee31168c14059cf3eb59578eb9f8a35306d918e72659d8a291134face9b4666dcf43e19
7
- data.tar.gz: d3ce9b5f7ff669835356bf6dca49702200d34ec7f50e8023aee2ba411e2d1bd5aaf34a563a518ee3e7c3b6d7291f1657248220ee526c9b46d79909b82162d949
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.3)
4
+ light_me_up (0.2.5)
5
5
  optimist (~> 3.0)
6
6
 
7
7
  GEM
data/bin/light-me-up CHANGED
@@ -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,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,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
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.4"
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.4
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-21 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