-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into homeassistant-number
- Loading branch information
Showing
50 changed files
with
527 additions
and
35 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
Validating CODEOWNERS rules …
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
59 changes: 59 additions & 0 deletions
59
esphome/components/homeassistant/switch/homeassistant_switch.cpp
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,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 |
22 changes: 22 additions & 0 deletions
22
esphome/components/homeassistant/switch/homeassistant_switch.h
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,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 |
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
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
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
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
Oops, something went wrong.