refactored xiaomi sensors (#755)

* refactored xiaomi sensors

* fix lint

* fixed and added tests

* fix namespace

* LYWSD02 has no battery level

* fixed enum

* fix

* fix case

* fix spaces in empty line...

* inform users of old sensors about the change
This commit is contained in:
Alexander Leisentritt 2019-10-16 13:29:56 +02:00 committed by Otto Winter
parent cdb9c59662
commit cdfbe5b523
17 changed files with 159 additions and 139 deletions

View file

@ -81,17 +81,17 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
return {};
}
bool is_mijia = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01;
bool is_miflora = (raw[1] & 0x20) == 0x20 && raw[2] == 0x98 && raw[3] == 0x00;
bool is_lywsdcgq = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01;
bool is_hhccjcy01 = (raw[1] & 0x20) == 0x20 && raw[2] == 0x98 && raw[3] == 0x00;
bool is_lywsd02 = (raw[1] & 0x20) == 0x20 && raw[2] == 0x5b && raw[3] == 0x04;
bool is_cleargrass = (raw[1] & 0x30) == 0x30 && raw[2] == 0x47 && raw[3] == 0x03;
bool is_cgg1 = (raw[1] & 0x30) == 0x30 && raw[2] == 0x47 && raw[3] == 0x03;
if (!is_mijia && !is_miflora && !is_lywsd02 && !is_cleargrass) {
if (!is_lywsdcgq && !is_hhccjcy01 && !is_lywsd02 && !is_cgg1) {
// ESP_LOGVV(TAG, "Xiaomi no magic bytes");
return {};
}
uint8_t raw_offset = is_mijia || is_cleargrass ? 11 : 12;
uint8_t raw_offset = is_lywsdcgq || is_cgg1 ? 11 : 12;
const uint8_t raw_type = raw[raw_offset];
const uint8_t data_length = raw[raw_offset + 2];
@ -103,13 +103,13 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
return {};
}
XiaomiParseResult result;
result.type = XiaomiParseResult::TYPE_MIFLORA;
if (is_mijia) {
result.type = XiaomiParseResult::TYPE_MIJIA;
result.type = XiaomiParseResult::TYPE_HHCCJCY01;
if (is_lywsdcgq) {
result.type = XiaomiParseResult::TYPE_LYWSDCGQ;
} else if (is_lywsd02) {
result.type = XiaomiParseResult::TYPE_LYWSD02;
} else if (is_cleargrass) {
result.type = XiaomiParseResult::TYPE_CLEARGRASS;
} else if (is_cgg1) {
result.type = XiaomiParseResult::TYPE_CGG1;
}
bool success = parse_xiaomi_data_byte(raw_type, data, data_length, result);
if (!success)
@ -122,12 +122,12 @@ bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
if (!res.has_value())
return false;
const char *name = "Mi Flora";
if (res->type == XiaomiParseResult::TYPE_MIJIA) {
name = "Mi Jia";
const char *name = "HHCCJCY01";
if (res->type == XiaomiParseResult::TYPE_LYWSDCGQ) {
name = "LYWSDCGQ";
} else if (res->type == XiaomiParseResult::TYPE_LYWSD02) {
name = "LYWSD02";
} else if (res->type == XiaomiParseResult::TYPE_CLEARGRASS) {
} else if (res->type == XiaomiParseResult::TYPE_CGG1) {
name = "Cleargrass";
}

View file

@ -9,7 +9,7 @@ namespace esphome {
namespace xiaomi_ble {
struct XiaomiParseResult {
enum { TYPE_MIJIA, TYPE_MIFLORA, TYPE_LYWSD02, TYPE_CLEARGRASS } type;
enum { TYPE_LYWSDCGQ, TYPE_HHCCJCY01, TYPE_LYWSD02, TYPE_CGG1 } type;
optional<float> temperature;
optional<float> humidity;
optional<float> battery_level;

View file

@ -7,12 +7,12 @@ from esphome.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, C
DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble']
xiaomi_cleargrass_ns = cg.esphome_ns.namespace('xiaomi_cleargrass')
XiaomiCleargrass = xiaomi_cleargrass_ns.class_(
'XiaomiCleargrass', esp32_ble_tracker.ESPBTDeviceListener, cg.Component)
xiaomi_cgg1_ns = cg.esphome_ns.namespace('xiaomi_cgg1')
XiaomiCGG1 = xiaomi_cgg1_ns.class_(
'XiaomiCGG1', esp32_ble_tracker.ESPBTDeviceListener, cg.Component)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(XiaomiCleargrass),
cv.GenerateID(): cv.declare_id(XiaomiCGG1),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 1),

View file

@ -1,21 +1,21 @@
#include "xiaomi_mijia.h"
#include "xiaomi_cgg1.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_mijia {
namespace xiaomi_cgg1 {
static const char *TAG = "xiaomi_mijia";
static const char *TAG = "xiaomi_cgg1";
void XiaomiMijia::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi Mijia");
void XiaomiCGG1::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi CGG1");
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Humidity", this->humidity_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
}
} // namespace xiaomi_mijia
} // namespace xiaomi_cgg1
} // namespace esphome
#endif

View file

@ -8,9 +8,9 @@
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_mijia {
namespace xiaomi_cgg1 {
class XiaomiMijia : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
class XiaomiCGG1 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
void set_address(uint64_t address) { address_ = address; }
@ -44,7 +44,7 @@ class XiaomiMijia : public Component, public esp32_ble_tracker::ESPBTDeviceListe
sensor::Sensor *battery_level_{nullptr};
};
} // namespace xiaomi_mijia
} // namespace xiaomi_cgg1
} // namespace esphome
#endif

