mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[mk2pvrouter] Add binary_sensor platform (#19719)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
72ac32a2c4
commit
499ce56c2b
@@ -0,0 +1,27 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_ID, CONF_TAG
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from .. import (
|
||||
CONF_MK2PVROUTER_ID,
|
||||
MK2PVROUTER_LISTENER_SCHEMA,
|
||||
mk2pvrouter_ns,
|
||||
register_mk2pvrouter_listener,
|
||||
)
|
||||
|
||||
Mk2PVRouterBinarySensor = mk2pvrouter_ns.class_(
|
||||
"Mk2PVRouterBinarySensor", binary_sensor.BinarySensor, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(Mk2PVRouterBinarySensor).extend(
|
||||
MK2PVROUTER_LISTENER_SCHEMA
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_TAG])
|
||||
await cg.register_component(var, config)
|
||||
await binary_sensor.register_binary_sensor(var, config)
|
||||
mk2pvrouter = await cg.get_variable(config[CONF_MK2PVROUTER_ID])
|
||||
await register_mk2pvrouter_listener(mk2pvrouter, var)
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "mk2pvrouter_binary_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace esphome::mk2pvrouter {
|
||||
|
||||
static const char *const TAG = "mk2pvrouter_binary_sensor";
|
||||
|
||||
Mk2PVRouterBinarySensor::Mk2PVRouterBinarySensor(const char *tag) : Mk2PVRouterListener(tag) {}
|
||||
|
||||
void Mk2PVRouterBinarySensor::publish_val(const char *val) {
|
||||
bool state = (strcmp(val, "0") != 0);
|
||||
this->publish_state(state);
|
||||
}
|
||||
|
||||
void Mk2PVRouterBinarySensor::dump_config() {
|
||||
LOG_BINARY_SENSOR(" ", "Mk2PVRouter Binary Sensor", this);
|
||||
ESP_LOGCONFIG(TAG, " Tag: %s", this->get_tag());
|
||||
}
|
||||
|
||||
} // namespace esphome::mk2pvrouter
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/mk2pvrouter/mk2pvrouter.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
|
||||
namespace esphome::mk2pvrouter {
|
||||
|
||||
class Mk2PVRouterBinarySensor final : public Mk2PVRouterListener, public binary_sensor::BinarySensor, public Component {
|
||||
public:
|
||||
explicit Mk2PVRouterBinarySensor(const char *tag);
|
||||
void publish_val(const char *val) override;
|
||||
void dump_config() override;
|
||||
};
|
||||
|
||||
} // namespace esphome::mk2pvrouter
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "esphome/components/mk2pvrouter/binary_sensor/mk2pvrouter_binary_sensor.h"
|
||||
|
||||
namespace esphome::mk2pvrouter::testing {
|
||||
|
||||
TEST(Mk2PVRouterBinarySensorTest, ZeroPublishesOff) {
|
||||
Mk2PVRouterBinarySensor sensor("R1");
|
||||
sensor.publish_val("0");
|
||||
ASSERT_TRUE(sensor.has_state());
|
||||
EXPECT_FALSE(sensor.state);
|
||||
}
|
||||
|
||||
TEST(Mk2PVRouterBinarySensorTest, OnePublishesOn) {
|
||||
Mk2PVRouterBinarySensor sensor("R1");
|
||||
sensor.publish_val("1");
|
||||
ASSERT_TRUE(sensor.has_state());
|
||||
EXPECT_TRUE(sensor.state);
|
||||
}
|
||||
|
||||
} // namespace esphome::mk2pvrouter::testing
|
||||
@@ -59,3 +59,10 @@ sensor:
|
||||
name: Relay 10 State
|
||||
tag: R10
|
||||
mk2pvrouter_id: test_mk2pvrouter
|
||||
|
||||
binary_sensor:
|
||||
# Test relay as binary sensor
|
||||
- platform: mk2pvrouter
|
||||
name: Relay 2 Binary
|
||||
tag: R2
|
||||
mk2pvrouter_id: test_mk2pvrouter
|
||||
|
||||
Reference in New Issue
Block a user