Documentation
ยถ
Overview ยถ
Package shelly provides a comprehensive Go library for controlling Shelly smart home devices across all device generations (Gen1, Gen2, Gen3, Gen4) and communication protocols (HTTP, WebSocket, MQTT, CoAP/CoIoT).
Overview ยถ
This library provides a unified interface for interacting with Shelly devices, whether you're controlling them locally via HTTP/WebSocket, discovering them on your network, or managing them through the Shelly Cloud API.
Quick Start ยถ
For Gen2/Gen3/Gen4 devices (RPC-based):
client := gen2.NewClient("http://192.168.1.100")
sw := components.NewSwitch(client, 0)
err := sw.Set(context.Background(), true)
For Gen1 devices (REST-based):
client := gen1.NewClient("http://192.168.1.101")
relay := components.NewRelay(client, 0)
err := relay.Set(context.Background(), true)
Package Organization ยถ
The library is organized into several focused packages:
- types: Core interfaces and type definitions
- transport: Communication layer implementations (HTTP, WebSocket, MQTT, CoAP)
- rpc: RPC framework for Gen2+ devices
- gen1: Support for Gen1 devices
- gen2: Support for Gen2+ devices (Plus, Pro, Gen3, Gen4)
- cloud: Shelly Cloud API integration
- discovery: Device discovery via mDNS, BLE, and CoIoT
- events: Event bus and real-time notifications
- helpers: Convenience utilities for batch operations, groups, and scenes
- profiles: Device profiles and capability detection
Device Generations ยถ
Shelly devices come in multiple generations with different protocols:
Gen1: REST API over HTTP, CoIoT for real-time updates
- Examples: Shelly 1, 1PM, 2.5, Plug, Bulb, RGBW2, Dimmer, EM, H&T
Gen2 (Plus): RPC over HTTP/WebSocket, MQTT support
- Examples: Shelly Plus 1, 1PM, 2PM, i4, Plug S, H&T
Pro: Enhanced Gen2 with Ethernet, ModBus, additional I/O
- Examples: Shelly Pro 1, 1PM, 2PM, 3, 3EM, 4PM, Dimmer
Gen3: Latest generation with improved hardware and features
- Examples: Shelly 1 Gen3, 1PM Gen3, 2PM Gen3, Wall Display
Gen4: Future-ready devices (as released by Allterco)
- Examples: Shelly 1 Gen4, 1PM Gen4
BLU: Bluetooth Low Energy devices
- Examples: BLU Button, Door/Window, Motion, H&T, TRV
Wave: Z-Wave devices
- Examples: Wave 1, 1PM, 2PM, Plug, Shutter
Components ยถ
Devices are composed of components that provide specific functionality:
- Switch: On/off control with power monitoring
- Cover: Roller shutter/blind control with position
- Light: Dimming and color control
- Input: Button and sensor inputs
- PM/EM: Power and energy monitoring
- WiFi/Ethernet/BLE: Network configuration
- Cloud/MQTT/Webhook: Integration services
- Script: JavaScript automation
- Temperature/Humidity/Smoke: Environmental sensors
- Thermostat: Climate control
Each component implements standard methods:
- GetConfig: Retrieve component configuration
- SetConfig: Update component configuration
- GetStatus: Get current component status
Transports ยถ
Communication with devices uses pluggable transports:
// HTTP transport (most common)
http := transport.NewHTTP("http://192.168.1.100",
transport.WithTimeout(30*time.Second),
transport.WithAuth("admin", "password"))
// WebSocket transport for real-time communication
ws := transport.NewWebSocket("ws://192.168.1.100/rpc")
// MQTT transport
mqtt := transport.NewMQTT("mqtt://broker:1883",
transport.WithMQTTTopic("shellies/device-id"))
Discovery ยถ
Devices can be discovered automatically using multiple methods:
scanner := discovery.NewScanner()
devices, err := scanner.Scan(ctx)
for _, device := range devices {
fmt.Printf("Found %s at %s\n", device.Name, device.Address)
}
Cloud API ยถ
Control devices remotely via Shelly Cloud:
client := cloud.NewClient() err := client.Authenticate(ctx, "username", "password") devices, err := client.ListDevices(ctx) err = client.SetSwitch(ctx, devices[0].ID, 0, true)
Events ยถ
Subscribe to real-time device events:
bus := events.NewBus()
bus.Subscribe(events.FilterByDevice(deviceID), func(e events.Event) {
fmt.Printf("Event: %+v\n", e)
})
device.AttachEventBus(bus)
Error Handling ยถ
The library defines standard error types:
- ErrNotFound: Resource not found
- ErrAuth: Authentication failed
- ErrTimeout: Operation timed out
- ErrNotSupported: Feature not supported by device
All errors are wrapped with context using fmt.Errorf with %w.
Context Support ยถ
All operations accept context.Context for cancellation and timeout:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() status, err := sw.GetStatus(ctx)
Thread Safety ยถ
All client and device implementations are safe for concurrent use unless otherwise documented. Components share the underlying client and can be used from multiple goroutines.
Extensibility ยถ
The library is designed to be extensible for new devices and firmware features:
- All structs include RawFields map[string]json.RawMessage for unknown fields
- Component interface allows custom implementations
- Transport interface supports new protocols
Testing ยถ
The library includes comprehensive testing utilities:
import "github.com/tj-smith47/shelly-go/internal/testutil"
mock := testutil.NewMockTransport()
mock.AddResponse("Shelly.GetDeviceInfo", testutil.LoadFixture("device.json"))
client := gen2.NewClient(mock)
Examples ยถ
See the examples/ directory for complete, runnable examples covering:
- Basic device control
- Device discovery
- Cloud API usage
- Real-time events
- Batch operations
- Scene management
- Device provisioning
- Firmware updates
Official Documentation ยถ
For more information about Shelly devices and protocols, see:
- Gen2+ API: https://shelly-api-docs.shelly.cloud/gen2/
- Gen1 API: https://shelly-api-docs.shelly.cloud/gen1/
- Cloud API: https://shelly-api-docs.shelly.cloud/cloud-control-api/
License ยถ
MIT License - Copyright (c) 2025 TJ Smith ยถ
Shellyยฎ is a registered trademark of Allterco Robotics. This project is not affiliated with or endorsed by Allterco Robotics.
Directories
ยถ
| Path | Synopsis |
|---|---|
|
Package backup provides device configuration backup and restore functionality.
|
Package backup provides device configuration backup and restore functionality. |
|
Package cloud provides a client for the Shelly Cloud Control API.
|
Package cloud provides a client for the Shelly Cloud Control API. |
|
Package discovery provides device discovery functionality for Shelly devices.
|
Package discovery provides device discovery functionality for Shelly devices. |
|
Package events provides a typed event system for Shelly device notifications.
|
Package events provides a typed event system for Shelly device notifications. |
|
examples
|
|
|
advanced/batch_operations
command
Example: batch_operations demonstrates batch operations across multiple Shelly devices.
|
Example: batch_operations demonstrates batch operations across multiple Shelly devices. |
|
advanced/custom_component
command
Example: custom_component demonstrates creating custom component types.
|
Example: custom_component demonstrates creating custom component types. |
|
advanced/scenes
command
Example: scenes demonstrates scene management for coordinated device control.
|
Example: scenes demonstrates scene management for coordinated device control. |
|
basic/cover_control
command
Example: cover_control demonstrates controlling a Shelly Gen2+ cover (roller shutter).
|
Example: cover_control demonstrates controlling a Shelly Gen2+ cover (roller shutter). |
|
basic/light_control
command
Example: light_control demonstrates controlling a Shelly Gen2+ light/dimmer.
|
Example: light_control demonstrates controlling a Shelly Gen2+ light/dimmer. |
|
basic/switch_control
command
Example: switch_control demonstrates controlling a Shelly Gen2+ switch.
|
Example: switch_control demonstrates controlling a Shelly Gen2+ switch. |
|
cloud/auth
command
Example: cloud/auth demonstrates authenticating with the Shelly Cloud API.
|
Example: cloud/auth demonstrates authenticating with the Shelly Cloud API. |
|
cloud/realtime
command
Example: cloud/realtime demonstrates real-time events from Shelly Cloud WebSocket.
|
Example: cloud/realtime demonstrates real-time events from Shelly Cloud WebSocket. |
|
discovery/mdns
command
Example: mdns demonstrates discovering Shelly devices on the local network via mDNS.
|
Example: mdns demonstrates discovering Shelly devices on the local network via mDNS. |
|
Package factory provides device creation utilities.
|
Package factory provides device creation utilities. |
|
Package firmware provides OTA (Over-The-Air) firmware management for Shelly devices.
|
Package firmware provides OTA (Over-The-Air) firmware management for Shelly devices. |
|
Package gen1 provides support for Shelly Gen1 devices.
|
Package gen1 provides support for Shelly Gen1 devices. |
|
components
Package components provides Gen1 Shelly device components.
|
Package components provides Gen1 Shelly device components. |
|
Package gen2 provides support for Gen2+ Shelly devices (Plus, Pro, Gen3, Gen4).
|
Package gen2 provides support for Gen2+ Shelly devices (Plus, Pro, Gen3, Gen4). |
|
Package helpers provides convenience utilities for working with Shelly devices.
|
Package helpers provides convenience utilities for working with Shelly devices. |
|
Package integrator provides a client for the Shelly Integrator API.
|
Package integrator provides a client for the Shelly Integrator API. |
|
internal
|
|
|
testutil
Package testutil provides testing utilities for the shelly-go library.
|
Package testutil provides testing utilities for the shelly-go library. |
|
testutil/integration
Package integration provides integration test utilities for testing against real Shelly devices and the Shelly Cloud API.
|
Package integration provides integration test utilities for testing against real Shelly devices and the Shelly Cloud API. |
|
Package lora provides support for the Shelly LoRa Add-On.
|
Package lora provides support for the Shelly LoRa Add-On. |
|
Package matter provides Matter protocol support for Shelly Gen4+ devices.
|
Package matter provides Matter protocol support for Shelly Gen4+ devices. |
|
Package notifications provides a unified interface for receiving real-time events from Shelly devices across all generations and connection methods.
|
Package notifications provides a unified interface for receiving real-time events from Shelly devices across all generations and connection methods. |
|
Package profiles provides device profile definitions for all Shelly devices.
|
Package profiles provides device profile definitions for all Shelly devices. |
|
blu
Package blu provides device profiles for Shelly BLU Bluetooth devices.
|
Package blu provides device profiles for Shelly BLU Bluetooth devices. |
|
gen1
Package gen1 provides device profiles for Shelly Gen1 devices.
|
Package gen1 provides device profiles for Shelly Gen1 devices. |
|
gen2
Package gen2 provides device profiles for Shelly Gen2 Plus and Pro devices.
|
Package gen2 provides device profiles for Shelly Gen2 Plus and Pro devices. |
|
gen3
Package gen3 provides device profiles for Shelly Gen3 devices.
|
Package gen3 provides device profiles for Shelly Gen3 devices. |
|
gen4
Package gen4 provides device profiles for Shelly Gen4 devices.
|
Package gen4 provides device profiles for Shelly Gen4 devices. |
|
wave
Package wave provides device profiles for Shelly Wave Z-Wave devices.
|
Package wave provides device profiles for Shelly Wave Z-Wave devices. |
|
Package provisioning provides utilities for initial setup and configuration of Shelly devices.
|
Package provisioning provides utilities for initial setup and configuration of Shelly devices. |
|
Package rpc provides a JSON-RPC 2.0 framework for Shelly device communication.
|
Package rpc provides a JSON-RPC 2.0 framework for Shelly device communication. |
|
tools
|
|
|
discover
command
Standalone Shelly device discovery tool.
|
Standalone Shelly device discovery tool. |
|
Package transport provides communication layer implementations for Shelly devices.
|
Package transport provides communication layer implementations for Shelly devices. |
|
Package types provides core interfaces and type definitions used throughout the shelly-go library.
|
Package types provides core interfaces and type definitions used throughout the shelly-go library. |
|
Package zigbee provides support for Shelly Gen2+ Zigbee component.
|
Package zigbee provides support for Shelly Gen2+ Zigbee component. |
|
Package zwave provides utilities for working with Shelly Wave Z-Wave devices.
|
Package zwave provides utilities for working with Shelly Wave Z-Wave devices. |