class Switchbot::Client

Constants

API_ENDPOINT

Public Class Methods

new(token) click to toggle source
# File lib/switchbot/client.rb, line 7
def initialize(token)
  @token = token
end

Public Instance Methods

bot(device_id) click to toggle source
# File lib/switchbot/client.rb, line 59
def bot(device_id)
  Bot.new(client: self, device_id: device_id)
end
commands(device_id:, command:, parameter: 'default', command_type: 'command') click to toggle source
# File lib/switchbot/client.rb, line 29
def commands(device_id:, command:, parameter: 'default', command_type: 'command')
  request(
    http_method: :post,
    endpoint: "/v1.0/devices/#{device_id}/commands",
    params: {
      command: command,
      parameter: parameter,
      commandType: command_type
    }
  )
end
device(device_id) click to toggle source
# File lib/switchbot/client.rb, line 18
def device(device_id)
  Device.new(client: self, device_id: device_id)
end
devices() click to toggle source
# File lib/switchbot/client.rb, line 11
def devices
  request(
    http_method: :get,
    endpoint: 'v1.0/devices'
  )
end
execute(scene_id:) click to toggle source
# File lib/switchbot/client.rb, line 52
def execute(scene_id:)
  request(
    http_method: :post,
    endpoint: "/v1.0/scenes/#{scene_id}/execute"
  )
end
humidifier(device_id) click to toggle source
# File lib/switchbot/client.rb, line 67
def humidifier(device_id)
  Humidifier.new(client: self, device_id: device_id)
end
light(device_id) click to toggle source
# File lib/switchbot/client.rb, line 63
def light(device_id)
  Light.new(client: self, device_id: device_id)
end
scene(scene_id) click to toggle source
# File lib/switchbot/client.rb, line 48
def scene(scene_id)
  Scene.new(client: self, scene_id: scene_id)
end
scenes() click to toggle source
# File lib/switchbot/client.rb, line 41
def scenes
  request(
    http_method: :get,
    endpoint: 'v1.0/scenes'
  )
end
status(device_id:) click to toggle source
# File lib/switchbot/client.rb, line 22
def status(device_id:)
  request(
    http_method: :get,
    endpoint: "/v1.0/devices/#{device_id}/status"
  )
end

Private Instance Methods

connection() click to toggle source
# File lib/switchbot/client.rb, line 80
def connection
  Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
    conn.request :json
  end
end
headers() click to toggle source
# File lib/switchbot/client.rb, line 73
def headers
  {
    'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
    'Authorization' => @token
  }
end
request(http_method:, endpoint:, params: {}) click to toggle source
# File lib/switchbot/client.rb, line 86
def request(http_method:, endpoint:, params: {})
  response = connection.public_send(http_method, endpoint, params)
  JSON.parse(response.body).deep_transform_keys(&:underscore).deep_symbolize_keys
end