Pacem.Domotics.Shelly 2.0.0-cantor

This is a prerelease version of Pacem.Domotics.Shelly.
dotnet add package Pacem.Domotics.Shelly --version 2.0.0-cantor
                    
NuGet\Install-Package Pacem.Domotics.Shelly -Version 2.0.0-cantor
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pacem.Domotics.Shelly" Version="2.0.0-cantor" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pacem.Domotics.Shelly" Version="2.0.0-cantor" />
                    
Directory.Packages.props
<PackageReference Include="Pacem.Domotics.Shelly" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Pacem.Domotics.Shelly --version 2.0.0-cantor
                    
#r "nuget: Pacem.Domotics.Shelly, 2.0.0-cantor"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Pacem.Domotics.Shelly@2.0.0-cantor
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Pacem.Domotics.Shelly&version=2.0.0-cantor&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Pacem.Domotics.Shelly&version=2.0.0-cantor&prerelease
                    
Install as a Cake Tool

Pacem.Domotics.Shelly

Shelly Gen2+ devices (Plus, Pro, Gen3, Gen4) on the local network, through their own RPC API: no Shelly Cloud, no account. Find them, bring a new one onto the house Wi-Fi, switch them, read their load and take the reports of the sleeping ones. Each becomes a brand-agnostic device of Pacem.Domotics: a plug is an IPlug, a temperature and humidity sensor a read-only IThermostat.

services.AddPacemShelly();          // returns the HttpClient builder: add a resilience handler if you like

// who is out there (mDNS, no password needed)
var devices = await discovery.BrowseAsync();                    // IShellyDiscovery
// devices[0].Host: "172.24.0.40", .Info: model, generation, firmware, whether a password is on

// a Plug S Gen3 (or any switch channel): an IPlug (ISwitch + IMeter), metering its load when IsMetered
var plug = factory.CreatePlug(devices[0].ToAddress(), password);   // IShellyDeviceFactory; password: null when auth is off
plug.Events.OnStateChangedAsync = context => { /* context.Status: output, power, energy… */ return Task.CompletedTask; };
await plug.RequestStateAsync();        // plug.State, .Power (W), .Voltage, .Current, .TotalEnergy (Wh)
await plug.SwitchOnAsync(timeout: 30);  // minutes: the device switches off by itself (toggle_after)

// pushed updates (button, timers, load): a WebSocket kept open by the caller
await plug.ListenAsync(stoppingToken);  // returns when the device closes it: reopen as you see fit

Sleeping sensors: the H&T

A battery sensor (the H&T Gen3) sleeps, waking every sys.wakeup_period seconds (2 h by default) or when a reading moves past its threshold; on USB it still sleeps. It can't be called while asleep: it reports by opening a WebSocket towards the host, its outbound WebSocket.

app.UsePacemShellyWebSocket();          // ws://{this host}/shelly; maps UseWebSockets() on its own branch

var ht = factory.CreateThermostat(new ShellyDeviceAddress("172.24.0.121", 0, "shellyhtg3-d885ac12c164"));
ht.Events.OnStateChangedAsync = context => { /* ht.MeasuredTemperature, .Humidity, .BatteryPercent, .LastSeen */ return Task.CompletedTask; };

// who connects, known or not: the MAC and the current address reconcile a device that moved
receiver.Events.OnConnectedAsync = connection => { /* connection.DeviceId, .Mac, .RemoteAddress, .IsKnown */ return Task.CompletedTask; };

// point an installed device at it (press its button first: it must be awake)
await provisioning.ConfigureOutboundWebSocketAsync(new ShellyEndpoint("172.24.0.121"), new Uri("ws://172.24.0.250:5080/shelly"));

Frames go by device id (pin it in the address), else by the address they came from. Nothing is created from a frame: an unknown device is only announced (IsKnown false); create it in OnConnectedAsync and it takes that connection's first report. The thermostat is read-only (Direction = Out): setting a temperature throws DeviceReadOnlyException.

The devices don't authenticate this socket: anyone on the LAN can claim a device id. Map it on a host only the house network reaches.

Addresses and passwords

A ShellyDeviceAddress is a host (IP, with :port when not 80), a channel (switch:0 → 0) and, optionally, the device id it must answer with: pin it (discovery's ToAddress() does) and a host that now answers for another device throws ShellyAddressException instead of being switched.

The password is the device's admin password, when its authentication is on. Over HTTP it answers the Digest challenge (Pacem.Net.Http's DigestAuthenticationHandler, SHA-256); over the WebSocket it goes inside the frames. It never leaves the process otherwise and is never printed.

Consumption

IsMetered tells whether the channel meters at all: a plug without a meter is an IPlug all the same, its IMeter requests yielding no readings. On a metered plug they read what the device has: the power, voltage and current (RequestCurrentAsync) and the energy total (RequestTotalAsync). The device keeps no hourly/daily/monthly history, so a plug is no IConsumptionTracker: the history is the controller's to keep.

Provisioning

An out-of-the-box Shelly has no Wi-Fi: it opens its own access point (SSID = its device id, open by default) and answers there, by default at 192.168.33.1 (ShellyDefaults.AccessPointHost; override DeviceHost if yours differs). From a machine joined to that AP:

var result = await provisioning.ProvisionAsync(new ShellyProvisioningRequest("HouseWiFi", wifiPassword)
{
    // optional: where a sleeping device (H&T) connects to report
    OutboundWebSocket = new Uri("ws://172.24.0.250:5080/shelly"),
});

The device then joins the house Wi-Fi (about a second later) and shows up in discovery.

The RPC client

ShellyRpcClient is the raw API for anything the devices don't cover: CallAsync(endpoint, "Switch.GetConfig", new { id = 0 }) returns the result, an error throws ShellyRpcException (IsUnauthorized for a missing or wrong password). Transport failures throw what HttpClient throws.

Notes

  • No polling and no timers: every call is the caller's, ListenAsync is the caller's socket.
  • A device takes a handful of WebSockets at once (6 on Gen2+): one per device, not one per user.
  • Battery devices sleep: discovery only sees them awake, and they report through their outbound WebSocket.
  • A device's MAC (Mac, from Shelly.GetDeviceInfo or the device id) never changes; its IP may. The address stays the readable host, and the MAC is what tells it's the same device at a new one.
Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-cantor 60 11/28/2026
2.0.0-buckmaster 66 11/27/2026
2.0.0-alpoge 70 11/27/2026