Class: SmaApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sma_api/client.rb

Overview

Ruby client for communicating with SMA Inverter web interface

Instance Method Summary collapse

Constructor Details

#initialize(host:, password:, sid: nil) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
# File 'lib/sma_api/client.rb', line 6

def initialize(host:, password:, sid: nil)
  @host = host
  @password = password
  @client = Http.new(host:, password:, sid:)
end

Instance Method Details

#destroy_sessionString

Logout and clear the session. This is the same as using Logout from the web interface

Returns:

  • (String)

    An empty session id



22
23
24
# File 'lib/sma_api/client.rb', line 22

def destroy_session
  @client.destroy_session
end

#download(path, target) ⇒ Object

Download the file specified by url and store it in a file on the local file system.

Parameters:

  • url (String)

    URL without /fs prefix. It should start with a ‘/’

  • path (String)

    Path of the local file



60
61
62
# File 'lib/sma_api/client.rb', line 60

def download(path, target)
  @client.download(path, target)
end

#get_fs(path) ⇒ Array

Retrieve list of files and directories for the path.

Returns:

  • (Array)

    List of directories and files



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sma_api/client.rb', line 42

def get_fs(path)
  result = @client.post('/dyn/getFS.json', { destDev: [], path: })

  result['result'].first[1][path].map do |f|
    type = f.key?('f') ? 'f' : 'd'
    {
      name: f['d'] || f['f'],
      type:,
      last_modified: Time.at(f['tm']),
      size: f['s']
    }
  end
end

#get_values(keys) ⇒ Hash

Retrieve values specified by the keys using getValues.json endpoint.

Parameters:

  • keys (Array<String>)

    List of keys

Returns:

  • (Hash)

    Key-value pairs



30
31
32
33
34
35
36
37
# File 'lib/sma_api/client.rb', line 30

def get_values(keys)
  result = @client.post('/dyn/getValues.json', { destDev: [], keys: })
  return nil unless result['result']

  keys.each_with_object({}) do |k, h|
    h[k] = get_value(k, result)
  end
end

#object_metadataArray

ObjectMetadata_Istl endpoint

Returns:

  • (Array)

    List of available object metadata



67
68
69
# File 'lib/sma_api/client.rb', line 67

def 
  @client.post('/data/ObjectMetadata_Istl.json')
end

#sidString

The current session id. If empty, it will create a new session

Returns:

  • (String)

    the session id that will be used in subsequent requests.



15
16
17
# File 'lib/sma_api/client.rb', line 15

def sid
  @client.sid
end