Advertisement
penright

Untitled

Jan 11th, 2022
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "esphome/core/component.h"
  4. #include "esphome/components/sensor/sensor.h"
  5. #include "esphome/components/binary_sensor/binary_sensor.h"
  6. #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
  7.  
  8. #ifdef USE_ESP32
  9.  
  10. namespace esphome {
  11. namespace pvvx_mithermometer {
  12.  
  13. struct ParseResult {
  14.   optional<float> temperature;
  15.   optional<float> humidity;
  16.   optional<float> battery_level;
  17.   optional<float> battery_voltage;
  18.   optional<int> flag_value;
  19.   optional<bool> reed_switch;
  20.   int raw_offset;
  21. };
  22. class PVVXMiThermometer : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
  23.  public:
  24.   void set_address(uint64_t address) { address_ = address; };
  25.  
  26.   bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
  27.   void dump_config() override;
  28.   float get_setup_priority() const override { return setup_priority::DATA; }
  29.   void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
  30.   void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
  31.   void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
  32.   void set_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; }
  33.   void set_flag_value(sensor::Sensor *flag_value) { flag_value_ = flag_value; }
  34.   void set_reed_switch(binary_sensor::BinarySensor *reed_switch) { reed_switch_ = reed_switch; }
  35.  
  36.  protected:
  37.   uint64_t address_;
  38.   sensor::Sensor *temperature_{nullptr};
  39.   sensor::Sensor *humidity_{nullptr};
  40.   sensor::Sensor *battery_level_{nullptr};
  41.   sensor::Sensor *battery_voltage_{nullptr};
  42.   sensor::Sensor *flag_value_{nullptr};
  43.   binary_sensor::BinarySensor *reed_switch_{nullptr};
  44.  
  45.   optional<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
  46.   bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);
  47.   bool report_results_(const optional<ParseResult> &result, const std::string &address);
  48. };
  49.  
  50. }  // namespace pvvx_mithermometer
  51. }  // namespace esphome
  52.  
  53. #endif
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement