mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 11:47:30 +01:00
[homeassistant] Native switch entity import and control (#7018)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
c5b1a8eb81
commit
1d25db491c
@ -169,6 +169,7 @@ esphome/components/heatpumpir/* @rob-deutsch
|
|||||||
esphome/components/hitachi_ac424/* @sourabhjaiswal
|
esphome/components/hitachi_ac424/* @sourabhjaiswal
|
||||||
esphome/components/hm3301/* @freekode
|
esphome/components/hm3301/* @freekode
|
||||||
esphome/components/homeassistant/* @OttoWinter @esphome/core
|
esphome/components/homeassistant/* @OttoWinter @esphome/core
|
||||||
|
esphome/components/homeassistant/switch/* @Links2004
|
||||||
esphome/components/honeywell_hih_i2c/* @Benichou34
|
esphome/components/honeywell_hih_i2c/* @Benichou34
|
||||||
esphome/components/honeywellabp/* @RubyBailey
|
esphome/components/honeywellabp/* @RubyBailey
|
||||||
esphome/components/honeywellabp2_i2c/* @jpfaff
|
esphome/components/honeywellabp2_i2c/* @jpfaff
|
||||||
|
30
esphome/components/homeassistant/switch/__init__.py
Normal file
30
esphome/components/homeassistant/switch/__init__.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import esphome.codegen as cg
|
||||||
|
from esphome.components import switch
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.const import CONF_ID
|
||||||
|
|
||||||
|
from .. import (
|
||||||
|
HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA,
|
||||||
|
homeassistant_ns,
|
||||||
|
setup_home_assistant_entity,
|
||||||
|
)
|
||||||
|
|
||||||
|
CODEOWNERS = ["@Links2004"]
|
||||||
|
DEPENDENCIES = ["api"]
|
||||||
|
|
||||||
|
HomeassistantSwitch = homeassistant_ns.class_(
|
||||||
|
"HomeassistantSwitch", switch.Switch, cg.Component
|
||||||
|
)
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = (
|
||||||
|
switch.switch_schema(HomeassistantSwitch)
|
||||||
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
|
.extend(HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def to_code(config):
|
||||||
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
|
await cg.register_component(var, config)
|
||||||
|
await switch.register_switch(var, config)
|
||||||
|
setup_home_assistant_entity(var, config)
|
@ -0,0 +1,59 @@
|
|||||||
|
#include "homeassistant_switch.h"
|
||||||
|
#include "esphome/components/api/api_server.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace homeassistant {
|
||||||
|
|
||||||
|
static const char *const TAG = "homeassistant.switch";
|
||||||
|
|
||||||
|
using namespace esphome::switch_;
|
||||||
|
|
||||||
|
void HomeassistantSwitch::setup() {
|
||||||
|
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, nullopt, [this](const std::string &state) {
|
||||||
|
auto val = parse_on_off(state.c_str());
|
||||||
|
switch (val) {
|
||||||
|
case PARSE_NONE:
|
||||||
|
case PARSE_TOGGLE:
|
||||||
|
ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str());
|
||||||
|
break;
|
||||||
|
case PARSE_ON:
|
||||||
|
case PARSE_OFF:
|
||||||
|
bool new_state = val == PARSE_ON;
|
||||||
|
ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), ONOFF(new_state));
|
||||||
|
this->publish_state(new_state);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void HomeassistantSwitch::dump_config() {
|
||||||
|
LOG_SWITCH("", "Homeassistant Switch", this);
|
||||||
|
ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
float HomeassistantSwitch::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
|
||||||
|
|
||||||
|
void HomeassistantSwitch::write_state(bool state) {
|
||||||
|
if (!api::global_api_server->is_connected()) {
|
||||||
|
ESP_LOGE(TAG, "No clients connected to API server");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
api::HomeassistantServiceResponse resp;
|
||||||
|
if (state) {
|
||||||
|
resp.service = "switch.turn_on";
|
||||||
|
} else {
|
||||||
|
resp.service = "switch.turn_off";
|
||||||
|
}
|
||||||
|
|
||||||
|
api::HomeassistantServiceMap entity_id_kv;
|
||||||
|
entity_id_kv.key = "entity_id";
|
||||||
|
entity_id_kv.value = this->entity_id_;
|
||||||
|
resp.data.push_back(entity_id_kv);
|
||||||
|
|
||||||
|
api::global_api_server->send_homeassistant_service_call(resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace homeassistant
|
||||||
|
} // namespace esphome
|
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/components/switch/switch.h"
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace homeassistant {
|
||||||
|
|
||||||
|
class HomeassistantSwitch : public switch_::Switch, public Component {
|
||||||
|
public:
|
||||||
|
void set_entity_id(const std::string &entity_id) { this->entity_id_ = entity_id; }
|
||||||
|
void setup() override;
|
||||||
|
void dump_config() override;
|
||||||
|
float get_setup_priority() const override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void write_state(bool state) override;
|
||||||
|
std::string entity_id_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace homeassistant
|
||||||
|
} // namespace esphome
|
@ -32,6 +32,11 @@ wifi:
|
|||||||
|
|
||||||
api:
|
api:
|
||||||
|
|
||||||
|
switch:
|
||||||
|
- platform: homeassistant
|
||||||
|
entity_id: switch.my_cool_switch
|
||||||
|
id: my_cool_switch
|
||||||
|
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
- platform: homeassistant
|
- platform: homeassistant
|
||||||
entity_id: binary_sensor.hello_world
|
entity_id: binary_sensor.hello_world
|
||||||
|
Loading…
Reference in New Issue
Block a user