dlipower
The dlipower library provides a Python API for managing Digital Loggers networked power switches. It allows programmatic control over power outlets, including turning them on/off, cycling power, and renaming outlets. The library also functions as a command-line utility. The current version is 1.0.176, and while still functional, it is noted as being best suited for older (pre-2017) DLI power controllers.
Common errors
-
TypeError: object of type 'NoneType' has no len()
cause This error typically occurs when the `PowerSwitch` object fails to connect to the DLI device, often due to an incorrectly formatted hostname (e.g., including `http://` or `https://` in the hostname string).fixProvide only the IP address or hostname without the protocol (e.g., `hostname='192.168.0.100'` instead of `hostname='http://192.168.0.100/'`). Ensure the DLI device is reachable and credentials are correct. -
login denied for admin@10.1.48.158 (plaintext): password check failed
cause The DLI power switch denied the login attempt. This often means incorrect credentials were provided, or the device's security settings prevent plain-text logins.fixDouble-check the `userid` and `password`. On the DLI device's web interface, navigate to 'Access Settings' or 'External APIs' and ensure that the user has appropriate permissions and that plain HTTP/plaintext authentication is allowed if `use_https=False` is used, or ensure HTTPS is enabled and `use_https=True`. -
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))cause The connection to the DLI power switch was unexpectedly closed by the device itself, often due to network instability, device overload, or the device rejecting the connection attempt prematurely.fixVerify network connectivity to the DLI device. Ensure the device is powered on and responsive. Increase the `timeout` parameter in the `PowerSwitch` constructor. Check the device's logs if accessible.
Warnings
- gotcha The `dlipower` library is specifically noted as being 'Best for the legacy (pre 2017) DLI power controllers.' Compatibility with newer Digital Loggers devices may vary or require alternative libraries/APIs.
- gotcha Authentication failures (e.g., 'login denied') are common if the Digital Loggers device's web interface settings are not configured to allow plain-text logins or if incorrect credentials are provided.
- gotcha Passing a hostname with an HTTP/HTTPS scheme (e.g., `http://192.168.0.100/`) to `PowerSwitch(hostname=...)` can lead to connection errors or `TypeError: object of type 'NoneType' has no len()` when trying to access outlets.
- gotcha Network requests can hang indefinitely without a timeout. The `PowerSwitch` constructor accepts a `timeout` parameter, which defaults to `None` (no timeout).
Install
-
pip install dlipower
Imports
- PowerSwitch
from dlipower import PowerSwitch
Quickstart
import os
from dlipower import PowerSwitch
HOSTNAME = os.environ.get('DLI_HOSTNAME', 'lpc.digital-loggers.com')
USERID = os.environ.get('DLI_USERID', 'admin')
PASSWORD = os.environ.get('DLI_PASSWORD', '4321') # Replace with actual password or secure method
try:
# Connect to the Digital Loggers Power Switch
switch = PowerSwitch(hostname=HOSTNAME, userid=USERID, password=PASSWORD)
# Print the current state of all outlets
print(f"\nCurrent Power Switch Status at {HOSTNAME}:")
print(switch)
# Turn off the first outlet (outlet number 1)
print("\nTurning off outlet 1...")
switch.off(1)
print(f"Outlet 1 state: {switch[0].state}")
# Turn on the second outlet (accessed by index 1, which is outlet number 2)
print("\nTurning on outlet 2...")
switch[1].on()
print(f"Outlet 2 state: {switch[1].state}")
# Rename an outlet
# print("\nRenaming outlet 3 to 'My Device A'...")
# switch[2].name = 'My Device A'
# print(f"Outlet 3 new name: {switch[2].name}")
print("\nOperations complete.")
except Exception as e:
print(f"An error occurred: {e}")