Merge branch 'development' into prerelease-13.2

This commit is contained in:
Theo Arends 2023-10-18 09:53:10 +02:00
commit 2efefeb8a7
9 changed files with 16 additions and 647 deletions

View File

@ -24,6 +24,7 @@ All notable changes to this project will be documented in this file.
### Fixed
- ESP32 shutter frequency (#19717)
- ModbusBridge write memory leak (#19758)
- Zigbee timezone when device reads LocalTime attribute (#19772)
### Removed
- WiFiClientSecure in favour of WiFiClientSecureLightBearSSL (#19725)

View File

@ -116,7 +116,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm
- Commands to allow setting of timeprop parameters [#19310](https://github.com/arendst/Tasmota/issues/19310)
- Command ``Mi32Name`` [#19619](https://github.com/arendst/Tasmota/issues/19619)
- Variables ``%power<1..28>%`` and ``%switch<1..28>%`` to rules [#19331](https://github.com/arendst/Tasmota/issues/19331)
- Support different baudrates on BL0942
- Support different baudrates on BL0942 energy monitor
- Support for Shelly PlusPMMini, Plus1Mini and Plus1PMMini
- Support for HDMI CEC protocol [#19434](https://github.com/arendst/Tasmota/issues/19434)
- Support for ENS16x (air quality) and ENS210 (temp & RH) sensors [#19479](https://github.com/arendst/Tasmota/issues/19479)
@ -148,6 +148,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm
- Exception 3 in IRHVAC [#19389](https://github.com/arendst/Tasmota/issues/19389)
- PCF8574 mode 1 with base relays exception 3/28 regression from v12.4.0.4 [#19408](https://github.com/arendst/Tasmota/issues/19408)
- ModbusBridge write memory leak [#19758](https://github.com/arendst/Tasmota/issues/19758)
- Zigbee timezone when device reads LocalTime attribute [#19772](https://github.com/arendst/Tasmota/issues/19772)
- ESP32 DS18x20 driver support extended over GPIO33
- ESP32 Support for IPv6 link-local zones for esp-idf 5.1 (necessary for Matter)
- ESP32 Shutter migration [#19454](https://github.com/arendst/Tasmota/issues/19454)
@ -156,8 +157,8 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm
- ESP32 shutter frequency [#19717](https://github.com/arendst/Tasmota/issues/19717)
- ESP32 Arduino Core v2 wifi client flush [#19642](https://github.com/arendst/Tasmota/issues/19642)
- ESP32 Partition Wizard grow filesystem support [#19645](https://github.com/arendst/Tasmota/issues/19645)
- ESP32C3 relay click on restart
- ESP32-C3 relay click on restart
- Matter support for Virtual Devices controllable via Rules or Berry [#19520](https://github.com/arendst/Tasmota/issues/19520)
### Removed
- Removed support for Homekit in favour of Matter [#19738](https://github.com/arendst/Tasmota/issues/19738)
- Support for Homekit in favour of Matter [#19738](https://github.com/arendst/Tasmota/issues/19738)

View File

@ -198,7 +198,7 @@ struct {
uint32_t triggeredTele:1;
uint32_t shallShowStatusInfo:1; // react to amount of found sensors via RULES
uint32_t didGetConfig:1;
uint32_t didStartHAP:1;
uint32_t triggerBerryAdvCB:1;
uint32_t triggerBerryConnCB:1;
uint32_t triggerNextConnJob:1;
@ -230,12 +230,6 @@ struct {
#endif //USE_ENERGY_SENSOR
#endif //USE_MI_EXT_GUI
#if USE_MI_HOMEKIT==1
void *outlet_hap_service[4]; //arbitrary chosen
int8_t HKconnectedControllers = 0; //should never be < 0
uint8_t HKinfoMsg = 0;
char hk_setup_code[11];
#endif //USE_MI_HOMEKIT
void *beConnCB;
void *beAdvCB;
void *beServerCB;
@ -331,17 +325,6 @@ struct mi_sensor_t{
union {
uint8_t bat; // many values seem to be hard-coded garbage (LYWSD0x, GCD1)
};
#if USE_MI_HOMEKIT==1
//HAP handles
void *temp_hap_service;
void *hum_hap_service;
void *light_hap_service;
void *motion_hap_service;
void *door_sensor_hap_service;
void *button_hap_service[6];
void *bat_hap_service;
void *leak_hap_service;
#endif //USE_MI_HOMEKIT
};
/*********************************************************************************************\
@ -400,8 +383,6 @@ const char kMI32_ConnErrorMsg[] PROGMEM = "no Error|could not connect|did discon
const char kMI32_BLEInfoMsg[] PROGMEM = "Scan ended|Got Notification|Did connect|Did disconnect|Still connected|Start passive scanning|Start active scanning|Server characteristic set|Server advertisement set|Server scan response set|Server client did connect|Server client did disconnect";
const char kMI32_HKInfoMsg[] PROGMEM = "HAP core started|HAP core did not start!!|HAP controller disconnected|HAP controller connected|HAP outlet added";
const char kMI32_ButtonMsg[] PROGMEM = "Single|Double|Hold"; //mapping: in Tasmota: 1,2,3 ; for HomeKit and Xiaomi 0,1,2
/*********************************************************************************************\
* enumerations
@ -473,14 +454,6 @@ enum MI32_BLEInfoMsg {
MI32_SERV_CLIENT_DISCONNECTED
};
enum MI32_HKInfoMsg {
MI32_HAP_DID_START = 1,
MI32_HAP_DID_NOT_START,
MI32_HAP_CONTROLLER_DISCONNECTED,
MI32_HAP_CONTROLLER_CONNECTED,
MI32_HAP_OUTLET_ADDED
};
/*********************************************************************************************\
* extended web gui
\*********************************************************************************************/
@ -523,12 +496,6 @@ const char HTTP_MI32_PARENT_START[] PROGMEM =
"<div class='box'><h2>MI32 Bridge</h2>"
"Observing <span id='numDev'>%u</span> devices<br>"
"Uptime: <span class='Ti'>%u</span> seconds<br>"
#if USE_MI_HOMEKIT==1
"HomeKit setup code: %s<br>"
"HAP controller connections: %d<br>"
#else
"HomeKit not enabled%s<br>"
#endif //USE_MI_HOMEKIT
"Free Heap: %u kB"
"</div>";

View File

@ -573,6 +573,7 @@
// -- One wire sensors ----------------------------
#define USE_DS18x20 // Add support for DS18x20 sensors with id sort, single scan and read retry (+2k6 code)
// #define W1_PARASITE_POWER // Optimize for parasite powered sensors
// #define DS18x20_USE_ID_AS_NAME // Use last 3 bytes for naming of sensors
// #define DS18x20_USE_ID_ALIAS // Add support aliasing for DS18x20 sensors. See comments in xsns_05 files (+0k5 code)
// -- I2C sensors ---------------------------------

View File

@ -2274,10 +2274,10 @@ void ZCLFrame::autoResponder(const uint16_t *attr_list_ids, size_t attr_len) {
attr.setUInt((Rtc.utc_time > START_VALID_TIME) ? 0x02 : 0x00);
break;
case 0x000A0002: // TimeZone
attr.setUInt(Settings->toffset[0] * 60);
attr.setUInt(Rtc.time_timezone * 60);
break;
case 0x000A0007: // LocalTime // TODO take DST
attr.setUInt(Settings->toffset[0] * 60 + ((Rtc.utc_time > START_VALID_TIME) ? Rtc.utc_time - 946684800 : Rtc.utc_time));
attr.setUInt(Rtc.time_timezone * 60 + ((Rtc.utc_time > START_VALID_TIME) ? Rtc.utc_time - 946684800 : Rtc.utc_time));
break;
}
if (!attr.isNone()) {

View File

@ -483,6 +483,7 @@ void Ds18x20Name(uint8_t sensor) {
}
snprintf_P(DS18X20Data.name, sizeof(DS18X20Data.name), PSTR("%s%c%s"), DS18X20Data.name, IndexSeparator(), address);
#elif defined(DS18x20_USE_ID_ALIAS)
sensor = ds18x20_sensor[sensor].index;
if (ds18x20_sensor[sensor].alias[0] != '0') {
if (isdigit(ds18x20_sensor[sensor].alias[0])) {
snprintf_P(DS18X20Data.name, sizeof(DS18X20Data.name), PSTR("DS18Sens%c%d"), IndexSeparator(), atoi(ds18x20_sensor[sensor].alias));

View File

@ -227,6 +227,7 @@ void Ds18x20Name(uint8_t sensor) {
}
snprintf_P(DS18X20Data.name, sizeof(DS18X20Data.name), PSTR("%s%c%s"), DS18X20Data.name, IndexSeparator(), address);
#elif defined(DS18x20_USE_ID_ALIAS)
sensor = ds18x20_sensor[sensor].index;
if (ds18x20_sensor[sensor].alias[0] != '0') {
if (isdigit(ds18x20_sensor[sensor].alias[0])) {
snprintf_P(DS18X20Data.name, sizeof(DS18X20Data.name), PSTR("DS18Sens%c%d"), IndexSeparator(), atoi(ds18x20_sensor[sensor].alias));

View File

@ -68,17 +68,6 @@
#include "include/xsns_62_esp32_mi.h"
#if USE_MI_HOMEKIT==0
#undef USE_MI_HOMEKIT
#endif
#if USE_MI_HOMEKIT==1
extern "C" void mi_homekit_main(void);
extern "C" void mi_homekit_update_value(void* handle, float value, uint32_t type);
extern "C" void mi_homekit_stop();
void MI32getSetupCodeFromMAC(char* code);
#endif //USE_MI_HOMEKIT
void MI32scanEndedCB(NimBLEScanResults results);
void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
void MI32AddKey(mi_bindKey_t keyMAC);
@ -497,18 +486,12 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.fert=1;
_newSensor.feature.lux=1;
_newSensor.feature.bat=1;
#if USE_MI_HOMEKIT==1
_newSensor.light_hap_service = nullptr;
#endif
break;
case NLIGHT:
_newSensor.events=0x00;
_newSensor.feature.motion=1;
_newSensor.feature.NMT=1;
_newSensor.NMT=0;
#if USE_MI_HOMEKIT==1
_newSensor.motion_hap_service = nullptr;
#endif //USE_MI_HOMEKIT
break;
case MJYD2S:
_newSensor.NMT=0;
@ -517,10 +500,6 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.NMT=1;
_newSensor.feature.lux=1;
_newSensor.feature.bat=1;
#if USE_MI_HOMEKIT==1
_newSensor.light_hap_service = nullptr;
_newSensor.motion_hap_service = nullptr;
#endif //USE_MI_HOMEKIT
_newSensor.feature.bat=1;
_newSensor.NMT=0;
break;
@ -531,17 +510,11 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.knob = 1;
_newSensor.dimmer = 0;
}
#if USE_MI_HOMEKIT==1
_newSensor.button_hap_service[0] = nullptr;
#endif //USE_MI_HOMEKIT
break;
case MCCGQ02:
_newSensor.events=0x00;
_newSensor.feature.bat=1;
_newSensor.feature.door=1;
#if USE_MI_HOMEKIT==1
_newSensor.door_sensor_hap_service = nullptr;
#endif //USE_MI_HOMEKIT
_newSensor.door = 255;
break;
case SJWS01L:
@ -549,11 +522,6 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.bat=1;
_newSensor.feature.Btn=1;
_newSensor.Btn=99;
#if USE_MI_HOMEKIT==1
_newSensor.leak_hap_service = nullptr;
_newSensor.bat_hap_service = nullptr;
_newSensor.button_hap_service[0] = nullptr;
#endif //USE_MI_HOMEKIT
break;
default:
_newSensor.hum=NAN;
@ -562,11 +530,6 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.hum=1;
_newSensor.feature.tempHum=1;
_newSensor.feature.bat=1;
#if USE_MI_HOMEKIT==1
_newSensor.temp_hap_service = nullptr;
_newSensor.hum_hap_service = nullptr;
_newSensor.bat_hap_service = nullptr;
#endif //USE_MI_HOMEKIT
break;
}
MIBLEsensors.push_back(_newSensor);
@ -709,17 +672,6 @@ void MI32Init(void) {
}
}
if(MI32.mode.didGetConfig && !Settings->flag5.zigbee_hide_bridge_topic){ // borrow SO125 1 to turn off HomeKit
MI32.mode.didStartHAP = 0;
#if USE_MI_HOMEKIT==1
MI32getSetupCodeFromMAC(MI32.hk_setup_code);
AddLog(LOG_LEVEL_INFO,PSTR("M32: Init HAP core"));
mi_homekit_main();
#else
MI32.mode.didStartHAP = 1;
#endif //USE_MI_HOMEKIT
}
if (!MI32.mode.init) {
// NimBLEDevice::setScanFilterMode(1); //CONFIG_BTDM_SCAN_DUPL_TYPE_DATA
// NimBLEDevice::setScanDuplicateCacheSize(40); // will not be perfect for every situation (few vs many BLE devices nearby)
@ -924,128 +876,7 @@ extern "C" {
}
} //extern "C"
/*********************************************************************************************\
* Homekit section
\*********************************************************************************************/
#if USE_MI_HOMEKIT==1
extern "C" {
const char * MI32getSetupCode(){
return (const char*)MI32.hk_setup_code;
}
uint32_t MI32numOfRelays(){
if(TasmotaGlobal.devices_present>0) MI32.HKinfoMsg = MI32_HAP_OUTLET_ADDED;
return TasmotaGlobal.devices_present;
}
void MI32setRelayFromHK(uint32_t relay, bool onOff){
ExecuteCommandPower(relay, onOff, SRC_IGNORE);
}
uint32_t MI32getDeviceType(uint32_t slot){
return MIBLEsensors[slot].type;
}
/**
* @brief Get at least a bit of the status of the HAP core, i.e. to reduce the activy of the driver while doing the pairing
*
* @param event
*/
void MI32passHapEvent(uint32_t event){
switch(event){
case 5: //HAP_EVENT_PAIRING_STARTED
MI32suspendScanTask();
default:
MI32resumeScanTask();
}
if(event==4){
MI32.HKinfoMsg = MI32_HAP_CONTROLLER_DISCONNECTED;
MI32.HKconnectedControllers--;
}
if(event==3){
MI32.HKinfoMsg = MI32_HAP_CONTROLLER_CONNECTED;
MI32.HKconnectedControllers++;
}
}
void MI32didStartHAP(bool HAPdidStart){
if(HAPdidStart) {
MI32.mode.didStartHAP = 1;
MI32.HKinfoMsg = MI32_HAP_DID_START;
}
else{
MI32.HKinfoMsg = MI32_HAP_DID_NOT_START;
}
}
/**
* @brief Simply store the writeable HAP characteristics as void pointers in the "main" driver for updates of the values
*
* @param slot - sensor slot in MIBLEsensors
* @param type - sensors type, except for the buttons this is equal to the mibeacon types
* @param handle - a void ponter to a characteristic
*/
void MI32saveHAPhandles(uint32_t slot, uint32_t type, void* handle){
// AddLog(LOG_LEVEL_INFO,PSTR("M32: pass ptr to hap service, type:%u"), type);
switch(type){
case 1000: case 1001: case 1002: case 1003: case 1004: case 1005:
MIBLEsensors[slot].button_hap_service[type-1000] = handle;
break;
case 0x04:
MIBLEsensors[slot].temp_hap_service = handle;
break;
case 0x06:
MIBLEsensors[slot].hum_hap_service = handle;
break;
case 0x0a:
MIBLEsensors[slot].bat_hap_service = handle;
break;
case 0x07:
MIBLEsensors[slot].light_hap_service = handle;
break;
case 0x0f:
MIBLEsensors[slot].motion_hap_service = handle;
break;
case 0x14:
MIBLEsensors[slot].leak_hap_service = handle;
break;
case 0x19:
MIBLEsensors[slot].door_sensor_hap_service = handle;
break;
case 0xf0:
if(slot>3) break; //support only 4 for now
MI32.outlet_hap_service[slot] = handle;
break;
}
}
}
/**
* @brief Creates a simplified setup code from the Wifi MAC for HomeKit by converting every ascii-converted byte to 1, if it not 2-9
* Example: AABBCC1234f2
* -> 111-11-234
* This is no security feature, only for convenience
* * @param setupcode
*/
void MI32getSetupCodeFromMAC(char *setupcode){
uint8_t _mac[6];
char _macStr[13] = { 0 };
WiFi.macAddress(_mac);
ToHex_P(_mac,6,_macStr,13);
AddLog(LOG_LEVEL_INFO,PSTR("M32: Wifi MAC: %s"), _macStr);
for(int i = 0; i<10; i++){
if(_macStr[i]>'9' || _macStr[i]<'1') setupcode[i]='1';
else setupcode[i] = _macStr[i];
}
setupcode[3] = '-';
setupcode[6] = '-';
setupcode[10] = 0;
AddLog(LOG_LEVEL_INFO,PSTR("M32: HK setup code: %s"), setupcode);
return;
}
#endif //USE_MI_HOMEKIT
/*********************************************************************************************\
* Config section
\*********************************************************************************************/
@ -1651,11 +1482,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].longpress = _payload.Btn.value;
MI32.mode.shallTriggerTele = 1;
MIBLEsensors[_slot].eventType.longpress = 1;
#if USE_MI_HOMEKIT==1
if((void**)MIBLEsensors[_slot].button_hap_service[0] != nullptr){
mi_homekit_update_value(MIBLEsensors[_slot].button_hap_service[0], (float)2.0f, 0x01); // only one button, long press = 2
}
#endif //USE_MI_HOMEKIT
break;
}
// single, double, long
@ -1668,13 +1494,6 @@ if(decryptRet!=0){
}
MIBLEsensors[_slot].eventType.Btn = 1;
MI32.mode.shallTriggerTele = 1;
#if USE_MI_HOMEKIT==1
if(MIBLEsensors[_slot].Btn>5) break; // hard coded limit for now
if((void**)MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn] != nullptr){
// AddLog(LOG_LEVEL_DEBUG,PSTR("Send Button %u: SingleLong:%u, pointer: %x"), MIBLEsensors[_slot].Btn,_singleLong,MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn] );
mi_homekit_update_value(MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn], (float)MIBLEsensors[_slot].BtnType, 0x01);
}
#endif //USE_MI_HOMEKIT
// AddLog(LOG_LEVEL_DEBUG,PSTR("Mode 1: U16: %u Button"), MIBLEsensors[_slot].Btn );
break;
case 0x04:
@ -1685,9 +1504,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.temp = 1;
DEBUG_SENSOR_LOG(PSTR("Mode 4: temp updated"));
}
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, _tempFloat, 0x04);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
MI32addHistory(MIBLEsensors[_slot].temp_history, _tempFloat, 0);
#endif //USE_MI_EXT_GUI
@ -1701,9 +1517,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.hum = 1;
DEBUG_SENSOR_LOG(PSTR("Mode 6: hum updated"));
}
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, _tempFloat,0x06);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
MI32addHistory(MIBLEsensors[_slot].hum_history, _tempFloat, 1);
#endif //USE_MI_EXT_GUI
@ -1716,9 +1529,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.noMotion = 1;
}
MIBLEsensors[_slot].eventType.lux = 1;
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].light_hap_service, (float)MIBLEsensors[_slot].lux,0x07);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
MI32addHistory(MIBLEsensors[_slot].lux_history, (float)MIBLEsensors[_slot].lux, 2);
#endif //USE_MI_EXT_GUI
@ -1749,9 +1559,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].bat = _payload.bat;
MIBLEsensors[_slot].eventType.bat = 1;
DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated"));
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].bat_hap_service, (float)_payload.bat,0xa);
#endif //USE_MI_HOMEKIT
}
// AddLog(LOG_LEVEL_DEBUG,PSTR("Mode a: U8: %u %%"), _payload.bat);
break;
@ -1780,10 +1587,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.lux = 1;
MIBLEsensors[_slot].NMT = 0;
MI32.mode.shallTriggerTele = 1;
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].motion_hap_service, (float)1,0x0f);
mi_homekit_update_value(MIBLEsensors[_slot].light_hap_service, (float)_payload.lux,0x07);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
MI32addHistory(MIBLEsensors[_slot].lux_history, (float)MIBLEsensors[_slot].lux, 2);
#endif //USE_MI_EXT_GUI
@ -1794,9 +1597,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].leak = _payload.leak;
MIBLEsensors[_slot].eventType.leak = 1;
if(_payload.leak>0) MI32.mode.shallTriggerTele = 1;
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].leak_hap_service, (float)_payload.leak,0x14);
#endif //USE_MI_HOMEKIT
break;
case 0x17:
MIBLEsensors[_slot].feature.NMT = 1;
@ -1811,9 +1611,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.door = 1;
MIBLEsensors[_slot].events++;
MI32.mode.shallTriggerTele = 1;
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].door_sensor_hap_service, (float)_payload.door,0x19);
#endif //USE_MI_HOMEKIT
// AddLog(LOG_LEVEL_DEBUG,PSTR("Mode 19: %u"), _payload.door);
break;
@ -1823,9 +1620,6 @@ if(decryptRet!=0){
MIBLEsensors[_slot].events++;
MIBLEsensors[_slot].NMT = 0;
MI32.mode.shallTriggerTele = 1;
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].motion_hap_service, (float)1,0x0f);
#endif //USE_MI_HOMEKIT
}
else{
//unknown payload
@ -1865,11 +1659,6 @@ void MI32ParseATCPacket(char * _buf, uint32_t length, uint8_t addr[6], int RSSI)
MIBLEsensors[_slot].eventType.tempHum = 1;
MIBLEsensors[_slot].eventType.bat = 1;
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, MIBLEsensors.at(_slot).temp,0x04);
mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, MIBLEsensors.at(_slot).hum,0x06);
mi_homekit_update_value(MIBLEsensors[_slot].bat_hap_service, (float)MIBLEsensors.at(_slot).bat,0x0a);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
bitSet(MI32.widgetSlot,_slot);
MI32addHistory(MIBLEsensors[_slot].temp_history, (float)MIBLEsensors[_slot].temp, 0);
@ -1898,9 +1687,6 @@ void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int RSSI
MIBLEsensors[_slot].temp = _tempFloat;
MIBLEsensors[_slot].eventType.temp = 1;
DEBUG_SENSOR_LOG(PSTR("CGD1: temp updated"));
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, _tempFloat,0x04);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
MI32addHistory(MIBLEsensors[_slot].temp_history, (float)MIBLEsensors[_slot].temp, 0);
#endif //USE_MI_EXT_GUI
@ -1910,9 +1696,6 @@ void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int RSSI
MIBLEsensors[_slot].hum = _tempFloat;
MIBLEsensors[_slot].eventType.hum = 1;
DEBUG_SENSOR_LOG(PSTR("CGD1: hum updated"));
#if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, _tempFloat,0x06);
#endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI
MI32addHistory(MIBLEsensors[_slot].hum_history, (float)MIBLEsensors[_slot].hum, 1);
#endif //USE_MI_EXT_GUI
@ -2029,14 +1812,7 @@ void MI32Every50mSecond(){
AddLog(LOG_LEVEL_DEBUG,PSTR("M32: %s"),_message);
MI32.infoMsg = 0;
}
#if USE_MI_HOMEKIT==1
if(MI32.HKinfoMsg > 0){
char _message[32];
GetTextIndexed(_message, sizeof(_message), MI32.HKinfoMsg-1, kMI32_HKInfoMsg);
AddLog(LOG_LEVEL_DEBUG,PSTR("M32: %s"),_message);
MI32.HKinfoMsg = 0;
}
#endif //USE_MI_HOMEKIT
}
/**
@ -2046,25 +1822,9 @@ void MI32Every50mSecond(){
void MI32EverySecond(bool restart){
#if USE_MI_HOMEKIT==1
if(TasmotaGlobal.devices_present>0){
for(uint32_t i=0;i<TasmotaGlobal.devices_present;i++){
power_t mask = 1 << i;
// AddLog(LOG_LEVEL_DEBUG,PSTR("M32: Power masl: %u"), (TasmotaGlobal.power & mask));
mi_homekit_update_value(MI32.outlet_hap_service[i],(TasmotaGlobal.power & mask)!=0,0xf0);
}
}
#endif //USE_MI_HOMEKIT
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
if(MIBLEsensors[i].type==NLIGHT || MIBLEsensors[i].type==MJYD2S){
MIBLEsensors[i].NMT++;
#if USE_MI_HOMEKIT==1
if(MIBLEsensors[i].NMT > 20){ //TODO: Make a choosable timeout later
mi_homekit_update_value(MIBLEsensors[i].motion_hap_service,0.0f,0x0f);
}
#endif //USE_MI_HOMEKIT
}
}
}
@ -2355,12 +2115,9 @@ void MI32InitGUI(void){
WSContentSend_P(HTTP_MI32_STYLE_SVG,1,185,124,124,185,124,124);
WSContentSend_P(HTTP_MI32_STYLE_SVG,2,151,190,216,151,190,216);
WSContentSend_P(HTTP_MI32_STYLE_SVG,3,242,240,176,242,240,176);
#if USE_MI_HOMEKIT==1
WSContentSend_P((HTTP_MI32_PARENT_START),MIBLEsensors.size(),UpTime(),MI32.hk_setup_code,MI32.HKconnectedControllers,ESP.getFreeHeap()/1024);
#else
const char _setupCode[1] = {0};
WSContentSend_P((HTTP_MI32_PARENT_START),MIBLEsensors.size(),UpTime(),_setupCode,ESP.getFreeHeap()/1024);
#endif //USE_MI_HOMEKIT
WSContentSend_P((HTTP_MI32_PARENT_START),MIBLEsensors.size(),UpTime(),ESP.getFreeHeap()/1024);
for(uint32_t _slot = 0;_slot<MIBLEsensors.size();_slot++){
MI32sendWidget(_slot);
}
@ -2574,11 +2331,7 @@ void MI32Show(bool json)
MI32suspendScanTask();
WSContentSend_P(HTTP_MI32, MIBLEsensors.size());
#if USE_MI_HOMEKIT==1
if(MI32.mode.didStartHAP){
WSContentSend_PD(PSTR("{s}HomeKit Code{m} %s{e}"),MI32.hk_setup_code);
}
#endif //USE_MI_HOMEKIT
#ifndef USE_MI_EXT_GUI
for (uint32_t i = 0; i<MIBLEsensors.size(); i++) {
WSContentSend_PD(HTTP_MI32_HL);
@ -2642,12 +2395,6 @@ int ExtStopBLE(){
AddLog(LOG_LEVEL_INFO,PSTR("M32: stop BLE"));
while (MI32.mode.runningScan) yield();
}
#if USE_MI_HOMEKIT==1
if(MI32.mode.didStartHAP) {
AddLog(LOG_LEVEL_INFO,PSTR("M32: stop Homebridge"));
mi_homekit_stop();
}
#endif //USE_MI_HOMEKIT
return 0;
}

View File

@ -1,350 +0,0 @@
#if(USE_MI_HOMEKIT==1)
#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <hap.h>
#include <hap_apple_servs.h>
#include <hap_apple_chars.h>
#include <hap_platform_keystore.h>
//Homekit
static int MI32_bridge_identify(hap_acc_t *ha);
static int MI32_accessory_identify(hap_acc_t *ha);
static void MI32_bridge_thread_entry(void *p);
extern uint32_t MI32numberOfDevices();
extern char *MI32getDeviceName(uint32_t slot);
extern uint32_t MI32getDeviceType(uint32_t slot);
extern void MI32saveHAPhandles(uint32_t slot, uint32_t type, void* handle);
extern void MI32passHapEvent(uint32_t event);
extern void MI32didStartHAP();
extern const char * MI32getSetupCode();
extern uint32_t MI32numOfRelays();
extern void MI32setRelayFromHK(uint32_t relay, bool onOff);
// static const char *TAG = "Mi Bridge";
static bool MIBridgeWasNeverConnected = true;
#define CONFIG_EXAMPLE_SETUP_ID "MI32"
#define UNKNOWN_MI 0
#define FLORA 1
#define MJ_HT_V1 2
#define LYWSD02 3
#define LYWSD03MMC 4
#define CGG1 5
#define CGD1 6
#define NLIGHT 7
#define MJYD2S 8
#define YLYK01 9
#define MHOC401 10
#define MHOC303 11
#define ATC 12
#define MCCGQ02 13
#define SJWS01L 14
#define PVVX 15
#define YLKG08 16
#define YLAI003 17
/*********************************************************************************************\
* Homekit
\*********************************************************************************************/
/* Mandatory identify routine for the bridge.
* In a real accessory, something like LED blink should be implemented
* got visual identification
*/
static int MI32_bridge_identify(hap_acc_t *ha)
{
return HAP_SUCCESS;
}
void mi_hap_event_handler(hap_event_t event, void *data)
{
MI32passHapEvent((uint32_t)event);
if(event == HAP_EVENT_CTRL_CONNECTED) MIBridgeWasNeverConnected = false;
}
static int MI32_bridge_read_callback(hap_read_data_t read_data[], int count,
void *serv_priv, void *read_priv)
{
return HAP_SUCCESS;
}
static int MI32_outlets_write_callback(hap_write_data_t write_data[], int count,
void *serv_priv, void *write_priv)
{
uint8_t _relay = ((uint8_t*)serv_priv)[0];
int i, ret = HAP_SUCCESS;
hap_write_data_t *write;
for (i = 0; i < count; i++) {
write = &write_data[i];
if (!strcmp(hap_char_get_type_uuid(write->hc), HAP_CHAR_UUID_ON)) {
MI32setRelayFromHK(_relay-48, write->val.b);
hap_char_update_val(write->hc, &(write->val));
*(write->status) = HAP_STATUS_SUCCESS;
} else {
*(write->status) = HAP_STATUS_RES_ABSENT;
}
}
return ret;
}
/* Mandatory identify routine for the bridged accessory
* In a real bridge, the actual accessory must be sent some request to
* identify itself visually
*/
static int MI32_accessory_identify(hap_acc_t *ha)
{
return HAP_SUCCESS;
}
/*The main thread for handling the Smart Outlet Accessory */
static void MI32_bridge_thread_entry(void *p)
{
// esp_log_level_set("*", ESP_LOG_NONE);
hap_acc_t *accessory;
hap_serv_t *service;
/* Initialize the HAP core */
hap_init(HAP_TRANSPORT_WIFI);
/* Initialise the mandatory parameters for Accessory which will be added as
* the mandatory services internally
*/
hap_acc_cfg_t cfg = {
.name = "Mi-Home-Bridge",
.manufacturer = "Tasmota",
.model = "ESP32",
.serial_num = "9600",
.fw_rev = "0.9.5",
.hw_rev = NULL,
.pv = "1.1.0",
.cid = HAP_CID_BRIDGE,
.identify_routine = MI32_bridge_identify
};
/* Create accessory object */
accessory = hap_acc_create(&cfg);
/* Add a dummy Product Data */
uint8_t product_data[] = {'T','M','H'};
hap_acc_add_product_data(accessory, product_data, sizeof(product_data));
/* Add the Accessory to the HomeKit Database */
hap_add_accessory(accessory);
#define NUM_BRIDGED_ACCESSORIES 1
/* Create and add the Accessory to the Bridge object*/
uint32_t _numDevices = MI32numberOfDevices();
for (uint32_t i = 0; i < _numDevices; i++) {
if(MI32getDeviceType(i) == UNKNOWN_MI) continue;
char *accessory_name = MI32getDeviceName(i);
char _serialNum[4] = {0};
snprintf(_serialNum,sizeof(_serialNum),"%u", i);
hap_acc_cfg_t bridge_cfg = {
.name = accessory_name,
.manufacturer = "Xiaomi",
.model = accessory_name,
.serial_num = _serialNum,
.fw_rev = "0.9.1",
.hw_rev = NULL,
.pv = "1.1.0",
.cid = HAP_CID_SENSOR,
.identify_routine = MI32_accessory_identify,
};
/* Create accessory object */
accessory = hap_acc_create(&bridge_cfg);
switch (MI32getDeviceType(i)){
case LYWSD02: case LYWSD03MMC: case CGG1: case CGD1: case MHOC303: case MHOC401: case ATC: case PVVX:
{
service = hap_serv_humidity_sensor_create(50.0f);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x06,(void *)hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_CURRENT_RELATIVE_HUMIDITY));
service = hap_serv_temperature_sensor_create(22.5f);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x04,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_CURRENT_TEMPERATURE));
service = hap_serv_battery_service_create(99,0,0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0a,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_BATTERY_LEVEL));
}
break;
case FLORA: case MJYD2S:
{
service = hap_serv_light_sensor_create(100.0f);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x07,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_CURRENT_AMBIENT_LIGHT_LEVEL));
service = hap_serv_battery_service_create(50,0,0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0a,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_BATTERY_LEVEL));
if(MI32getDeviceType(i) == MJYD2S){
service = hap_serv_motion_sensor_create(false);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0f,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_MOTION_DETECTED));
}
break;
}
case NLIGHT:
{
service = hap_serv_motion_sensor_create(false);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0f,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_MOTION_DETECTED));
service = hap_serv_battery_service_create(50,0,0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0a,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_BATTERY_LEVEL));
break;
//motion 0x0f
}
case MCCGQ02:
{
service = hap_serv_contact_sensor_create(0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x19,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_CONTACT_SENSOR_STATE));
service = hap_serv_battery_service_create(50,0,0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0a,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_BATTERY_LEVEL));
break;
}
case YLYK01:
{
bridge_cfg.cid = HAP_CID_PROGRAMMABLE_SWITCH;
hap_serv_t * _label = hap_serv_service_label_create(1);
hap_acc_add_serv(accessory, _label);
for(uint8_t _but=0;_but<6;_but++){
hap_serv_t * _newSwitch = hap_serv_stateless_programmable_switch_create(0);
const uint8_t _validVals[] = {0,2};
hap_char_add_valid_vals(hap_serv_get_char_by_uuid(_newSwitch, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT), _validVals, 2);
hap_char_t *_index = hap_char_service_label_index_create(_but+1);
hap_serv_add_char(_newSwitch,_index);
hap_acc_add_serv(accessory, _newSwitch);
MI32saveHAPhandles(i,_but+1000,hap_serv_get_char_by_uuid(_newSwitch, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT));
}
}
break;
case YLKG08: //without the dimmer function due to lack of HomeKit support
{
bridge_cfg.cid = HAP_CID_PROGRAMMABLE_SWITCH;
hap_serv_t * _label = hap_serv_service_label_create(1);
hap_acc_add_serv(accessory, _label);
hap_serv_t * _newSwitch = hap_serv_stateless_programmable_switch_create(0);
const uint8_t _validVals[] = {0,1,2};
hap_char_add_valid_vals(hap_serv_get_char_by_uuid(_newSwitch, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT), _validVals, 3);
hap_char_t *_index = hap_char_service_label_index_create(1);
hap_serv_add_char(_newSwitch,_index);
hap_acc_add_serv(accessory, _newSwitch);
MI32saveHAPhandles(i,1000,hap_serv_get_char_by_uuid(_newSwitch, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT));
}
break;
case SJWS01L:
service = hap_serv_leak_sensor_create(0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x14,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_LEAK_DETECTED));
hap_serv_t * _newSwitch = hap_serv_stateless_programmable_switch_create(0);
const uint8_t _validVals[] = {0,2};
hap_char_add_valid_vals(hap_serv_get_char_by_uuid(_newSwitch, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT), _validVals, 2);
hap_acc_add_serv(accessory, _newSwitch);
MI32saveHAPhandles(i,1000,hap_serv_get_char_by_uuid(_newSwitch, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT));
service = hap_serv_battery_service_create(50,0,0);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0x0a,hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_BATTERY_LEVEL));
break;
default:
break;
}
/* Add the Accessory to the HomeKit Database */
hap_add_bridged_accessory(accessory, hap_get_unique_aid(accessory_name));
}
// add internal Tasmota devices
for(uint32_t i = 0;i<MI32numOfRelays();i++){
char _serialNum[2] = {i+49,0};
hap_acc_cfg_t bridge_cfg = {
.name = _serialNum,
.manufacturer = "Xiaomi",
.model = "Tasmota",
.serial_num = _serialNum,
.fw_rev = "0.9.1",
.hw_rev = NULL,
.pv = "1.1.0",
.cid = HAP_CID_OUTLET,
.identify_routine = MI32_accessory_identify,
};
/* Create accessory object */
accessory = hap_acc_create(&bridge_cfg);
service = hap_serv_outlet_create(false,true);
hap_serv_set_bulk_read_cb(service, MI32_bridge_read_callback);
hap_serv_set_write_cb(service, MI32_outlets_write_callback);
hap_serv_set_priv(service, strdup(_serialNum));
hap_acc_add_serv(accessory, service);
MI32saveHAPhandles(i,0xf0,(void *)hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_ON));
hap_add_bridged_accessory(accessory, hap_get_unique_aid(_serialNum));
}
hap_register_event_handler(mi_hap_event_handler);
hap_set_setup_code(MI32getSetupCode());
hap_set_setup_id(CONFIG_EXAMPLE_SETUP_ID);
if (hap_start() == HAP_SUCCESS){
MI32didStartHAP(true);
}
else MI32didStartHAP(false);
/* The task ends here. The read/write callbacks will be invoked by the HAP Framework */
vTaskDelete(NULL);
}
void mi_homekit_main(void){
xTaskCreate(MI32_bridge_thread_entry, "MIBRIDGE", 8192, NULL, 5, NULL);
}
void mi_homekit_update_value(void* handle, float value, uint32_t type){
if(handle == NULL) return;
if(MIBridgeWasNeverConnected) return;
hap_val_t new_val;
switch(type){
case 0x01: case 0x19: case 0x0a: case 0x14: case 0xf0:
if(type == 0x19){
value = 1-(uint8_t)value;
}
new_val.i = (uint8_t)value;
break;
case 0x0f:
new_val.b = (value > 0.0f);
break;
default:
new_val.f = value;
}
int ret = hap_char_update_val((hap_char_t *)handle, &new_val);
// if(ret!= HAP_SUCCESS){
// ESP_LOGE(TAG,"error:",ret);
// }
}
void mi_homekit_stop(){
hap_stop();
}
#endif //USE_MI_ESP32