remote updates

This commit is contained in:
Otto Winter 2019-05-13 20:45:22 +02:00
parent 5bb963fa82
commit 60d67e5428
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
12 changed files with 88 additions and 25 deletions

View file

@ -5,7 +5,7 @@ from esphome.components import binary_sensor
from esphome.const import CONF_DATA, CONF_ID, 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_WAIT_TIME, CONF_TIMES, CONF_TYPE_ID, CONF_CARRIER_FREQUENCY
from esphome.core import coroutine
from esphome.py_compat import string_types, text_type
from esphome.util import Registry, SimpleRegistry
@ -359,7 +359,9 @@ def raw_dumper(var, config):
pass
@register_action('raw', RawAction, RAW_SCHEMA)
@register_action('raw', RawAction, RAW_SCHEMA.extend({
cv.Optional(CONF_CARRIER_FREQUENCY, default='0Hz'): cv.All(cv.frequency, cv.int_),
}))
def raw_action(var, config, args):
code_ = config[CONF_CODE]
if cg.is_template(code_):
@ -369,6 +371,8 @@ def raw_action(var, config, args):
code_ = config[CONF_CODE]
arr = cg.progmem_array(config[CONF_CODE_STORAGE_ID], code_)
cg.add(var.set_code_static(arr, len(code_)))
templ = yield cg.templatable(config[CONF_CARRIER_FREQUENCY], args, cg.uint32)
cg.add(var.set_carrier_frequency(templ))
# RC5
@ -476,6 +480,13 @@ RC_SWITCH_TYPE_D_SCHEMA = cv.Schema({
cv.Required(CONF_STATE): cv.boolean,
cv.Optional(CONF_PROTOCOL, default=1): RC_SWITCH_PROTOCOL_SCHEMA,
})
RC_SWITCH_TRANSMITTER = cv.Schema({
cv.Optional(CONF_REPEAT, default={CONF_TIMES: 5}): cv.Schema({
cv.Required(CONF_TIMES): cv.templatable(cv.positive_int),
cv.Optional(CONF_WAIT_TIME, default='10ms'):
cv.templatable(cv.positive_time_period_milliseconds),
}),
})
rc_switch_protocols = ns.rc_switch_protocols
RCSwitchBase = ns.class_('RCSwitchBase')
@ -494,7 +505,8 @@ def rc_switch_raw_binary_sensor(var, config):
cg.add(var.set_code(config[CONF_CODE]))
@register_action('rc_switch_raw', RCSwitchRawAction, RC_SWITCH_RAW_SCHEMA)
@register_action('rc_switch_raw', RCSwitchRawAction,
RC_SWITCH_RAW_SCHEMA.extend(RC_SWITCH_TRANSMITTER))
def rc_switch_raw_action(var, config, args):
proto = yield cg.templatable(config[CONF_PROTOCOL], args, RCSwitchBase,
to_exp=build_rc_switch_protocol)
@ -508,7 +520,8 @@ def rc_switch_type_a_binary_sensor(var, config):
cg.add(var.set_type_a(config[CONF_GROUP], config[CONF_DEVICE], config[CONF_STATE]))
@register_action('rc_switch_type_a', RCSwitchTypeAAction, RC_SWITCH_TYPE_A_SCHEMA)
@register_action('rc_switch_type_a', RCSwitchTypeAAction,
RC_SWITCH_TYPE_A_SCHEMA.extend(RC_SWITCH_TRANSMITTER))
def rc_switch_type_a_action(var, config, args):
proto = yield cg.templatable(config[CONF_PROTOCOL], args, RCSwitchBase,
to_exp=build_rc_switch_protocol)
@ -524,7 +537,8 @@ def rc_switch_type_b_binary_sensor(var, config):
cg.add(var.set_type_b(config[CONF_ADDRESS], config[CONF_CHANNEL], config[CONF_STATE]))
@register_action('rc_switch_type_b', RCSwitchTypeBAction, RC_SWITCH_TYPE_B_SCHEMA)
@register_action('rc_switch_type_b', RCSwitchTypeBAction,
RC_SWITCH_TYPE_B_SCHEMA.extend(RC_SWITCH_TRANSMITTER))
def rc_switch_type_b_action(var, config, args):
proto = yield cg.templatable(config[CONF_PROTOCOL], args, RCSwitchBase,
to_exp=build_rc_switch_protocol)
@ -541,7 +555,8 @@ def rc_switch_type_c_binary_sensor(var, config):
config[CONF_STATE]))
@register_action('rc_switch_type_c', RCSwitchTypeCAction, RC_SWITCH_TYPE_C_SCHEMA)
@register_action('rc_switch_type_c', RCSwitchTypeCAction,
RC_SWITCH_TYPE_C_SCHEMA.extend(RC_SWITCH_TRANSMITTER))
def rc_switch_type_c_action(var, config, args):
proto = yield cg.templatable(config[CONF_PROTOCOL], args, RCSwitchBase,
to_exp=build_rc_switch_protocol)
@ -552,13 +567,15 @@ def rc_switch_type_c_action(var, config, args):
cg.add(var.set_state((yield cg.templatable(config[CONF_STATE], args, bool))))
@register_binary_sensor('rc_switch_type_d', RCSwitchRawReceiver, RC_SWITCH_TYPE_D_SCHEMA)
@register_binary_sensor('rc_switch_type_d', RCSwitchRawReceiver,
RC_SWITCH_TYPE_D_SCHEMA.extend(RC_SWITCH_TRANSMITTER))
def rc_switch_type_d_binary_sensor(var, config):
cg.add(var.set_protocol(build_rc_switch_protocol(config[CONF_PROTOCOL])))
cg.add(var.set_type_d(config[CONF_GROUP], config[CONF_DEVICE], config[CONF_STATE]))
@register_action('rc_switch_type_d', RCSwitchTypeDAction, RC_SWITCH_TYPE_D_SCHEMA)
@register_action('rc_switch_type_d', RCSwitchTypeDAction,
RC_SWITCH_TYPE_D_SCHEMA.extend(RC_SWITCH_TRANSMITTER))
def rc_switch_type_d_action(var, config, args):
proto = yield cg.templatable(config[CONF_PROTOCOL], args, RCSwitchBase,
to_exp=build_rc_switch_protocol)

