-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mqtt.homeassistant] Implement Tag Scanner (#17852)
Signed-off-by: Cody Cutrer <[email protected]>
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
53 changes: 53 additions & 0 deletions
53
...ssistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/Tag.java
This file contains 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,53 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.mqtt.homeassistant.internal.component; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.mqtt.generic.values.TextValue; | ||
import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType; | ||
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration; | ||
|
||
/** | ||
* A MQTT Tag scanner, following the https://www.home-assistant.io/integrations/tag.mqtt/ specification. | ||
* | ||
* @author Cody Cutrer - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class Tag extends AbstractComponent<Tag.ChannelConfiguration> { | ||
public static final String TAG_CHANNEL_ID = "tag"; | ||
|
||
/** | ||
* Configuration class for MQTT component | ||
*/ | ||
public static class ChannelConfiguration extends AbstractChannelConfiguration { | ||
ChannelConfiguration() { | ||
super("MQTT Tag Scanner"); | ||
} | ||
|
||
protected String topic = ""; | ||
} | ||
|
||
public Tag(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) { | ||
super(componentConfiguration, ChannelConfiguration.class, newStyleChannels); | ||
|
||
buildChannel(TAG_CHANNEL_ID, ComponentChannelType.TRIGGER, new TextValue(), getName(), | ||
componentConfiguration.getUpdateListener()) | ||
.stateTopic(channelConfiguration.topic, channelConfiguration.getValueTemplate()).trigger(true).build(); | ||
finalizeChannels(); | ||
} | ||
|
||
@Override | ||
protected void addJsonAttributesChannel() { | ||
// json_attributes are not supported | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/TagTests.java
This file contains 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,64 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.mqtt.homeassistant.internal.component; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.jupiter.api.Test; | ||
import org.openhab.binding.mqtt.generic.values.TextValue; | ||
|
||
/** | ||
* Tests for {@link Tag} | ||
* | ||
* @author Cody Cutrer - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class TagTests extends AbstractComponentTests { | ||
public static final String CONFIG_TOPIC = "tag/0AFFD2"; | ||
|
||
@SuppressWarnings("null") | ||
@Test | ||
public void test() throws InterruptedException { | ||
var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC), """ | ||
{ | ||
"topic": "0AFFD2/tag_scanned", | ||
"value_template": "{{ value_json.PN532.UID }}" | ||
} | ||
"""); | ||
|
||
assertThat(component.channels.size(), is(1)); | ||
assertThat(component.getName(), is("MQTT Tag Scanner")); | ||
|
||
assertChannel(component, Tag.TAG_CHANNEL_ID, "0AFFD2/tag_scanned", "", "MQTT Tag Scanner", TextValue.class); | ||
|
||
publishMessage("0AFFD2/tag_scanned", """ | ||
{ | ||
"Time": "2020-09-28T17:02:10", | ||
"PN532": { | ||
"UID": "E9F35959", | ||
"DATA":"ILOVETASMOTA" | ||
} | ||
} | ||
"""); | ||
assertTriggered(component, Tag.TAG_CHANNEL_ID, "E9F35959"); | ||
} | ||
|
||
@Override | ||
protected Set<String> getConfigTopics() { | ||
return Set.of(CONFIG_TOPIC); | ||
} | ||
} |