Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ca241615d | ||
|
|
b8aa84002a | ||
|
|
10cc0b1d5b | ||
|
|
11d9c203c1 | ||
|
|
c9ab454c3c | ||
|
|
4a55692885 | ||
|
|
88c129e705 | ||
|
|
80b48f01fb |
@@ -50,6 +50,7 @@ def globals_set_to_code(config, action_id, template_arg, args):
|
||||
full_id, paren = yield cg.get_variable_with_full_id(config[CONF_ID])
|
||||
template_arg = cg.TemplateArguments(full_id.type, *template_arg)
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
templ = yield cg.templatable(config[CONF_VALUE], args, None)
|
||||
templ = yield cg.templatable(config[CONF_VALUE], args, None,
|
||||
to_exp=cg.RawExpression)
|
||||
cg.add(var.set_value(templ))
|
||||
yield var
|
||||
|
||||
30
esphome/components/mqtt/custom_mqtt_device.cpp
Normal file
30
esphome/components/mqtt/custom_mqtt_device.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "custom_mqtt_device.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
static const char *TAG = "mqtt.custom";
|
||||
|
||||
bool CustomMQTTDevice::publish(const std::string &topic, const std::string &payload, uint8_t qos, bool retain) {
|
||||
return global_mqtt_client->publish(topic, payload, qos, retain);
|
||||
}
|
||||
bool CustomMQTTDevice::publish(const std::string &topic, float value, int8_t number_decimals) {
|
||||
auto str = value_accuracy_to_string(value, number_decimals);
|
||||
return this->publish(topic, str);
|
||||
}
|
||||
bool CustomMQTTDevice::publish(const std::string &topic, int value) {
|
||||
char buffer[24];
|
||||
sprintf(buffer, "%d", value);
|
||||
return this->publish(topic, buffer);
|
||||
}
|
||||
bool CustomMQTTDevice::publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos, bool retain) {
|
||||
return global_mqtt_client->publish_json(topic, f, qos, retain);
|
||||
}
|
||||
bool CustomMQTTDevice::publish_json(const std::string &topic, const json::json_build_t &f) {
|
||||
return this->publish_json(topic, f, 0, false);
|
||||
}
|
||||
bool CustomMQTTDevice::is_connected() { return global_mqtt_client != nullptr && global_mqtt_client->is_connected(); }
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
217
esphome/components/mqtt/custom_mqtt_device.h
Normal file
217
esphome/components/mqtt/custom_mqtt_device.h
Normal file
@@ -0,0 +1,217 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "mqtt_client.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
/** This class is a helper class for custom components that communicate using
|
||||
* MQTT. It has 5 helper functions that you can use (square brackets indicate optional):
|
||||
*
|
||||
* - `subscribe(topic, function_pointer, [qos])`
|
||||
* - `subscribe_json(topic, function_pointer, [qos])`
|
||||
* - `publish(topic, payload, [qos], [retain])`
|
||||
* - `publish_json(topic, payload_builder, [qos], [retain])`
|
||||
* - `is_connected()`
|
||||
*/
|
||||
class CustomMQTTDevice {
|
||||
public:
|
||||
/** Subscribe to an MQTT topic with the given Quality of Service.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* class MyCustomMQTTDevice : public Component, public mqtt:CustomMQTTDevice {
|
||||
* public:
|
||||
* void setup() override {
|
||||
* subscribe("the/topic", &MyCustomMQTTDevice::on_message);
|
||||
* pinMode(5, OUTPUT);
|
||||
* }
|
||||
*
|
||||
* // topic and payload parameters can be removed if not needed
|
||||
* // e.g: void on_message() {
|
||||
*
|
||||
* void on_message(const std::string &topic, const std::string &payload) {
|
||||
* // do something with topic and payload
|
||||
* if (payload == "ON") {
|
||||
* digitalWrite(5, HIGH);
|
||||
* } else {
|
||||
* digitalWrite(5, LOW);
|
||||
* }
|
||||
* }
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* @tparam T A C++ template argument for determining the type of the callback.
|
||||
* @param topic The topic to subscribe to. Re-subscription on re-connects is automatically handled.
|
||||
* @param callback The callback (must be a class member) to subscribe with.
|
||||
* @param qos The Quality of Service to subscribe with. Defaults to 0.
|
||||
*/
|
||||
template<typename T>
|
||||
void subscribe(const std::string &topic, void (T::*callback)(const std::string &, const std::string &),
|
||||
uint8_t qos = 0);
|
||||
|
||||
template<typename T>
|
||||
void subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos = 0);
|
||||
|
||||
template<typename T> void subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos = 0);
|
||||
|
||||
/** Subscribe to an MQTT topic and call the callback if the payload can be decoded
|
||||
* as JSON with the given Quality of Service.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* class MyCustomMQTTDevice : public Component, public mqtt:CustomMQTTDevice {
|
||||
* public:
|
||||
* void setup() override {
|
||||
* subscribe_json("the/topic", &MyCustomMQTTDevice::on_json_message);
|
||||
* pinMode(5, OUTPUT);
|
||||
* }
|
||||
*
|
||||
* // topic parameter can be remove if not needed:
|
||||
* // e.g.: void on_json_message(JsonObject &payload) {
|
||||
*
|
||||
* void on_json_message(const std::string &topic, JsonObject &payload) {
|
||||
* // do something with topic and payload
|
||||
* if (payload["number"] == 1) {
|
||||
* digitalWrite(5, HIGH);
|
||||
* } else {
|
||||
* digitalWrite(5, LOW);
|
||||
* }
|
||||
* }
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* @tparam T A C++ template argument for determining the type of the callback.
|
||||
* @param topic The topic to subscribe to. Re-subscription on re-connects is automatically handled.
|
||||
* @param callback The callback (must be a class member) to subscribe with.
|
||||
* @param qos The Quality of Service to subscribe with. Defaults to 0.
|
||||
*/
|
||||
template<typename T>
|
||||
void subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject &),
|
||||
uint8_t qos = 0);
|
||||
|
||||
template<typename T>
|
||||
void subscribe_json(const std::string &topic, void (T::*callback)(JsonObject &), uint8_t qos = 0);
|
||||
|
||||
/** Publish an MQTT message with the given payload and QoS and retain settings.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* void in_some_method() {
|
||||
* publish("the/topic", "The Payload", 0, true);
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param topic The topic to publish to.
|
||||
* @param payload The payload to publish.
|
||||
* @param qos The Quality of Service to publish with. Defaults to 0
|
||||
* @param retain Whether to retain the message. Defaults to false.
|
||||
*/
|
||||
bool publish(const std::string &topic, const std::string &payload, uint8_t qos = 0, bool retain = false);
|
||||
|
||||
/** Publish an MQTT message with the given floating point number and number of decimals.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* void in_some_method() {
|
||||
* publish("the/topic", 1.0);
|
||||
* // with two digits after the decimal point
|
||||
* publish("the/topic", 1.0, 2);
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param topic The topic to publish to.
|
||||
* @param payload The payload to publish.
|
||||
* @param number_decimals The number of digits after the decimal point to round to, defaults to 3 digits.
|
||||
*/
|
||||
bool publish(const std::string &topic, float value, int8_t number_decimals = 3);
|
||||
|
||||
/** Publish an MQTT message with the given integer as payload.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* void in_some_method() {
|
||||
* publish("the/topic", 42);
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param topic The topic to publish to.
|
||||
* @param payload The payload to publish.
|
||||
*/
|
||||
bool publish(const std::string &topic, int value);
|
||||
|
||||
/** Publish a JSON-encoded MQTT message with the given Quality of Service and retain settings.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* void in_some_method() {
|
||||
* publish("the/topic", [=](JsonObject &root) {
|
||||
* root["the_key"] = "Hello World!";
|
||||
* }, 0, false);
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param topic The topic to publish to.
|
||||
* @param payload The payload to publish.
|
||||
* @param qos The Quality of Service to publish with.
|
||||
* @param retain Whether to retain the message.
|
||||
*/
|
||||
bool publish_json(const std::string &topic, const json::json_build_t &f, uint8_t qos, bool retain);
|
||||
|
||||
/** Publish a JSON-encoded MQTT message.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```cpp
|
||||
* void in_some_method() {
|
||||
* publish("the/topic", [=](JsonObject &root) {
|
||||
* root["the_key"] = "Hello World!";
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param topic The topic to publish to.
|
||||
* @param payload The payload to publish.
|
||||
*/
|
||||
bool publish_json(const std::string &topic, const json::json_build_t &f);
|
||||
|
||||
/// Check whether the MQTT client is currently connected and messages can be published.
|
||||
bool is_connected();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void CustomMQTTDevice::subscribe(const std::string &topic,
|
||||
void (T::*callback)(const std::string &, const std::string &), uint8_t qos) {
|
||||
auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2);
|
||||
global_mqtt_client->subscribe(topic, f, qos);
|
||||
}
|
||||
template<typename T>
|
||||
void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos) {
|
||||
auto f = std::bind(callback, (T *) this, std::placeholders::_2);
|
||||
global_mqtt_client->subscribe(topic, f, qos);
|
||||
}
|
||||
template<typename T> void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos) {
|
||||
auto f = std::bind(callback, (T *) this);
|
||||
global_mqtt_client->subscribe(topic, f, qos);
|
||||
}
|
||||
template<typename T>
|
||||
void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject &),
|
||||
uint8_t qos) {
|
||||
auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2);
|
||||
global_mqtt_client->subscribe_json(topic, f, qos);
|
||||
}
|
||||
template<typename T>
|
||||
void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(JsonObject &), uint8_t qos) {
|
||||
auto f = std::bind(callback, (T *) this, std::placeholders::_2);
|
||||
global_mqtt_client->subscribe_json(topic, f, qos);
|
||||
}
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
@@ -2,7 +2,7 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_DATA, CONF_ID, CONF_TRIGGER_ID, CONF_NBITS, CONF_ADDRESS, \
|
||||
from esphome.const import CONF_DATA, CONF_TRIGGER_ID, CONF_NBITS, CONF_ADDRESS, \
|
||||
CONF_COMMAND, CONF_CODE, CONF_PULSE_LENGTH, CONF_SYNC, CONF_ZERO, CONF_ONE, CONF_INVERTED, \
|
||||
CONF_PROTOCOL, CONF_GROUP, CONF_DEVICE, CONF_STATE, CONF_CHANNEL, CONF_FAMILY, CONF_REPEAT, \
|
||||
CONF_WAIT_TIME, CONF_TIMES, CONF_TYPE_ID, CONF_CARRIER_FREQUENCY
|
||||
@@ -350,7 +350,7 @@ RAW_SCHEMA = cv.Schema({
|
||||
@register_binary_sensor('raw', RawBinarySensor, RAW_SCHEMA)
|
||||
def raw_binary_sensor(var, config):
|
||||
code_ = config[CONF_CODE]
|
||||
arr = cg.progmem_array(config[CONF_ID], code_)
|
||||
arr = cg.progmem_array(config[CONF_CODE_STORAGE_ID], code_)
|
||||
cg.add(var.set_data(arr))
|
||||
cg.add(var.set_len(len(code_)))
|
||||
|
||||
|
||||
@@ -232,7 +232,7 @@ bool RCSwitchDumper::dump(RemoteReceiveData src) {
|
||||
uint8_t out_nbits;
|
||||
RCSwitchBase *protocol = &rc_switch_protocols[i];
|
||||
if (protocol->decode(src, &out_data, &out_nbits) && out_nbits >= 3) {
|
||||
char buffer[32];
|
||||
char buffer[33];
|
||||
for (uint8_t j = 0; j < out_nbits; j++)
|
||||
buffer[j] = (out_data & (1 << (out_nbits - j - 1))) ? '1' : '0';
|
||||
|
||||
|
||||
@@ -52,6 +52,15 @@ def _tz_dst_str(dt):
|
||||
_tz_timedelta(td))
|
||||
|
||||
|
||||
def _non_dst_tz(tz, dt):
|
||||
tzname = tz.tzname(dt)
|
||||
utcoffset = tz.utcoffset(dt)
|
||||
_LOGGER.info("Detected timezone '%s' with UTC offset %s",
|
||||
tzname, _tz_timedelta(utcoffset))
|
||||
tzbase = '{}{}'.format(tzname, _tz_timedelta(-1 * utcoffset))
|
||||
return tzbase
|
||||
|
||||
|
||||
def convert_tz(pytz_obj):
|
||||
tz = pytz_obj
|
||||
|
||||
@@ -59,24 +68,14 @@ def convert_tz(pytz_obj):
|
||||
first_january = datetime.datetime(year=now.year, month=1, day=1)
|
||||
|
||||
if not isinstance(tz, pytz.tzinfo.DstTzInfo):
|
||||
tzname = tz.tzname(first_january)
|
||||
utcoffset = tz.utcoffset(first_january)
|
||||
_LOGGER.info("Detected timezone '%s' with UTC offset %s",
|
||||
tzname, _tz_timedelta(utcoffset))
|
||||
tzbase = '{}{}'.format(tzname, _tz_timedelta(-1 * utcoffset))
|
||||
return tzbase
|
||||
return _non_dst_tz(tz, first_january)
|
||||
|
||||
# pylint: disable=protected-access
|
||||
transition_times = tz._utc_transition_times
|
||||
transition_info = tz._transition_info
|
||||
idx = max(0, bisect.bisect_right(transition_times, now))
|
||||
if idx >= len(transition_times):
|
||||
tzname = tz.tzname(now)
|
||||
utcoffset = tz.utcoffset(now)
|
||||
_LOGGER.info("Detected timezone '%s' with UTC offset %s",
|
||||
tzname, _tz_timedelta(utcoffset))
|
||||
tzbase = '{}{}'.format(tzname, _tz_timedelta(-1 * utcoffset))
|
||||
return tzbase
|
||||
return _non_dst_tz(tz, now)
|
||||
|
||||
idx1, idx2 = idx, idx + 1
|
||||
dstoffset1 = transition_info[idx1][1]
|
||||
@@ -84,6 +83,14 @@ def convert_tz(pytz_obj):
|
||||
# Normalize to 1 being DST on
|
||||
idx1, idx2 = idx + 1, idx + 2
|
||||
|
||||
if idx2 >= len(transition_times):
|
||||
return _non_dst_tz(tz, now)
|
||||
|
||||
if transition_times[idx2].year > now.year + 1:
|
||||
# Next transition is scheduled after this year
|
||||
# Probably a scheduler timezone change.
|
||||
return _non_dst_tz(tz, now)
|
||||
|
||||
utcoffset_on, _, tzname_on = transition_info[idx1]
|
||||
utcoffset_off, _, tzname_off = transition_info[idx2]
|
||||
dst_begins_utc = transition_times[idx1]
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
MAJOR_VERSION = 1
|
||||
MINOR_VERSION = 13
|
||||
PATCH_VERSION = '5'
|
||||
PATCH_VERSION = '6'
|
||||
__short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION)
|
||||
__version__ = '{}.{}'.format(__short_version__, PATCH_VERSION)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#endif
|
||||
|
||||
#include "esphome/core/esphal.h"
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@ def copy_src_tree():
|
||||
continue
|
||||
# Transform path to target path name
|
||||
target = os.path.relpath(path, CORE.relative_src_path()).replace(os.path.sep, '/')
|
||||
if target == DEFINES_H_TARGET:
|
||||
if target in (DEFINES_H_TARGET, VERSION_H_TARGET):
|
||||
# Ignore defines.h, will be dealt with later
|
||||
continue
|
||||
if target not in source_files_copy:
|
||||
@@ -341,7 +341,7 @@ def copy_src_tree():
|
||||
write_file_if_changed(ESPHOME_H_FORMAT.format(include_s),
|
||||
CORE.relative_src_path('esphome.h'))
|
||||
write_file_if_changed(VERSION_H_FORMAT.format(__version__),
|
||||
CORE.relative_src_path('esphome' 'core', 'version.h'))
|
||||
CORE.relative_src_path('esphome', 'core', 'version.h'))
|
||||
|
||||
|
||||
def generate_defines_h():
|
||||
|
||||
@@ -661,6 +661,13 @@ binary_sensor:
|
||||
number: 1
|
||||
mode: INPUT
|
||||
inverted: True
|
||||
- platform: remote_receiver
|
||||
name: "Raw Remote Receiver Test"
|
||||
raw:
|
||||
code: [5685, -4252, 1711, -2265, 1712, -2265, 1711, -2264, 1712, -2266,
|
||||
3700, -2263, 1712, -4254, 1711, -4249, 1715, -2266, 1710, -2267,
|
||||
1709, -2265, 3704, -4250, 1712, -4254, 3700, -2260, 1714, -2265,
|
||||
1712, -2262, 1714, -2267, 1709]
|
||||
|
||||
pca9685:
|
||||
frequency: 500
|
||||
@@ -1276,6 +1283,9 @@ text_sensor:
|
||||
id: template_text
|
||||
state: |-
|
||||
return "Hello World2";
|
||||
- globals.set:
|
||||
id: glob_int
|
||||
value: '0'
|
||||
- platform: template
|
||||
name: Template Text Sensor
|
||||
id: template_text
|
||||
|
||||
Reference in New Issue
Block a user