View file

@ -6,7 +6,7 @@ namespace remote_base {
static const char *TAG = "remote.raw";
void RawDumper::dump(RemoteReceiveData src) {
bool RawDumper::dump(RemoteReceiveData src) {
char buffer[256];
uint32_t buffer_offset = 0;
buffer_offset += sprintf(buffer, "Received Raw: ");
@ -16,7 +16,7 @@ void RawDumper::dump(RemoteReceiveData src) {
const uint32_t remaining_length = sizeof(buffer) - buffer_offset;
int written;
if (i + 1 < src.size()) {
if (i + 1 < src.size() - 1) {
written = snprintf(buffer + buffer_offset, remaining_length, "%d, ", value);
} else {
written = snprintf(buffer + buffer_offset, remaining_length, "%d", value);
@ -40,6 +40,7 @@ void RawDumper::dump(RemoteReceiveData src) {
if (buffer_offset != 0) {
ESP_LOGD(TAG, "%s", buffer);
}
return true;
}
} // namespace remote_base

View file

@ -44,9 +44,9 @@ template<typename... Ts> class RawAction : public RemoteTransmitterActionBase<Ts
this->code_static_ = code;
this->code_static_len_ = len;
}
TEMPLATABLE_VALUE(uint32_t, carrier_frequency);
void encode(RemoteTransmitData *dst, Ts... x) override {
// dst->set_data(data);
if (this->code_static_ != nullptr) {
for (size_t i = 0; i < this->code_static_len_; i++) {
auto val = this->code_static_[i];
@ -58,6 +58,7 @@ template<typename... Ts> class RawAction : public RemoteTransmitterActionBase<Ts
} else {
dst->set_data(this->code_func_(x...));
}
dst->set_carrier_frequency(this->carrier_frequency_.value(x...));
}
protected:
@ -68,7 +69,8 @@ template<typename... Ts> class RawAction : public RemoteTransmitterActionBase<Ts
class RawDumper : public RemoteReceiverDumperBase {
public:
void dump(RemoteReceiveData src) override;
bool dump(RemoteReceiveData src) override;
bool is_secondary() override { return true; }
};
} // namespace remote_base

View file

@ -53,6 +53,7 @@ void RCSwitchBase::sync(RemoteTransmitData *dst) const {
}
}
void RCSwitchBase::transmit(RemoteTransmitData *dst, uint32_t code, uint8_t len) const {
dst->set_carrier_frequency(0);
for (int16_t i = len - 1; i >= 0; i--) {
if (code & (1 << i))
this->one(dst);
@ -224,21 +225,25 @@ bool RCSwitchRawReceiver::matches(RemoteReceiveData src) {
return decoded_nbits == this->nbits_ && decoded_code == this->code_;
}
void RCSwitchDumper::dump(RemoteReceiveData src) {
bool RCSwitchDumper::dump(RemoteReceiveData src) {
for (uint8_t i = 1; i <= 7; i++) {
src.reset();
uint32_t out_data;
uint8_t out_nbits;
RCSwitchBase *protocol = &rc_switch_protocols[i];
if (protocol->decode(src, &out_data, &out_nbits)) {
if (protocol->decode(src, &out_data, &out_nbits) && out_nbits >= 3) {
char buffer[32];
for (uint8_t j = 0; j < out_nbits; j++)
buffer[j] = (out_data & (1 << (out_nbits - j - 1))) ? '1' : '0';
buffer[out_nbits] = '\0';
ESP_LOGD(TAG, "Received RCSwitch Raw: protocol=%u data='%s'", i, buffer);
// only send first decoded protocol
return true;
}
}
return false;
}
} // namespace remote_base

View file

@ -197,7 +197,7 @@ class RCSwitchRawReceiver : public RemoteReceiverBinarySensorBase {
class RCSwitchDumper : public RemoteReceiverDumperBase {
public:
void dump(RemoteReceiveData src) override;
bool dump(RemoteReceiveData src) override;
};
} // namespace remote_base

View file

@ -212,14 +212,21 @@ class RemoteReceiverListener {
class RemoteReceiverDumperBase {
public:
virtual void dump(RemoteReceiveData src) = 0;
virtual bool dump(RemoteReceiveData src) = 0;
virtual bool is_secondary() { return false; }
};
class RemoteReceiverBase : public RemoteComponentBase {
public:
RemoteReceiverBase(GPIOPin *pin) : RemoteComponentBase(pin) {}
void register_listener(RemoteReceiverListener *listener) { this->listeners_.push_back(listener); }
void register_dumper(RemoteReceiverDumperBase *dumper) { this->dumpers_.push_back(dumper); }
void register_dumper(RemoteReceiverDumperBase *dumper) {
if (dumper->is_secondary()) {
this->secondary_dumpers_.push_back(dumper);
} else {
this->dumpers_.push_back(dumper);
}
}
void set_tolerance(uint8_t tolerance) { tolerance_ = tolerance; }
protected:
@ -233,11 +240,19 @@ class RemoteReceiverBase : public RemoteComponentBase {
return success;
}
void call_dumpers_() {
bool success = false;
for (auto *dumper : this->dumpers_) {
auto data = RemoteReceiveData(&this->temp_, this->tolerance_);
if (dumper->dump(data))
success = true;
}
if (!success) {
for (auto *dumper : this->secondary_dumpers_) {
auto data = RemoteReceiveData(&this->temp_, this->tolerance_);
dumper->dump(data);
}
}
}
void call_listeners_dumpers_() {
if (this->call_listeners_())
return;
@ -247,6 +262,7 @@ class RemoteReceiverBase : public RemoteComponentBase {
std::vector<RemoteReceiverListener *> listeners_;
std::vector<RemoteReceiverDumperBase *> dumpers_;
std::vector<RemoteReceiverDumperBase *> secondary_dumpers_;
std::vector<int32_t> temp_;
uint8_t tolerance_{25};
};
@ -323,12 +339,13 @@ template<typename... Ts> class RemoteTransmitterActionBase : public Action<Ts...
template<typename T, typename D> class RemoteReceiverDumper : public RemoteReceiverDumperBase {
public:
void dump(RemoteReceiveData src) override {
bool dump(RemoteReceiveData src) override {
auto proto = T();
auto decoded = proto.decode(src);
if (!decoded.has_value())
return;
return false;
proto.dump(*decoded);
return true;
}
};

View file

@ -53,6 +53,10 @@ void RemoteReceiverComponent::setup() {
void RemoteReceiverComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Remote Receiver:");
LOG_PIN(" Pin: ", this->pin_);
if (this->pin_->digital_read()) {
ESP_LOGW(TAG, "Remote Receiver Signal starts with a HIGH value. Usually this means you have to "
"invert the signal using 'inverted: True' in the pin schema!");
}
ESP_LOGCONFIG(TAG, " Channel: %d", this->channel_);
ESP_LOGCONFIG(TAG, " Clock divider: %u", this->clock_divider_);
ESP_LOGCONFIG(TAG, " Tolerance: %u%%", this->tolerance_);

View file

@ -5,10 +5,10 @@ from esphome.const import CONF_CHANNEL, CONF_ID
from . import ttp229_bsf_ns, TTP229BSFComponent, CONF_TTP229_ID
DEPENDENCIES = ['ttp229_bsf']
TTP229Channel = ttp229_bsf_ns.class_('TTP229Channel', binary_sensor.BinarySensor)
TTP229BSFChannel = ttp229_bsf_ns.class_('TTP229BSFChannel', binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(TTP229Channel),
cv.GenerateID(): cv.declare_id(TTP229BSFChannel),
cv.GenerateID(CONF_TTP229_ID): cv.use_id(TTP229BSFComponent),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=15),
})

View file

@ -227,6 +227,11 @@ def int_(value):
check_not_templatable(value)
if isinstance(value, integer_types):
return value
if isinstance(value, float):
if int(value) == value:
return int(value)
raise Invalid("This option only accepts integers with no fractional part. Please remove "
"the fractional part from {}".format(value))
value = string_strict(value).lower()
base = 10
if value.startswith('0x'):

12
script/fulltest Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")/.."
set -x
script/ci-custom.py
script/lint-python
script/lint-cpp
script/test

View file

@ -12,5 +12,5 @@ fi
set -x
script/clang-tidy -c --fix --all-headers
script/clang-format -c -i
script/clang-tidy $@ --fix --all-headers
script/clang-format $@ -i

View file

@ -8,4 +8,4 @@ set -x
script/ci-custom.py
script/lint-python -c
script/lint-cpp
script/lint-cpp -c