This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Implement TasmotaPower driver #425
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../../../../packages/jumpstarter-driver-tasmota/README.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Tasmota driver | ||
|
|
||
| `jumpstarter-driver-tasmota` provides functionality for interacting with tasmota compatible devices. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```shell | ||
| pip3 install --extra-index-url https://pkg.jumpstarter.dev/simple/ jumpstarter-driver-tasmota | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| Example configuration: | ||
|
|
||
| ```yaml | ||
| export: | ||
| power: | ||
| type: jumpstarter_driver_tasmota.driver.TasmotaPower | ||
|
NickCao marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ### Config parameters | ||
|
|
||
| | Parameter | Description | Default | | ||
| |--------------|-----------------------------------------------------------------|----------| | ||
| | `host` | MQTT broker hostname or IP address | Required | | ||
| | `port` | MQTT broker port | 1883 | | ||
| | `tls` | MQTT broker TLS enabled | True | | ||
| | `client_id` | Client identifier for MQTT connection | | | ||
| | `transport` | Transport protocol, one of "tcp", "websockets", "unix" | "tcp" | | ||
| | `timeout` | Timeout in seconds for operations | | | ||
| | `username` | Username for MQTT authentication | | | ||
| | `password` | Password for MQTT authentication | | | ||
| | `cmnd_topic` | MQTT topic for sending commands to the Tasmota device | Required | | ||
| | `stat_topic` | MQTT topic for receiving status updates from the Tasmota device | Required | | ||
|
|
||
| ## API Reference | ||
|
|
||
| The tasmota power driver provides a `PowerClient` with the following API: | ||
|
|
||
| ```{eval-rst} | ||
| .. autoclass:: jumpstarter_driver_power.client.PowerClient() | ||
| :no-index: | ||
| :members: on, off | ||
|
NickCao marked this conversation as resolved.
|
||
| ``` | ||
|
NickCao marked this conversation as resolved.
|
||
Empty file.
85 changes: 85 additions & 0 deletions
85
packages/jumpstarter-driver-tasmota/jumpstarter_driver_tasmota/driver.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from dataclasses import dataclass, field | ||
| from threading import Condition | ||
| from typing import Literal | ||
|
|
||
| import paho.mqtt.client as paho | ||
| from jumpstarter_driver_power.driver import PowerInterface | ||
| from paho.mqtt.enums import CallbackAPIVersion | ||
|
|
||
| from jumpstarter.driver import Driver, export | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class TasmotaPower(PowerInterface, Driver): | ||
| """driver for tasmota compatible power switches""" | ||
|
|
||
| client_id: str | None = None | ||
| transport: Literal["tcp", "websockets", "unix"] = "tcp" | ||
| timeout: float | None = None | ||
|
|
||
| host: str | ||
| port: int = 1883 | ||
| tls: bool = True | ||
|
|
||
| username: str | None = None | ||
| password: str | None = None | ||
|
|
||
| cmnd_topic: str | ||
| stat_topic: str | ||
|
|
||
| mq: paho.Client = field(init=False) | ||
| state: str | None = field(init=False, default=None) | ||
| cond: Condition = field(init=False, default_factory=Condition) | ||
|
|
||
| def __post_init__(self): | ||
| if hasattr(super(), "__post_init__"): | ||
| super().__post_init__() | ||
|
|
||
| self.mq = paho.Client( | ||
| callback_api_version=CallbackAPIVersion.VERSION2, | ||
| client_id=self.client_id, | ||
| transport=self.transport, | ||
| ) | ||
|
|
||
| def on_message(client, userdata, msg): | ||
| if msg.topic == self.stat_topic: | ||
| self.state = msg.payload.decode() | ||
| with self.cond: | ||
| self.cond.notify_all() | ||
|
|
||
| self.mq.on_message = on_message | ||
|
|
||
| if self.tls: | ||
| self.mq.tls_set() | ||
|
|
||
| self.mq.username_pw_set(self.username, self.password) | ||
| self.mq.connect(self.host, self.port) | ||
| self.mq.loop_start() | ||
|
|
||
| self.mq.subscribe(self.stat_topic) | ||
|
|
||
| def publish(self, state): | ||
| self.mq.publish( | ||
| self.cmnd_topic, | ||
| payload=state, | ||
| qos=1, | ||
| ).wait_for_publish( | ||
| timeout=self.timeout, | ||
| ) | ||
| with self.cond: | ||
| self.cond.wait_for( | ||
| lambda: self.state == state, | ||
| timeout=self.timeout, | ||
| ) | ||
|
NickCao marked this conversation as resolved.
|
||
|
|
||
| @export | ||
| def on(self): | ||
| self.publish("ON") | ||
|
|
||
| @export | ||
| def off(self): | ||
| self.publish("OFF") | ||
|
|
||
| @export | ||
| def read(self): | ||
| pass | ||
|
NickCao marked this conversation as resolved.
|
||
29 changes: 29 additions & 0 deletions
29
packages/jumpstarter-driver-tasmota/jumpstarter_driver_tasmota/driver_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import pytest | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| from pytest_mqtt.model import MqttMessage | ||
|
|
||
| from .driver import TasmotaPower | ||
| from jumpstarter.common.utils import serve | ||
|
|
||
|
|
||
| @pytest.mark.skip("requires docker") | ||
| def test_tasmota_power(mosquitto, capmqtt): | ||
| cmnd_topic = "cmnd/tasmota_6990F2/POWER" | ||
| stat_topic = "stat/tasmota_6990F2/POWER" | ||
|
|
||
| with serve( | ||
| TasmotaPower( | ||
| host=mosquitto[0], | ||
| port=int(mosquitto[1]), | ||
| tls=False, | ||
| transport="tcp", | ||
| cmnd_topic=cmnd_topic, | ||
| stat_topic=stat_topic, | ||
| ) | ||
| ) as client: | ||
| capmqtt.publish(topic=stat_topic, payload="ON") | ||
| client.on() | ||
| assert MqttMessage(topic=cmnd_topic, payload=b"ON", userdata=None) in capmqtt.messages | ||
|
|
||
| capmqtt.publish(topic=stat_topic, payload="OFF") | ||
| client.off() | ||
| assert MqttMessage(topic=cmnd_topic, payload=b"OFF", userdata=None) in capmqtt.messages | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| [project] | ||
| name = "jumpstarter-driver-tasmota" | ||
| dynamic = ["version", "urls"] | ||
| description = "Jumpstarter driver for controlling Tasmota-compatible devices via MQTT" | ||
| readme = "README.md" | ||
| license = "Apache-2.0" | ||
| authors = [{ name = "Nick Cao", email = "nickcao@nichi.co" }] | ||
| requires-python = ">=3.11" | ||
| dependencies = [ | ||
| "anyio>=4.6.2.post1", | ||
| "jumpstarter_driver_power", | ||
| "jumpstarter", | ||
| "paho-mqtt>=2.1.0", | ||
| ] | ||
|
|
||
| [project.entry-points."jumpstarter.drivers"] | ||
| TasmotaPower = "jumpstarter_driver_tasmota.driver:TasmotaPower" | ||
|
|
||
|
|
||
| [tool.hatch.version] | ||
| source = "vcs" | ||
| raw-options = { 'root' = '../../' } | ||
|
|
||
| [tool.hatch.metadata.hooks.vcs.urls] | ||
| Homepage = "https://jumpstarter.dev" | ||
| source_archive = "https://github.com/jumpstarter-dev/repo/archive/{commit_hash}.zip" | ||
|
|
||
| [tool.pytest.ini_options] | ||
| addopts = "--cov --cov-report=html --cov-report=xml" | ||
| log_cli = true | ||
| log_cli_level = "INFO" | ||
| testpaths = ["jumpstarter_driver_tasmota"] | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling", "hatch-vcs", "hatch-pin-jumpstarter"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [tool.hatch.build.hooks.pin_jumpstarter] | ||
| name = "pin_jumpstarter" | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "pytest-cov>=6.0.0", | ||
| "pytest>=8.3.3", | ||
| "pytest-mqtt>=0.5.0", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.