View file

@ -0,0 +1,49 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, esp32_ble_tracker
from esphome.const import CONF_BATTERY_LEVEL, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \
UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID, \
CONF_MOISTURE, CONF_ILLUMINANCE, ICON_BRIGHTNESS_5, UNIT_LUX, CONF_CONDUCTIVITY, \
UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER
DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble']
xiaomi_hhccjcy01_ns = cg.esphome_ns.namespace('xiaomi_hhccjcy01')
XiaomiHHCCJCY01 = xiaomi_hhccjcy01_ns.class_('XiaomiHHCCJCY01',
esp32_ble_tracker.ESPBTDeviceListener, cg.Component)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(XiaomiHHCCJCY01),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1),
cv.Optional(CONF_MOISTURE): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 0),
cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(UNIT_LUX, ICON_BRIGHTNESS_5, 0),
cv.Optional(CONF_CONDUCTIVITY):
sensor.sensor_schema(UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER, 0),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield esp32_ble_tracker.register_ble_device(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
if CONF_TEMPERATURE in config:
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
if CONF_MOISTURE in config:
sens = yield sensor.new_sensor(config[CONF_MOISTURE])
cg.add(var.set_moisture(sens))
if CONF_ILLUMINANCE in config:
sens = yield sensor.new_sensor(config[CONF_ILLUMINANCE])
cg.add(var.set_illuminance(sens))
if CONF_CONDUCTIVITY in config:
sens = yield sensor.new_sensor(config[CONF_CONDUCTIVITY])
cg.add(var.set_conductivity(sens))
if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))

View file

@ -1,15 +1,15 @@
#include "xiaomi_miflora.h"
#include "xiaomi_hhccjcy01.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_miflora {
namespace xiaomi_hhccjcy01 {
static const char *TAG = "xiaomi_miflora";
static const char *TAG = "xiaomi_hhccjcy01";
void XiaomiMiflora::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi Mijia");
void XiaomiHHCCJCY01::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi HHCCJCY01");
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Moisture", this->moisture_);
LOG_SENSOR(" ", "Conductivity", this->conductivity_);
@ -17,7 +17,7 @@ void XiaomiMiflora::dump_config() {
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
}
} // namespace xiaomi_miflora
} // namespace xiaomi_hhccjcy01
} // namespace esphome
#endif

View file

@ -8,9 +8,9 @@
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_miflora {
namespace xiaomi_hhccjcy01 {
class XiaomiMiflora : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
class XiaomiHHCCJCY01 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
void set_address(uint64_t address) { address_ = address; }
@ -52,7 +52,7 @@ class XiaomiMiflora : public Component, public esp32_ble_tracker::ESPBTDeviceLis
sensor::Sensor *battery_level_{nullptr};
};
} // namespace xiaomi_miflora
} // namespace xiaomi_hhccjcy01
} // namespace esphome
#endif

View file

@ -0,0 +1,38 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, esp32_ble_tracker
from esphome.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \
UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID
DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble']
xiaomi_lywsdcgq_ns = cg.esphome_ns.namespace('xiaomi_lywsdcgq')
XiaomiLYWSDCGQ = xiaomi_lywsdcgq_ns.class_('XiaomiLYWSDCGQ', esp32_ble_tracker.ESPBTDeviceListener,
cg.Component)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(XiaomiLYWSDCGQ),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 1),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield esp32_ble_tracker.register_ble_device(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
if CONF_TEMPERATURE in config:
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
if CONF_HUMIDITY in config:
sens = yield sensor.new_sensor(config[CONF_HUMIDITY])
cg.add(var.set_humidity(sens))
if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))

View file

@ -1,21 +1,21 @@
#include "xiaomi_cleargrass.h"
#include "xiaomi_lywsdcgq.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_cleargrass {
namespace xiaomi_lywsdcgq {
static const char *TAG = "xiaomi_cleargrass";
static const char *TAG = "xiaomi_lywsdcgq";
void XiaomiCleargrass::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi Cleargrass");
void XiaomiLYWSDCGQ::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi LYWSDCGQ");
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Humidity", this->humidity_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
}
} // namespace xiaomi_cleargrass
} // namespace xiaomi_lywsdcgq
} // namespace esphome
#endif

View file

@ -8,9 +8,9 @@
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_cleargrass {
namespace xiaomi_lywsdcgq {
class XiaomiCleargrass : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
class XiaomiLYWSDCGQ : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
void set_address(uint64_t address) { address_ = address; }
@ -44,7 +44,7 @@ class XiaomiCleargrass : public Component, public esp32_ble_tracker::ESPBTDevice
sensor::Sensor *battery_level_{nullptr};
};
} // namespace xiaomi_cleargrass
} // namespace xiaomi_lywsdcgq
} // namespace esphome
#endif

View file

@ -1,49 +1,3 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, esp32_ble_tracker
from esphome.const import CONF_BATTERY_LEVEL, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \
UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID, \
CONF_MOISTURE, CONF_ILLUMINANCE, ICON_BRIGHTNESS_5, UNIT_LUX, CONF_CONDUCTIVITY, \
UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER
DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble']
xiaomi_miflora_ns = cg.esphome_ns.namespace('xiaomi_miflora')
XiaomiMiflora = xiaomi_miflora_ns.class_('XiaomiMiflora', esp32_ble_tracker.ESPBTDeviceListener,
cg.Component)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(XiaomiMiflora),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1),
cv.Optional(CONF_MOISTURE): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 0),
cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(UNIT_LUX, ICON_BRIGHTNESS_5, 0),
cv.Optional(CONF_CONDUCTIVITY):
sensor.sensor_schema(UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER, 0),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield esp32_ble_tracker.register_ble_device(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
if CONF_TEMPERATURE in config:
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
if CONF_MOISTURE in config:
sens = yield sensor.new_sensor(config[CONF_MOISTURE])
cg.add(var.set_moisture(sens))
if CONF_ILLUMINANCE in config:
sens = yield sensor.new_sensor(config[CONF_ILLUMINANCE])
cg.add(var.set_illuminance(sens))
if CONF_CONDUCTIVITY in config:
sens = yield sensor.new_sensor(config[CONF_CONDUCTIVITY])
cg.add(var.set_conductivity(sens))
if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))
CONFIG_SCHEMA = cv.invalid("This sensor has been renamed to xiaomi_hhccjcy01")

View file

@ -1,38 +1,3 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, esp32_ble_tracker
from esphome.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, \
UNIT_CELSIUS, ICON_THERMOMETER, UNIT_PERCENT, ICON_WATER_PERCENT, ICON_BATTERY, CONF_ID
DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble']
xiaomi_mijia_ns = cg.esphome_ns.namespace('xiaomi_mijia')
XiaomiMijia = xiaomi_mijia_ns.class_('XiaomiMijia', esp32_ble_tracker.ESPBTDeviceListener,
cg.Component)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(XiaomiMijia),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(UNIT_PERCENT, ICON_WATER_PERCENT, 1),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield esp32_ble_tracker.register_ble_device(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
if CONF_TEMPERATURE in config:
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
if CONF_HUMIDITY in config:
sens = yield sensor.new_sensor(config[CONF_HUMIDITY])
cg.add(var.set_humidity(sens))
if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))
CONFIG_SCHEMA = cv.invalid("This sensor has been renamed to xiaomi_lywsdcgq")

View file

@ -61,26 +61,40 @@ sensor:
- platform: ble_rssi
mac_address: AC:37:43:77:5F:4C
name: "BLE Google Home Mini RSSI value"
- platform: xiaomi_miflora
- platform: xiaomi_hhccjcy01
mac_address: 94:2B:FF:5C:91:61
temperature:
name: "Xiaomi MiFlora Temperature"
name: "Xiaomi HHCCJCY01 Temperature"
moisture:
name: "Xiaomi MiFlora Moisture"
name: "Xiaomi HHCCJCY01 Moisture"
illuminance:
name: "Xiaomi MiFlora Illuminance"
name: "Xiaomi HHCCJCY01 Illuminance"
conductivity:
name: "Xiaomi MiFlora Soil Conductivity"
name: "Xiaomi HHCCJCY01 Soil Conductivity"
battery_level:
name: "Xiaomi MiFlora Battery Level"
- platform: xiaomi_mijia
name: "Xiaomi HHCCJCY01 Battery Level"
- platform: xiaomi_lywsdcgq
mac_address: 7A:80:8E:19:36:BA
temperature:
name: "Xiaomi MiJia Temperature"
name: "Xiaomi LYWSDCGQ Temperature"
humidity:
name: "Xiaomi MiJia Humidity"
name: "Xiaomi LYWSDCGQ Humidity"
battery_level:
name: "Xiaomi MiJia Battery Level"
name: "Xiaomi LYWSDCGQ Battery Level"
- platform: xiaomi_lywsd02
mac_address: 3F:5B:7D:82:58:4E
temperature:
name: "Xiaomi LYWSD02 Temperature"
humidity:
name: "Xiaomi LYWSD02 Humidity"
- platform: xiaomi_cgg1
mac_address: 7A:80:8E:19:36:BA
temperature:
name: "Xiaomi CGG1 Temperature"
humidity:
name: "Xiaomi CGG1 Humidity"
battery_level:
name: "Xiaomi CGG1 Battery Level"
- platform: pmsx003
type: PMSX003
pm_1_0: