mansellrace

Untitled

Sep 1st, 2023
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 3.50 KB | None | 0 0
  1. remote_receiver:
  2.   pin:
  3.     number: D6
  4.     inverted: false
  5.   # dump: raw
  6.   idle: 10ms
  7.   filter: 250us
  8.   buffer_size: 3k
  9.   on_raw:
  10.     then:
  11.       - lambda: |-
  12.           id(codice) = 0;
  13.           id(indirizzo) = 0;
  14.           ESP_LOGV("DEBUG", "RICEVUTO CODICE RAW DI LUNGHEZZA %u", size(x));
  15.           char message[30];
  16.           int bits = 0;
  17.           if (size(x) >= 38 && size(x) <= 50) {
  18.             for (auto it = x.begin() + 1; it != x.end() -1; it+=2) {
  19.               if (-*it < 2500 && -*it > 1000)
  20.               {
  21.                 message[bits] = 0;
  22.                 bits += 1;
  23.               }
  24.               else if (-*it < 6200 && -*it > 3500)
  25.               {
  26.                 message[bits] = 1;
  27.                 bits += 1;
  28.               }
  29.               if (bits == 18) {
  30.                 int sum = 0;
  31.                 int checksum = 0;
  32.                 for (int i = 0; i < 14; i++)
  33.                 {
  34.                   if (message[i] == 1) sum++;
  35.                 }
  36.                 checksum = (message[17] * 8) + (message[16] * 4) + (message[15] * 2) + message[14];
  37.  
  38.                 if (checksum == sum) {
  39.                   int msgCode[6];
  40.                   int msgAddr[8];
  41.                   for (int j = 0; j < 14; j++)
  42.                   {
  43.                     if (j < 6) msgCode[j] = message[j];
  44.                     else msgAddr[j - 6] = message[j];
  45.                   }
  46.                   id(codice) = (msgCode[5] * 32) + (msgCode[4] * 16) + (msgCode[3] * 8) + (msgCode[2] * 4) + (msgCode[1] * 2) + msgCode[0];
  47.                   id(indirizzo) = (msgAddr[7] * 128) + (msgAddr[6] * 64) + (msgAddr[5] * 32) + (msgAddr[4] * 16) + (msgAddr[3] * 8) + (msgAddr[2] * 4) + (msgAddr[1] * 2) + msgAddr[0];
  48.                   if (id(codice) < 63)
  49.                   {
  50.                     ESP_LOGD("DEBUG", "RICEVUTO COMANDO = %i", id(codice));
  51.                     ESP_LOGD("DEBUG", "RICEVUTO INDIRIZZO = %i", id(indirizzo));
  52.                     ESP_LOGD("DEBUG", "CHECKSUM OK");
  53.                     auto capi = new esphome::api::CustomAPIDevice();
  54.                     capi->fire_homeassistant_event("esphome.comelit_ricevuto", {{"indirizzo", std::to_string(id(indirizzo))}, {"comando", std::to_string(id(codice))}});
  55.                   } else {
  56.                     id(codice) = 0;
  57.                     id(indirizzo) = 0;
  58.                   }
  59.                 }
  60.               }
  61.             }
  62.           }
  63.       - if:
  64.           condition:
  65.             lambda: 'return (id(codice) > 0) && (id(indirizzo) > 0);'
  66.           then:
  67.             - lambda: |-
  68.                 std::string str = "";
  69.                 str = "codice " + to_string(id(codice)) + " indirizzo " + to_string(id(indirizzo));
  70.                 id(descrizione) = str;
  71.                 id(codice) = 0;
  72.                 id(indirizzo) = 0;
  73.             - if:
  74.                 condition:
  75.                     lambda: 'return id(descrizione) != "";'
  76.                 then:
  77.                   - logger.log:
  78.                       format: "Descrizione codice: %s"
  79.                       args: ['id(descrizione).c_str()']
  80.                   - homeassistant.service:
  81.                       service: logbook.log
  82.                       data:
  83.                         name: "Citofono:"
  84.                         entity_id: "binary_sensor.comelit_stato"
  85.                       data_template:
  86.                         message: "{{ com }}"
  87.                       variables:
  88.                         com: 'return id(descrizione).c_str();'
  89.                   - lambda: 'id(descrizione) = "";'
Advertisement
Add Comment
Please, Sign In to add comment