class YeelightCli::Bulb::ArgsValidator

The class contains all the params validations rubocop:disable ClassLength

Constants

BaseError
IncorrectArgumentError
WrongInitialDataFormatError

Public Class Methods

check_adjust_action!(action) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 69
def check_adjust_action!(action)
  return if action.to_sym.in?(%i[increase decrease circle])

  raise IncorrectArgumentError,
        'Action must be :increase, :decrease or :circle'
end
check_adjust_prop!(prop) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 76
def check_adjust_prop!(prop)
  return if prop.to_sym.in?(%i[bright ct color])

  raise IncorrectArgumentError, 'Adjust prop must be :bright, :ct or :color'
end
check_brightness!(brightness) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 27
def check_brightness!(brightness)
  return if (1..100).cover?(brightness)

  raise IncorrectArgumentError, 'Brightness must be in 1..100'
end
check_cf_action!(action) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 88
def check_cf_action!(action)
  return if (0..2).cover?(action)

  raise IncorrectArgumentError, 'Cf action must be in 0..2'
end
check_cf_brightness!(brightness) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 133
def check_cf_brightness!(brightness)
  return if (-1..100).cover?(brightness)

  raise IncorrectArgumentError, 'Brightness must be in -1..100'
end
check_cf_count!(count) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 139
def check_cf_count!(count)
  raise IncorrectArgumentError, 'Count must be >= 0' unless count >= 0
end
check_cf_duration!(duration) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 121
def check_cf_duration!(duration)
  return if duration.is_a?(Integer) && duration >= 50

  raise IncorrectArgumentError, 'Duration must be an integer >= 50'
end
check_cf_expression!(expression) click to toggle source

rubocop:disable MethodLength

# File lib/yeelight_cli/bulb/args_validator.rb, line 95
def check_cf_expression!(expression)
  if expression.blank?
    raise IncorrectArgumentError, 'Expression must not be blank'
  end

  exp_array = expression.is_a?(Array) ? expression : expression.split(',')

  if exp_array.count % 4 != 0
    raise IncorrectArgumentError,
          'Expression array must contain n*4 elements'
  end

  exp_array.each_slice(4) do |duration, mode, value, brightness|
    check_cf_slice!(duration, mode, value, brightness)
  end
end
check_cf_mode!(mode) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 127
def check_cf_mode!(mode)
  return if mode.in?([1, 2, 7])

  raise IncorrectArgumentError, 'Mode must be 1, 2 or 7'
end
check_cf_slice!(duration, mode, value, brightness) click to toggle source

rubocop:enable MethodLength

# File lib/yeelight_cli/bulb/args_validator.rb, line 113
def check_cf_slice!(duration, mode, value, brightness)
  check_cf_duration!(duration.to_i)
  check_cf_mode!(mode.to_i)
  check_rgb!(value.to_i) if mode == 1
  check_color_temperature!(value.to_i) if mode == 2
  check_cf_brightness!(brightness.to_i) if mode != 7
end
check_color_temperature!(color_temperature) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 39
def check_color_temperature!(color_temperature)
  return if (1700..6500).cover?(color_temperature)

  raise IncorrectArgumentError, 'Color temperature must be in 1700..6500'
end
check_duration!(duration) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 21
def check_duration!(duration)
  return if duration.is_a?(Integer) && !duration.negative?

  raise IncorrectArgumentError, 'Duration must be integer >= 0'
end
check_host!(host) click to toggle source

rubocop:disable RescueModifier

# File lib/yeelight_cli/bulb/args_validator.rb, line 150
def check_host!(host)
  return if (IPAddr.new(host) rescue nil).present?

  raise IncorrectArgumentError, 'Wrong host'
end
check_hue!(hue) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 45
def check_hue!(hue)
  return if (0..359).cover?(hue)

  raise IncorrectArgumentError, 'Hue must be in 0..359'
end
check_initial_data!(initial_data) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 11
def check_initial_data!(initial_data)
  raise WrongInitialDataFormatError unless initial_data.is_a?(Hash)

  raise WrongInitialDataFormatError if initial_data[:id].blank?

  raise WrongInitialDataFormatError if initial_data[:Location].blank?

  raise WrongInitialDataFormatError if initial_data[:support].blank?
end
check_music_action!(action) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 143
def check_music_action!(action)
  return if action.in?(%i[on off])

  raise IncorrectArgumentError, 'Action must be :on or :off'
end
check_percentage!(percentage) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 82
def check_percentage!(percentage)
  return if (-100..100).cover?(percentage) && !percentage.zero?

  raise IncorrectArgumentError, 'Percentage must be in -100..-1 1..100'
end
check_port!(port) click to toggle source

rubocop:enable RescueModifier

# File lib/yeelight_cli/bulb/args_validator.rb, line 157
def check_port!(port)
  return if (1..65_535).cover?(port)

  raise IncorrectArgumentError, 'Port must be in 1..65535'
end
check_power_state!(power_state) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 33
def check_power_state!(power_state)
  return if %i[on off].include?(power_state)

  raise IncorrectArgumentError, 'Power state must be :on or :off'
end
check_rgb!(rgb) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 57
def check_rgb!(rgb)
  return if (0x000001..0xffffff).cover?(rgb)

  raise IncorrectArgumentError, 'Rgb must be in 0x000001..0xffffff'
end
check_sat!(sat) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 51
def check_sat!(sat)
  return if (0..100).cover?(sat)

  raise IncorrectArgumentError, 'Sat must be in 0..100'
end
check_timeout!(minutes) click to toggle source
# File lib/yeelight_cli/bulb/args_validator.rb, line 63
def check_timeout!(minutes)
  return if (0..1440).cover?(minutes)

  raise IncorrectArgumentError, 'Minutes must be in 0..1440'
end