enphase-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e8f4ce942ac46d32bb544eab1d0df466f948f989bf6860a17ea64e92125db96
4
+ data.tar.gz: acf020736382a8e3678ace7fd43f431a763e66b47a54bf012e9eb16b5cae77f3
5
+ SHA512:
6
+ metadata.gz: 673e424dcada9328bed969184b1b36147f9e6bef4e7e20fe3c4ec189f4b58cf02956324ae01f505d5524a99bd7e221ed4a9633d2b65694b005b2c291c8d4199d
7
+ data.tar.gz: 750d1a2605e6c7c86e680e1f7a7494519050888f7221505efd1af2c3ed745b2cc7c5c004ab434eb8971215710a9a1ead0ac3c014309e1acb5e4bb021a7a022cb
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in cocina-models.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ enphase-client (0.0.1)
5
+ faraday
6
+ faraday_middleware
7
+ json
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ faraday (1.9.3)
13
+ faraday-em_http (~> 1.0)
14
+ faraday-em_synchrony (~> 1.0)
15
+ faraday-excon (~> 1.1)
16
+ faraday-httpclient (~> 1.0)
17
+ faraday-multipart (~> 1.0)
18
+ faraday-net_http (~> 1.0)
19
+ faraday-net_http_persistent (~> 1.0)
20
+ faraday-patron (~> 1.0)
21
+ faraday-rack (~> 1.0)
22
+ faraday-retry (~> 1.0)
23
+ ruby2_keywords (>= 0.0.4)
24
+ faraday-em_http (1.0.0)
25
+ faraday-em_synchrony (1.0.0)
26
+ faraday-excon (1.1.0)
27
+ faraday-httpclient (1.0.1)
28
+ faraday-multipart (1.0.3)
29
+ multipart-post (>= 1.2, < 3)
30
+ faraday-net_http (1.0.1)
31
+ faraday-net_http_persistent (1.2.0)
32
+ faraday-patron (1.0.0)
33
+ faraday-rack (1.0.0)
34
+ faraday-retry (1.0.3)
35
+ faraday_middleware (1.2.0)
36
+ faraday (~> 1.0)
37
+ json (2.6.1)
38
+ multipart-post (2.1.1)
39
+ ruby2_keywords (0.0.5)
40
+
41
+ PLATFORMS
42
+ x86_64-darwin-18
43
+ x86_64-darwin-19
44
+
45
+ DEPENDENCIES
46
+ enphase-client!
47
+
48
+ BUNDLED WITH
49
+ 2.3.4
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2022 Peter Mangiafico
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # Enlighten Ruby
2
+
3
+ This is a lightweight Ruby wrapper around the Enphase Enlighten API available at https://developer.enphase.com/
4
+
5
+ I am not affiliated with Enphase, this is just a convenience wrapper around their published API to make it easy to use in a Ruby app. To use it, you will need to register an app to obtain an API key (or use your own existing API key). You will also need to get authorization to connect a user's system (even if your own) using the instructions provided at https://developer.enphase.com/docs/quickstart.html
6
+
7
+ ## Usage
8
+
9
+ 1. Add the gem to your Gemfile.
10
+ 2. Require if neeed.
11
+ 3. Once you have an API key and an authorized user ID, you can instantiate the client and make API calls. You will usually need the system ID you are interested in for most calls. There is a call to fetch system IDs that you have access to. Responses are ruby hash objects.
12
+ 4. Dates returned by the API can be in one of two formats, UNIX Epoch or ISO8601. The client defaults to ISO8601, but you can specify epoch if you want when instantiating the client.
13
+
14
+ In Gemfile:
15
+ ```
16
+ gem 'enphase-client'
17
+ ```
18
+
19
+ In app:
20
+ ```
21
+ require 'enphase-client'
22
+ client = Enphase::Client.new(api_key: YOUR_API_KEY, user_id: AUTHORIZED_USER_ID)
23
+
24
+ OR
25
+
26
+ client = Enphase::Client.new(api_key: YOUR_API_KEY, user_id: AUTHORIZED_USER_ID, datetime_format: 'epoch') # for epoch datetimes
27
+
28
+ result = client.stats(system_id: SYSTEM_ID)
29
+ result
30
+ =>
31
+ 3.0.3 :006 > c.stats(system_id: '2599884')
32
+ =>
33
+ {"system_id"=>2599884,
34
+ "total_devices"=>19,
35
+ "intervals"=>
36
+ [{"end_at"=>"2022-01-21T07:20:00-08:00", "devices_reporting"=>19, "powr"=>3, "enwh"=>0},
37
+ {"end_at"=>"2022-01-21T07:25:00-08:00", "devices_reporting"=>19, "powr"=>7, "enwh"=>1},
38
+ {"end_at"=>"2022-01-21T07:30:00-08:00", "devices_reporting"=>19, "powr"=>29, "enwh"=>2},
39
+ {"end_at"=>"2022-01-21T07:35:00-08:00", "devices_reporting"=>19, "powr"=>46, "enwh"=>4},
40
+ {"end_at"=>"2022-01-21T07:40:00-08:00", "devices_reporting"=>19, "powr"=>75, "enwh"=>6},
41
+ {"end_at"=>"2022-01-21T07:45:00-08:00", "devices_reporting"=>19, "powr"=>98, "enwh"=>8},
42
+ {"end_at"=>"2022-01-21T07:50:00-08:00", "devices_reporting"=>19, "powr"=>119, "enwh"=>10},
43
+ {"end_at"=>"2022-01-21T07:55:00-08:00", "devices_reporting"=>19, "powr"=>132, "enwh"=>11},
44
+ {"end_at"=>"2022-01-21T08:00:00-08:00", "devices_reporting"=>19, "powr"=>151, "enwh"=>13},
45
+ {"end_at"=>"2022-01-21T08:05:00-08:00", "devices_reporting"=>19, "powr"=>169, "enwh"=>14},
46
+ {"end_at"=>"2022-01-21T08:10:00-08:00", "devices_reporting"=>19, "powr"=>187, "enwh"=>16},
47
+ {"end_at"=>"2022-01-21T08:15:00-08:00", "devices_reporting"=>19, "powr"=>214, "enwh"=>18},
48
+ {"end_at"=>"2022-01-21T08:20:00-08:00", "devices_reporting"=>19, "powr"=>233, "enwh"=>19},
49
+ {"end_at"=>"2022-01-21T08:25:00-08:00", "devices_reporting"=>19, "powr"=>252, "enwh"=>21},
50
+ {"end_at"=>"2022-01-21T08:30:00-08:00", "devices_reporting"=>19, "powr"=>282, "enwh"=>24},
51
+ {"end_at"=>"2022-01-21T08:35:00-08:00", "devices_reporting"=>19, "powr"=>321, "enwh"=>27},
52
+ {"end_at"=>"2022-01-21T08:40:00-08:00", "devices_reporting"=>19, "powr"=>380, "enwh"=>32},
53
+ {"end_at"=>"2022-01-21T08:45:00-08:00", "devices_reporting"=>19, "powr"=>460, "enwh"=>38},
54
+ {"end_at"=>"2022-01-21T08:50:00-08:00", "devices_reporting"=>19, "powr"=>560, "enwh"=>47},
55
+ {"end_at"=>"2022-01-21T08:55:00-08:00", "devices_reporting"=>19, "powr"=>654, "enwh"=>55},
56
+ {"end_at"=>"2022-01-21T09:00:00-08:00", "devices_reporting"=>19, "powr"=>720, "enwh"=>60},
57
+ {"end_at"=>"2022-01-21T09:05:00-08:00", "devices_reporting"=>19, "powr"=>760, "enwh"=>63},
58
+ {"end_at"=>"2022-01-21T09:10:00-08:00", "devices_reporting"=>19, "powr"=>807, "enwh"=>67},
59
+ {"end_at"=>"2022-01-21T09:15:00-08:00", "devices_reporting"=>19, "powr"=>852, "enwh"=>71},
60
+ {"end_at"=>"2022-01-21T09:20:00-08:00", "devices_reporting"=>19, "powr"=>865, "enwh"=>72},
61
+ {"end_at"=>"2022-01-21T09:25:00-08:00", "devices_reporting"=>19, "powr"=>846, "enwh"=>71},
62
+ {"end_at"=>"2022-01-21T09:30:00-08:00", "devices_reporting"=>19, "powr"=>847, "enwh"=>71},
63
+ {"end_at"=>"2022-01-21T09:35:00-08:00", "devices_reporting"=>19, "powr"=>936, "enwh"=>78},
64
+ {"end_at"=>"2022-01-21T09:40:00-08:00", "devices_reporting"=>19, "powr"=>1054, "enwh"=>88},
65
+ {"end_at"=>"2022-01-21T09:45:00-08:00", "devices_reporting"=>19, "powr"=>1164, "enwh"=>97},
66
+ {"end_at"=>"2022-01-21T09:50:00-08:00", "devices_reporting"=>19, "powr"=>1315, "enwh"=>110},
67
+ {"end_at"=>"2022-01-21T09:55:00-08:00", "devices_reporting"=>19, "powr"=>1464, "enwh"=>122},
68
+ {"end_at"=>"2022-01-21T10:00:00-08:00", "devices_reporting"=>19, "powr"=>1587, "enwh"=>132},
69
+ {"end_at"=>"2022-01-21T10:05:00-08:00", "devices_reporting"=>19, "powr"=>1759, "enwh"=>147},
70
+ {"end_at"=>"2022-01-21T10:10:00-08:00", "devices_reporting"=>19, "powr"=>1967, "enwh"=>164},
71
+ {"end_at"=>"2022-01-21T10:15:00-08:00", "devices_reporting"=>19, "powr"=>2107, "enwh"=>176},
72
+ {"end_at"=>"2022-01-21T10:20:00-08:00", "devices_reporting"=>19, "powr"=>2216, "enwh"=>185},
73
+ {"end_at"=>"2022-01-21T10:25:00-08:00", "devices_reporting"=>19, "powr"=>2364, "enwh"=>197},
74
+ {"end_at"=>"2022-01-21T10:30:00-08:00", "devices_reporting"=>19, "powr"=>2535, "enwh"=>211},
75
+ {"end_at"=>"2022-01-21T10:35:00-08:00", "devices_reporting"=>19, "powr"=>2645, "enwh"=>220},
76
+ {"end_at"=>"2022-01-21T10:40:00-08:00", "devices_reporting"=>19, "powr"=>2685, "enwh"=>224},
77
+ {"end_at"=>"2022-01-21T10:45:00-08:00", "devices_reporting"=>19, "powr"=>2802, "enwh"=>234},
78
+ {"end_at"=>"2022-01-21T10:50:00-08:00", "devices_reporting"=>19, "powr"=>2944, "enwh"=>245},
79
+ {"end_at"=>"2022-01-21T10:55:00-08:00", "devices_reporting"=>19, "powr"=>2988, "enwh"=>249},
80
+ {"end_at"=>"2022-01-21T11:00:00-08:00", "devices_reporting"=>19, "powr"=>2997, "enwh"=>250},
81
+ {"end_at"=>"2022-01-21T11:05:00-08:00", "devices_reporting"=>19, "powr"=>3070, "enwh"=>256},
82
+ {"end_at"=>"2022-01-21T11:10:00-08:00", "devices_reporting"=>19, "powr"=>3144, "enwh"=>262}],
83
+ "meta"=>
84
+ {"status"=>"normal",
85
+ "last_report_at"=>"2022-01-21T11:16:29-08:00",
86
+ "last_energy_at"=>"2022-01-21T11:12:58-08:00",
87
+ "operational_at"=>"2014-01-01T10:29:53-08:00"}}
88
+ ```
89
+
90
+ ## Available API calls in the client
91
+
92
+ These are just mapped 1:1 for the calls shown at https://developer.enphase.com/docs
93
+
94
+ If the call succeeds, you will get a Ruby hash out that maps exactly the JSON response described in the documents.
95
+ If the call fails, you will get a `Enphase::ClientError` error raised with a status code and message. You should handle this exception in your app to provide error handling.
96
+
97
+ If optional parameters can be passed for these API calls, you can add them has keyword arguments to the method (a couple examples shown below). Note that the `start_at` and `end_at` interval parameters that can be passed to many of the
98
+ calls are UNIX epoch times, regardless of the return date format specified. You can convert ruby dates to UNIX epoch:
99
+
100
+ ```
101
+ require 'time'
102
+ puts Time.parse('2022-01-15T10:20:00-08:00').to_i
103
+ => 1642270800
104
+ ```
105
+
106
+ 1. Stats: https://developer.enphase.com/docs#stats
107
+
108
+ ```
109
+ result = client.stats(system_id: SYSTEM_ID)
110
+
111
+ # with optional intervals parameters
112
+ result = client.stats(system_id: SYSTEM_ID, start_at: '1642270800', end_at: '1642278000')
113
+
114
+ ```
115
+
116
+ 2. Summary: https://developer.enphase.com/docs#summary
117
+
118
+ ```
119
+ result = client.summary(system_id: SYSTEM_ID)
120
+ ```
121
+
122
+ 3. Inverters summary: https://developer.enphase.com/docs#inverters_summary_by_envoy_or_site
123
+
124
+ ```
125
+ result = client.inverters(system_id: SYSTEM_ID)
126
+ ```
127
+
128
+ 4. Systems: https://developer.enphase.com/docs#index
129
+
130
+ ```
131
+ result = client.systems
132
+ ```
133
+
134
+ ## License
135
+
136
+ This code is provided under an MIT License. https://opensource.org/licenses/MIT
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'enphase/client'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'enphase-client'
3
+ s.version = '0.0.1'
4
+ s.summary = "A Ruby gem wrapper for the Enphase Enlighten Systems API"
5
+ s.description = "See https://developer.enphase.com/ for information on how to get an API key and authorize access for a user."
6
+ s.authors = ["Peter Mangiafico"]
7
+ s.email = 'peter@mangiafico.org'
8
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
9
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
10
+ end
11
+ s.add_dependency 'faraday'
12
+ s.add_dependency 'faraday_middleware'
13
+ s.add_dependency 'json'
14
+
15
+ s.homepage = 'https://rubygems.org/gems/enphase-client'
16
+ s.license = 'MIT'
17
+ end
@@ -0,0 +1,63 @@
1
+ require 'faraday'
2
+ require 'faraday/net_http'
3
+ require 'faraday_middleware'
4
+ require 'json'
5
+
6
+ module Enphase
7
+ class ClientError < StandardError; end
8
+ end
9
+
10
+ module Enphase
11
+ class Client
12
+ attr_reader :api_key, :user_id, :datetime_format
13
+
14
+ # datetime format can be 'iso8601' or 'epoch', defaults to 'iso8601'
15
+ def initialize(api_key:, user_id:, datetime_format: 'iso8601')
16
+ @api_key = api_key
17
+ @user_id = user_id
18
+ @datetime_format = datetime_format
19
+ end
20
+
21
+ # Stats Endpoint
22
+ def stats(system_id:, **params)
23
+ get_json(url: "systems/#{system_id}/stats", params: params)
24
+ end
25
+
26
+ # Summary Endpoint
27
+ def summary(system_id:, **params)
28
+ get_json(url: "systems/#{system_id}/summary", params: params)
29
+ end
30
+
31
+ # Inverters Summary Endpoint
32
+ def inverters(site_id:)
33
+ get_json(url: 'systems/inverters_summary_by_envoy_or_site', params: {site_id: site_id})
34
+ end
35
+
36
+ # Index Endpoint
37
+ def systems(**params)
38
+ get_json(url: 'systems', params: params)
39
+ end
40
+
41
+ private
42
+ def get_json(url:, params: {})
43
+ conn = Faraday.new(
44
+ url: "#{base_url}/#{url}",
45
+ params: params.merge({key: api_key, user_id: user_id, datetime_format: datetime_format})
46
+ ) do |f|
47
+ f.request :json # encode req bodies as JSON and automatically set the Content-Type header
48
+ f.response :json # decode response bodies as JSON
49
+ f.adapter :net_http # adds the adapter to the connection
50
+ end
51
+ response = conn.get
52
+ if response.success?
53
+ response.body
54
+ else
55
+ raise Enphase::ClientError.new("API returned status #{response.status}: #{response.reason_phrase}")
56
+ end
57
+ end
58
+
59
+ def base_url
60
+ 'https://api.enphaseenergy.com/api/v2'
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enphase-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Mangiafico
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: See https://developer.enphase.com/ for information on how to get an API
56
+ key and authorize access for a user.
57
+ email: peter@mangiafico.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE
65
+ - README.md
66
+ - bin/console
67
+ - bin/setup
68
+ - enphase-client.gemspec
69
+ - lib/enphase/client.rb
70
+ homepage: https://rubygems.org/gems/enphase-client
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.3.6
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A Ruby gem wrapper for the Enphase Enlighten Systems API
93
+ test_files: []