PhilipDaubmeier.SonnenClient
1.9.0
dotnet add package PhilipDaubmeier.SonnenClient --version 1.9.0
NuGet\Install-Package PhilipDaubmeier.SonnenClient -Version 1.9.0
<PackageReference Include="PhilipDaubmeier.SonnenClient" Version="1.9.0" />
paket add PhilipDaubmeier.SonnenClient --version 1.9.0
#r "nuget: PhilipDaubmeier.SonnenClient, 1.9.0"
// Install PhilipDaubmeier.SonnenClient as a Cake Addin #addin nuget:?package=PhilipDaubmeier.SonnenClient&version=1.9.0 // Install PhilipDaubmeier.SonnenClient as a Cake Tool #tool nuget:?package=PhilipDaubmeier.SonnenClient&version=1.9.0
SonnenClient
SonnenClient is a .NET Core class library with a wrapper for the Sonnen Portal RESTful JSON interface. It encapsulates all authentication, retry and parsing logic and provides a strongly typed method interface.
NuGet
PM> Install-Package PhilipDaubmeier.SonnenClient
Usage
You have to implement the interface ISonnenConnectionProvider
to provide the SonnenPortalClient
with all information necessary to authenticate at the API and establish a connection or use the existing SonnenConnectionProvider
.
The minimal viable example for playing around with the client would be as follows:
var sonnenConnProvider = new SonnenConnectionProvider(
authData: new SonnenAuth("<username>", "<password>"),
clientId: "<client_id>"
);
Caution: in a productive use you may want to implement your own
ISonnenConnectionProvider
and load your client id from a suitable vault and the user credentials should be entered by the user in some way and immediatelly discarded again. TheISonnenAuth
object will contain a refresh token that can be used to re-authenticate at any time, which can be persisted by implementing a customISonnenAuth
class. You can have a look at the respective classes inGraphIoT.Sonnen
as an example.
If you have the connection provider in place, you can create a SonnenPortalClient
and query for battery info and measurements:
var sonnenClient = new SonnenPortalClient(sonnenConnProvider);
// get the default Sonnen battery site
var siteId = (await sonnenClient.GetUserSites()).DefaultSiteId;
// get the first battery of this site
var battery = (await sonnenClient.GetBatterySystems(siteId)).First();
Console.WriteLine($"Charge cycles: {battery.BatteryChargeCycles}");
// get statistics
var stats = await sonnenClient.GetStatistics(siteId, DateTime.Now.AddDays(-1), DateTime.Now);
Console.WriteLine($"Produced energy (last 24h): {stats.ProducedEnergy}");
// get high resolution measurements and print the last 10 values of the time series
var graph = await sonnenClient.GetEnergyMeasurements(siteId, DateTime.Now.AddDays(-1), DateTime.Now);
var productionWattGraph = graph.ProductionPower;
Console.WriteLine($"Production power: {string.Join(',', productionWattGraph.TakeLast(10))}");
Platform Support
SonnenClient is targeted for .NET 9.0 or higher.
License
The MIT License (MIT)
Copyright (c) 2019-2024 Philip Daubmeier
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
-
net9.0
- HtmlAgilityPack (>= 1.11.71)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.9.0 - upgraded to target framework net9.0
1.8.0 - upgraded to target framework net8.0
1.7.0 - upgraded to target framework net5.0
1.6.2 - adjusted parsing according to Sonnen API changes
1.6.1 - fixed multi TFM issues
1.6.0 - widened range of supported platforms by targeting netstandard2.1
1.5.0 - moved to System.Text.Json and removed dependency to Json.NET
1.4.0 - upgraded to target framework netcoreapp3.1
1.3.0 - included default implementation of ISonnenConnectionProvider
1.2.0-beta1 - added support for HttpClientFactory and Polly. Enabled non-nullable reference types.
1.1.0 - migrated to netcoreapp3.0
1.0.0 - Initial version with publishing version 1.0 of GraphIoT