growatt 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.env.template +4 -4
- data/.gitignore +47 -47
- data/CHANGELOG.md +16 -14
- data/Gemfile +6 -6
- data/README.md +114 -83
- data/Rakefile +22 -19
- data/growatt.gemspec +37 -37
- data/lib/growatt/api.rb +59 -35
- data/lib/growatt/authorization.rb +91 -36
- data/lib/growatt/client.rb +219 -160
- data/lib/growatt/connection.rb +45 -26
- data/lib/growatt/const.rb +47 -29
- data/lib/growatt/error.rb +24 -12
- data/lib/growatt/pagination.rb +32 -26
- data/lib/growatt/version.rb +5 -5
- data/lib/growatt.rb +62 -27
- metadata +4 -14
- data/test/auth_test.rb +0 -20
- data/test/client_test.rb +0 -69
- data/test/test_helper.rb +0 -23
data/lib/growatt/error.rb
CHANGED
@@ -1,12 +1,24 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
#
|
7
|
-
class
|
8
|
-
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Growatt
|
4
|
+
|
5
|
+
# Base error class for all Growatt-related exceptions.
|
6
|
+
# Allows rescuing all Growatt-specific errors with `Growatt::GrowattError`.
|
7
|
+
class GrowattError < StandardError; end
|
8
|
+
|
9
|
+
# Raised when there is a configuration issue, such as missing or invalid settings.
|
10
|
+
#
|
11
|
+
# @example Raising a configuration error
|
12
|
+
# raise Growatt::ConfigurationError, "Invalid API endpoint"
|
13
|
+
class ConfigurationError < GrowattError; end
|
14
|
+
|
15
|
+
# Raised when an authentication attempt fails.
|
16
|
+
#
|
17
|
+
# @example Handling authentication failure
|
18
|
+
# begin
|
19
|
+
# client.login
|
20
|
+
# rescue Growatt::AuthenticationError => e
|
21
|
+
# puts "Login failed: #{e.message}"
|
22
|
+
# end
|
23
|
+
class AuthenticationError < GrowattError; end
|
24
|
+
end
|
data/lib/growatt/pagination.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Growatt
|
7
|
+
# Provides pagination handling for API requests.
|
8
|
+
module RequestPagination
|
9
|
+
# Custom data pager for processing API responses.
|
10
|
+
# Inherits from `WrAPI::RequestPagination::DefaultPager` and extracts relevant data from responses.
|
11
|
+
class DataPager < WrAPI::RequestPagination::DefaultPager
|
12
|
+
# Extracts the relevant data from the API response body.
|
13
|
+
#
|
14
|
+
# @param body [String, Hash] The response body from an API request.
|
15
|
+
# @return [Hash] The parsed response data.
|
16
|
+
#
|
17
|
+
# @example Extracting data from a response
|
18
|
+
# response = { "back" => { "items" => [...] } }
|
19
|
+
# Growatt::RequestPagination::DataPager.data(response)
|
20
|
+
# # => { "items" => [...] }
|
21
|
+
def self.data(body)
|
22
|
+
# If the body is a Hash, return the 'back' key if it exists
|
23
|
+
if body.is_a? Hash
|
24
|
+
body['back'] || body
|
25
|
+
else
|
26
|
+
# If the body is a String, parse it as JSON
|
27
|
+
JSON.parse(body)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/growatt/version.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Growatt
|
4
|
-
VERSION = '0.2.
|
5
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Growatt
|
4
|
+
VERSION = '0.2.1'
|
5
|
+
end
|
data/lib/growatt.rb
CHANGED
@@ -1,27 +1,62 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require File.expand_path('growatt/
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wrapi'
|
4
|
+
require File.expand_path('growatt/client', __dir__)
|
5
|
+
require File.expand_path('growatt/version', __dir__)
|
6
|
+
require File.expand_path('growatt/pagination', __dir__)
|
7
|
+
|
8
|
+
# The `Growatt` module provides a Ruby client for interacting with the Growatt API.
|
9
|
+
#
|
10
|
+
# It extends `WrAPI::Configuration` for managing API settings and `WrAPI::RespondTo`
|
11
|
+
# for handling API responses dynamically.
|
12
|
+
#
|
13
|
+
# The module allows you to create a client instance, configure API endpoints, and manage pagination settings.
|
14
|
+
#
|
15
|
+
# @example Creating a Growatt API client:
|
16
|
+
# client = Growatt.client(api_key: "your_api_key")
|
17
|
+
#
|
18
|
+
module Growatt
|
19
|
+
extend WrAPI::Configuration
|
20
|
+
extend WrAPI::RespondTo
|
21
|
+
|
22
|
+
# Default User-Agent string for API requests.
|
23
|
+
DEFAULT_UA = "Ruby Growatt API client #{Growatt::VERSION}".freeze
|
24
|
+
|
25
|
+
# Default API endpoint for Growatt services.
|
26
|
+
#
|
27
|
+
# Note: `https://openapi.growatt.com/` is an alternative, but it does not work with some accounts.
|
28
|
+
DEFAULT_ENDPOINT = 'https://server.growatt.com/'
|
29
|
+
|
30
|
+
# Default pagination class used for handling paginated API responses.
|
31
|
+
DEFAULT_PAGINATION = RequestPagination::DataPager
|
32
|
+
|
33
|
+
# Initializes and returns a new `Growatt::Client` instance.
|
34
|
+
#
|
35
|
+
# @param options [Hash] Configuration options for the client.
|
36
|
+
# @option options [String] :user_agent Custom user agent string.
|
37
|
+
# @option options [String] :endpoint Custom API endpoint.
|
38
|
+
# @option options [Class] :pagination_class Custom pagination class.
|
39
|
+
#
|
40
|
+
# @return [Growatt::Client] A new API client instance.
|
41
|
+
#
|
42
|
+
# @example Creating a client with custom options:
|
43
|
+
# client = Growatt.client(user_agent: "MyCustomClient/1.0")
|
44
|
+
def self.client(options = {})
|
45
|
+
Growatt::Client.new({
|
46
|
+
user_agent: DEFAULT_UA,
|
47
|
+
endpoint: DEFAULT_ENDPOINT,
|
48
|
+
pagination_class: DEFAULT_PAGINATION
|
49
|
+
}.merge(options))
|
50
|
+
end
|
51
|
+
|
52
|
+
# Resets the Growatt configuration to default values.
|
53
|
+
#
|
54
|
+
# This method restores the API endpoint, user agent, and pagination settings to their default values.
|
55
|
+
def self.reset
|
56
|
+
super
|
57
|
+
self.endpoint = nil
|
58
|
+
self.user_agent = DEFAULT_UA
|
59
|
+
self.endpoint = DEFAULT_ENDPOINT
|
60
|
+
self.pagination_class = DEFAULT_PAGINATION
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: growatt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janco Tanis
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-03-19 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: wrapi
|
@@ -94,7 +93,6 @@ dependencies:
|
|
94
93
|
- - ">="
|
95
94
|
- !ruby/object:Gem::Version
|
96
95
|
version: '0'
|
97
|
-
description:
|
98
96
|
email: gems@jancology.com
|
99
97
|
executables: []
|
100
98
|
extensions: []
|
@@ -116,16 +114,12 @@ files:
|
|
116
114
|
- lib/growatt/error.rb
|
117
115
|
- lib/growatt/pagination.rb
|
118
116
|
- lib/growatt/version.rb
|
119
|
-
- test/auth_test.rb
|
120
|
-
- test/client_test.rb
|
121
|
-
- test/test_helper.rb
|
122
117
|
homepage: https://rubygems.org/gems/growatt
|
123
118
|
licenses:
|
124
119
|
- MIT
|
125
120
|
metadata:
|
126
121
|
homepage_uri: https://rubygems.org/gems/growatt
|
127
122
|
source_code_uri: https://github.com/jancotanis/growatt
|
128
|
-
post_install_message:
|
129
123
|
rdoc_options: []
|
130
124
|
require_paths:
|
131
125
|
- lib
|
@@ -140,11 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
134
|
- !ruby/object:Gem::Version
|
141
135
|
version: '0'
|
142
136
|
requirements: []
|
143
|
-
rubygems_version: 3.
|
144
|
-
signing_key:
|
137
|
+
rubygems_version: 3.6.2
|
145
138
|
specification_version: 4
|
146
139
|
summary: A Ruby wrapper for the Growatt APIs (readonly)
|
147
|
-
test_files:
|
148
|
-
- test/auth_test.rb
|
149
|
-
- test/client_test.rb
|
150
|
-
- test/test_helper.rb
|
140
|
+
test_files: []
|
data/test/auth_test.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'dotenv'
|
2
|
-
require 'logger'
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
|
6
|
-
describe 'authentication' do
|
7
|
-
|
8
|
-
it '#1 use wrong username/password' do
|
9
|
-
assert_raises Growatt::AuthenticationError do
|
10
|
-
client = Growatt.client( { username: "xxx"+Growatt.username, password: Growatt.password } )
|
11
|
-
client.login
|
12
|
-
flunk( 'AuthenticationError expected' )
|
13
|
-
end
|
14
|
-
end
|
15
|
-
it '#2 use correct username/password' do
|
16
|
-
client = Growatt.client( { username: Growatt.username, password:Growatt.password } )
|
17
|
-
client.login
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
data/test/client_test.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'dotenv'
|
2
|
-
require 'logger'
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
def p m, o
|
6
|
-
# puts "#{m}: #{o.inspect}"
|
7
|
-
end
|
8
|
-
|
9
|
-
describe 'client' do
|
10
|
-
before do
|
11
|
-
@client = Growatt.client
|
12
|
-
@client.login
|
13
|
-
end
|
14
|
-
|
15
|
-
it '#1 GET info' do
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
it "#2 plant/device list" do
|
20
|
-
plants = @client.plant_list
|
21
|
-
|
22
|
-
p "\n* plants", plants
|
23
|
-
plant_id = plants.data.first.plantId
|
24
|
-
assert plant_id, "plant_id should not be nil"
|
25
|
-
|
26
|
-
detail = @client.plant_detail(plant_id)
|
27
|
-
p "\n* plant detail", detail
|
28
|
-
assert value(detail.plantData.plantId).must_equal(plant_id), 'correct plantId/structure'
|
29
|
-
|
30
|
-
plant_info = @client.plant_info(plant_id)
|
31
|
-
p "\n* plant info:", plant_info
|
32
|
-
|
33
|
-
devices = @client.device_list(plant_id)
|
34
|
-
p "\n* devices:", plant_info.deviceList
|
35
|
-
inverter = devices.first
|
36
|
-
# get data
|
37
|
-
data = @client.inverter_data(inverter.deviceSn,Growatt::Timespan::DAY,Time.now)
|
38
|
-
assert data, "Get day data by hour"
|
39
|
-
data = @client.inverter_data(inverter.deviceSn,Growatt::Timespan::MONTH,Time.now)
|
40
|
-
assert data, "Get month data by day"
|
41
|
-
data = @client.inverter_data(inverter.deviceSn,Growatt::Timespan::YEAR,Time.now)
|
42
|
-
assert data, "Get year data by month"
|
43
|
-
puts "\n* Inverter data:"
|
44
|
-
puts data.to_json
|
45
|
-
|
46
|
-
# turn device on
|
47
|
-
result = @client.turn_inverter(inverter.deviceSn,true)
|
48
|
-
p "\n* turnon result:", result
|
49
|
-
is_on = @client.inverter_on?(inverter.deviceSn)
|
50
|
-
|
51
|
-
assert is_on, "Inverter should be on"
|
52
|
-
assert result, "inverter on should be success"
|
53
|
-
end
|
54
|
-
it "#3 export limitation parameters" do
|
55
|
-
|
56
|
-
assert_raises ArgumentError do
|
57
|
-
@client.export_limit('xxxx', 4)
|
58
|
-
flunk( 'ArgumentError expected, invalid limtation' )
|
59
|
-
end
|
60
|
-
assert_raises ArgumentError do
|
61
|
-
@client.export_limit('xxxx', Growatt::ExportLimit::WATT)
|
62
|
-
flunk( 'ArgumentError expected, no value given for WATTs' )
|
63
|
-
end
|
64
|
-
assert_raises ArgumentError do
|
65
|
-
@client.export_limit('xxxx',Growatt::ExportLimit::PERCENTAGE)
|
66
|
-
flunk( 'ArgumentError expected, no value given for PERCENTAGEs' )
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'simplecov'
|
3
|
-
SimpleCov.start
|
4
|
-
|
5
|
-
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
6
|
-
require 'minitest/autorun'
|
7
|
-
require 'minitest/spec'
|
8
|
-
|
9
|
-
require 'dotenv'
|
10
|
-
require 'growatt'
|
11
|
-
|
12
|
-
Dotenv.load
|
13
|
-
|
14
|
-
TEST_LOGGER = 'test.log'
|
15
|
-
|
16
|
-
File.delete(TEST_LOGGER) if File.exist?(TEST_LOGGER)
|
17
|
-
|
18
|
-
Growatt.reset
|
19
|
-
Growatt.configure do |config|
|
20
|
-
config.username = ENV['GROWATT_USERNAME']
|
21
|
-
config.password = ENV['GROWATT_PASSWORD']
|
22
|
-
config.logger = Logger.new(TEST_LOGGER)
|
23
|
-
end
|