Skip to content

Commit ef7a9dc

Browse files
committed
Add method for POST /v1/systems/wallboxes/search
1 parent cb33382 commit ef7a9dc

5 files changed

Lines changed: 622 additions & 0 deletions

File tree

lib/senec/cloud/connection.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def wallbox(system_id, wallbox_id)
7070
get "#{WALLBOX_HOST}/v1/systems/#{system_id}/wallboxes/#{wallbox_id}"
7171
end
7272

73+
def wallbox_search(system_id)
74+
post "#{WALLBOX_HOST}/v1/systems/wallboxes/search", { systemIds: [system_id] }
75+
end
76+
7377
private
7478

7579
attr_accessor :oauth_token
@@ -139,6 +143,24 @@ def get(url, default: nil)
139143
# :nocov:
140144
end
141145

146+
def post(url, data, default: nil)
147+
return default unless ensure_token_valid
148+
149+
response = oauth_token.post(
150+
url,
151+
body: data.to_json,
152+
headers: { 'Content-Type' => 'application/json' },
153+
)
154+
return default unless response.status == 200
155+
156+
JSON.parse(response.body)
157+
rescue StandardError => e
158+
# :nocov:
159+
warn "API error: #{e.message}"
160+
default
161+
# :nocov:
162+
end
163+
142164
def http_request(method, url, data: nil)
143165
Faraday
144166
.new

spec/lib/senec/cloud/connection_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,44 @@
151151
end
152152
end
153153
end
154+
155+
describe '#wallbox_search' do
156+
subject(:wallbox_search) { connection.wallbox_search(system_id) }
157+
158+
context 'with valid system_id', vcr: 'cloud/wallbox_search' do
159+
let(:system_id) { ENV.fetch('SENEC_SYSTEM_ID') }
160+
161+
before { connection.authenticate! }
162+
163+
it { is_expected.to be_a(Array) }
164+
165+
it 'returns wallbox details' do
166+
expect(wallbox_search.first.keys).to include(
167+
'chargingCurrents',
168+
'chargingMode',
169+
'chargingPowerStats',
170+
'controllerId',
171+
'disconnected',
172+
'id',
173+
'isInterchargeAvailable',
174+
'isSolarChargingAvailable',
175+
'name',
176+
'productFamily',
177+
'prohibitUsage',
178+
'state',
179+
'type',
180+
)
181+
end
182+
end
183+
184+
context 'with invalid system_id', vcr: 'cloud/wallbox_search-invalid-id' do
185+
let(:system_id) { '12345' }
186+
187+
before { connection.authenticate! }
188+
189+
it 'returns nil' do
190+
expect(wallbox_search).to be_nil
191+
end
192+
end
193+
end
154194
end

0 commit comments

Comments
 (0)