Skip to content

Commit

Permalink
[tasmotaplug] Initial contribution (openhab#15857)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Lobstein <[email protected]>
  • Loading branch information
mlobstein authored Nov 10, 2023
1 parent 10d519b commit ffc127f
Show file tree
Hide file tree
Showing 14 changed files with 584 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
/bundles/org.openhab.binding.tado/ @dfrommi @andrewfg
/bundles/org.openhab.binding.tankerkoenig/ @dolic @JueBag
/bundles/org.openhab.binding.tapocontrol/ @wildcs
/bundles/org.openhab.binding.tasmotaplug/ @mlobstein
/bundles/org.openhab.binding.telegram/ @ZzetT
/bundles/org.openhab.binding.teleinfo/ @Nokyyz @olivierkeke
/bundles/org.openhab.binding.tellstick/ @openhab/add-ons-maintainers
Expand Down
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,11 @@
<artifactId>org.openhab.binding.tapocontrol</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.tasmotaplug</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.telegram</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions bundles/org.openhab.binding.tasmotaplug/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab-addons
77 changes: 77 additions & 0 deletions bundles/org.openhab.binding.tasmotaplug/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# TasmotaPlug Binding

This binding connects Tasmota flashed smart plugs with 1, 2, 3 or 4 relay channels to openHAB.
The plug must report the status of the relay via the url `http://$PLUG_IP/cm?cmnd=Power` in order for the binding to work.
See the [Tasmota Supported Devices Repository](https://templates.blakadder.com/plug.html) for a list of supported plugs.

## Supported Things

There is exactly one supported thing type, which represents any supported Tasmota smart plug.
It has the `plug` id.
Multiple Things can be added if more than one plug is to be controlled.

## Discovery

Discovery is not supported. All things must be added manually.

## Thing Configuration

At minimum, the host name must be specified.
The refresh interval and number of channels can be overridden from the default.

| Parameter | Description |
|-------------|-----------------------------------------------------------------------------------------|
| hostName | The host name or IP address of the plug. Mandatory. |
| refresh | Overrides the refresh interval of the plug status. Optional, the default is 30 seconds. |
| numChannels | Number of channels on the Tasmota Plug (1-4). Optional, the default is 1 |
| username | Username for authentication with the Tasmota Plug. Default 'admin' |
| password | Password for authentication with the Tasmota Plug, if not supplied auth is disabled. |

## Channels

The number of channels depends of on the `numChannels` configuration parameter.
Channels above the number specified are automatically removed.
Therefore `numChannels` cannot be changed upward after Thing creation.
If the number of channels must be increased, delete the Thing and re-create it with the correct number.

| Channel ID | Item Type | Description |
|------------|-----------|-----------------------------------------|
| power | Switch | Turns the smart plug relay #1 ON or OFF |
| power2 | Switch | Turns the smart plug relay #2 ON or OFF |
| power3 | Switch | Turns the smart plug relay #3 ON or OFF |
| power4 | Switch | Turns the smart plug relay #4 ON or OFF |

## Full Example

tasmotaplug.things:

```java
tasmotaplug:plug:plug1 "Plug 1" [ hostName="192.168.10.1", refresh=30 ]
tasmotaplug:plug:plug2 "Plug 2" [ hostName="myplug2", refresh=30 ]
```

tasmotaplug.items:

```java
Switch Plug1 "Plug 1 Power" { channel="tasmotaplug:plug:plug1:power" }

Switch Plug2a "4ch Power 1" { channel="tasmotaplug:plug:plug2:power" }
Switch Plug2b "4ch Power 2" { channel="tasmotaplug:plug:plug2:power2" }
Switch Plug2c "4ch Power 3" { channel="tasmotaplug:plug:plug2:power3" }
Switch Plug2d "4ch Power 4" { channel="tasmotaplug:plug:plug2:power4" }
```

tasmotaplug.sitemap:

```perl
sitemap tasmotaplug label="My Tasmota Plugs" {
Frame label="Plugs" {
Switch item=Plug1

Switch item=Plug2a
Switch item=Plug2b
Switch item=Plug2c
Switch item=Plug2d
}
}
```
17 changes: 17 additions & 0 deletions bundles/org.openhab.binding.tasmotaplug/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.tasmotaplug</artifactId>

<name>openHAB Add-ons :: Bundles :: Tasmota Plug Binding</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.tasmotaplug-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-binding-tasmotaplug" description="Tasmota Plug Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.tasmotaplug/${project.version}</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tasmotaplug.internal;

import java.util.List;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;

/**
* The {@link TasmotaPlugBinding} class defines common constants, which are
* used across the whole binding.
*
* @author Michael Lobstein - Initial contribution
*/
@NonNullByDefault
public class TasmotaPlugBindingConstants {
public static final String BINDING_ID = "tasmotaplug";

public static final int DEFAULT_REFRESH_PERIOD_SEC = 30;
public static final int DEFAULT_NUM_CHANNELS = 1;

public static final String CMD_URI = "/cm?cmnd=%s";
public static final String CMD_URI_AUTH = "/cm?user=%s&password=%s&cmnd=%s";

public static final String ON = "ON";
public static final String OFF = "OFF";
public static final String BLANK = "";

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_PLUG = new ThingTypeUID(BINDING_ID, "plug");

// List of all Channel id's
public static final String POWER = "power";
public static final String POWER2 = "power2";
public static final String POWER3 = "power3";
public static final String POWER4 = "power4";

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_PLUG);
public static final List<String> SUPPORTED_CHANNEL_IDS = List.of(POWER, POWER2, POWER3, POWER4);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tasmotaplug.internal;

import static org.openhab.binding.tasmotaplug.internal.TasmotaPlugBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link TasmotaPlugConfiguration} is the class used to match the
* thing configuration.
*
* @author Michael Lobstein - Initial contribution
*/
@NonNullByDefault
public class TasmotaPlugConfiguration {
public String hostName = BLANK;
public int refresh = DEFAULT_REFRESH_PERIOD_SEC;
public int numChannels = DEFAULT_NUM_CHANNELS;
public String username = BLANK;
public String password = BLANK;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tasmotaplug.internal;

import static org.openhab.binding.tasmotaplug.internal.TasmotaPlugBindingConstants.THING_TYPE_PLUG;

import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.tasmotaplug.internal.handler.TasmotaPlugHandler;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* The {@link TasmotaPlugHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Michael Lobstein - Initial contribution
*/
@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.tasmotaplug")
public class TasmotaPlugHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_PLUG);
private final HttpClient httpClient;

@Activate
public TasmotaPlugHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
this.httpClient = httpClientFactory.getCommonHttpClient();
}

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}

@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
if (THING_TYPE_PLUG.equals(thing.getThingTypeUID())) {
return new TasmotaPlugHandler(thing, httpClient);
}

return null;
}
}
Loading

0 comments on commit ffc127f

Please sign in to comment.