Driver for Khawasu
Project description
Description
Provides a simple usage for Khawasu socket-server.
How to install
Just make it: pip install driver-khawasu
Examples:
Create new Driver Instance
from driver_khawasu.driver import LogicalDriver
# Create instance and connect to server
driver = LogicalDriver("127.0.0.1", 1234)
Execute action
# Execute "power_level" action for device "103.47.177.100/29" with args: b'\125'
# No response returned
driver.execute("103.47.177.100/29", "power_level", bytes([125]))
# Or
# (note that the arguments here are bytes, and the special data that this Action accepts)
driver.get_device_by_address("103.47.177.100/29").execute("power_level", 125 / 255)
Get action state
# Get state for "power_level" action at device "103.47.177.100/29"
# Return a state of action (action-spec)
driver.action_get("103.47.177.100/29", "power_level")
# Or
# Returns special data, not bytes
# (see item "How to do anything with the device")
driver.get_device_by_address("103.47.177.100/29").get("power_level") # returned float [0, 1]
Subscribe to action
# Action handler for subscribe
def action_handler(address, method_name, data):
print( f"Wow, {method_name} was executed on"
f"{address} and returned {len(data)} bytes!")
# Subscribe to device "103.47.177.100/29" at action "power_level" with:
# duration: 60 sec
# timeout: 0 sec (for immediate subscribe)
# No response returned
# Will call action_handler when the answer comes
driver.subscribe("103.47.177.100/29", "power_level", 0, 60, action_handler)
# For subscribe on duration - 60 sec and timeout - 1 sec:
driver.subscribe("103.47.177.100/29", "power_level", 1000, 60, action_handler)
# OR
driver.get_device_by_address("103.47.177.100/29").subscribe("power_level", 1000, 60, action_handler)
Send request to socket server with response
# Send request to socket server and get response at command "list-devices"
# Return a data from socket server
driver.get("list-devices")
Class Device
Get list of devices
# Functions return List[Device]
driver.get_devices() # Get devices from khawasu server or internal cache
driver.get_devices_force() # Get devices from khawasu server
Get device by address
# Function return Device or None (if not found)
driver.get_device_by_address("103.47.177.100/29") # Get device by address
How to do anything with the device
# Get device by address
dev = driver.get_device_by_address("103.47.177.100/29")
# ActionType.TOGGLE
dev.get("power_state") # Return bool
dev.execute("power_state", True) # Specify bool in arg
# ActionType.RANGE
dev.get("power_level") # Return float [0, 1]
dev.execute("power_state", 0.41) # Specify float in arg
# ActionType.TEMPERATURE
dev.get("temperature") # Return float temperature in celsius
# ActionType.HUMIDITY
dev.get("humudity") # Return float [0, 1]
Class Action
Has methods to convert special objects to bytes and back: format_bytes_to_data(..)
and format_data_to_bytes(..)
Action(name, dev_class: int)
class ActionType(Enum):
UNKNOWN = 0
IMMEDIATE = 1
TOGGLE = 2
RANGE = 3
LABEL = 4
TEMPERATURE = 5
HUMIDITY = 6
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
driver_khawasu-0.0.2.tar.gz
(7.2 kB
view hashes)
Built Distribution
Close
Hashes for driver_khawasu-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65af8f1139d53aaf7e30252a4a2203c1333b40da5bdd05a65eab79cfc5490f20 |
|
MD5 | f86d1584376e1ffb80801cf88a6898ee |
|
BLAKE2b-256 | 25a4349ee3cfff6870a2162b437eda37e68cd131375aeb586f8b77c09ef03de5 |