Skip to content

Commit 3a13006

Browse files
committed
Add first version of HomeKit Support
1 parent da09e96 commit 3a13006

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed

data/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
"mqttbroker": "mqtt://192.168.1.1",
55
"mqttusername": "myusername",
66
"mqttpasswd": "mypassword",
7+
"homekitpin": "111-11-111",
78
"timezone": "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00",
89
"scenes": [
10+
{
11+
"name": "Entrance",
12+
"type": "HomeKitSwitch",
13+
"icon": "door"
14+
},
915
{
1016
"name": "Entrance",
1117
"type": "Light",

main/AppContext.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ namespace ctx
1212
void AppContext::setup()
1313
{
1414
fs::FileSystem::getInstance().loadPartitions();
15-
mMQTTGroups = fs::ConfigReader::getMQTTGroups();
16-
mpMQTTConnection = std::make_shared<mqtt::MQTTConnection>(fs::ConfigReader::getMQTTConfig(), mMQTTGroups);
15+
mDeviceGroups = fs::ConfigReader::getDeviceGroups();
16+
const auto hkConfig = fs::ConfigReader::getHKConfig();
17+
if (hkConfig.isEnabled)
18+
{
19+
mpHKConnection = std::make_shared<homekit::HKConnection>(hkConfig, mDeviceGroups);
20+
}
21+
mpMQTTConnection = std::make_shared<mqtt::MQTTConnection>(fs::ConfigReader::getMQTTConfig(), mDeviceGroups);
1722
const auto timeZone = fs::ConfigReader::getTimeZone();
1823
if (timeZone != "")
1924
{
@@ -34,12 +39,16 @@ namespace ctx
3439
if (cb == mqtt::MQTTConnectionStatus::CONNECTED)
3540
{
3641
mpMQTTConnection->bindScenes();
42+
if (mpHKConnection)
43+
{
44+
mpHKConnection->start();
45+
}
3746
}
3847
});
3948
}
4049

41-
std::vector<MQTTVariants> &AppContext::getMQTTGroups()
50+
std::vector<DeviceVariants> &AppContext::getDeviceGroups()
4251
{
43-
return mMQTTGroups;
52+
return mDeviceGroups;
4453
}
4554
} // namespace ctx

main/AppContext.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <homekit/HKConnection.h>
34
#include <mqtt/MQTTConnection.h>
45
#include <mqtt/MQTTGroup.hpp>
56
#include <ntp/NTPSync.h>
@@ -21,13 +22,14 @@ namespace ctx
2122

2223
WifiContext& getWifiContext() { return mWifiContext; };
2324
std::shared_ptr<mqtt::MQTTConnection> getMQTTConnection() { return mpMQTTConnection; };
24-
std::vector<MQTTVariants> &getMQTTGroups();
25+
std::vector<DeviceVariants> &getDeviceGroups();
2526

2627
private:
2728
std::shared_ptr<mqtt::MQTTConnection> mpMQTTConnection;
29+
std::shared_ptr<homekit::HKConnection> mpHKConnection = nullptr;
2830
std::shared_ptr<ntp::NTPSync> mNTPSync;
2931
WifiContext mWifiContext;
30-
std::vector<MQTTVariants> mMQTTGroups;
32+
std::vector<DeviceVariants> mDeviceGroups;
3133
rapidjson::Document mConfigDocument;
3234
};
3335
} // namespace ctx

main/AppScreen.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace gfx
4242
template<class ScreenDriver, class NavigationDriver>
4343
void AppScreen<ScreenDriver, NavigationDriver>::presentScreen(const uint16_t sceneId)
4444
{
45-
auto& scenes = mpAppContext->getMQTTGroups();
45+
auto& scenes = mpAppContext->getDeviceGroups();
4646
auto widgets = std::vector<WidgetPtr>();
4747

4848
auto mqttScene = std::find_if(scenes.begin(), scenes.end(), [&](auto& scene)
@@ -111,7 +111,7 @@ namespace gfx
111111
{
112112
std::lock_guard<std::mutex> guard(viewMutex);
113113
auto screenNavigator = std::make_shared<ScreenNavigator<NavigationDriver>>(&mTft, menuFrame, 1000);
114-
auto& scenes = mpAppContext->getMQTTGroups();
114+
auto& scenes = mpAppContext->getDeviceGroups();
115115
auto widgets = std::vector<WidgetPtr>();
116116
for (auto& scene : scenes)
117117
{

main/fs/ConfigReader.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <util/warnings.h>
44
#include "rapidjson/document.h"
55
#include "Filesystem.h"
6+
#include <homekit/HKConfig.hpp>
7+
#include <homekit/HKDevice.hpp>
68
#include <mqtt/MQTTConnection.h>
79
#include <mqtt/MQTTGroup.hpp>
810
#include <mqtt/MQTTSensorGroup.hpp>

main/homekit/HKDevice.hpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,9 @@ namespace homekit
2020
{
2121
struct HKDevice
2222
{
23-
24-
static void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context)
25-
{
26-
printf("Switch On Callback\r\n");
27-
printf("With ID: %i\r\n", _ch->id);
28-
if (_ch->value.bool_value != on.bool_value)
29-
{
30-
_ch->value.bool_value = on.bool_value;
31-
homekit_characteristic_notify(_ch, _ch->value);
32-
}
33-
(*static_cast<HKDevice*>(context)).setDeviceState(on.bool_value);
34-
}
35-
3623
static void deviceSetCallback(homekit_characteristic_t *ch, const homekit_value_t value)
3724
{
3825
(*static_cast<HKDevice*>(ch->context)).setDeviceState(value.bool_value);
39-
printf("Device has been set!");
4026
}
4127

4228
void setDeviceState(bool on)
@@ -54,9 +40,6 @@ namespace homekit
5440
mSetNeedsUpdateCB(on);
5541
}
5642
}
57-
printf("Updated with new value");
58-
printf(sceneName.c_str());
59-
printf("\r\n");
6043
}
6144

6245
HKDevice(const HKDevice&) =delete;
@@ -96,6 +79,7 @@ namespace homekit
9679
{
9780
return mIsActive;
9881
}
82+
9983
std::string sceneName = "";
10084
std::string iconName = "";
10185
uint16_t groupId;

main/ui/UIWidgetBuilder.hpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,39 @@ namespace util
4949
return button;
5050
};
5151

52+
auto operator()(HKDevicePtr ptr, ScreenType screen) -> std::shared_ptr<UIButton>
53+
{
54+
auto button = std::make_shared<UIButton>(&(screen->mTft), Frame(), ptr->groupId);
55+
button->setBackgroundColor(Color::InactiveBgColor());
56+
button->setLabel(ptr->sceneName);
57+
58+
const auto icons = GetIconFileNames(ptr->iconName);
59+
const auto textColor = ptr->isActive() ? Color::ActiveBgColor() : Color::InactiveTextColor();
60+
const auto imagePath = ptr->isActive() ? icons.first : icons.second;
61+
button->setImage(imagePath);
62+
button->setTextColor(textColor);
63+
auto& context = screen->mpAppContext;
64+
button->addTargetAction([ptr, context](const uint16_t id) {
65+
const bool isActive = ptr->isActive();
66+
ptr->setDeviceState(!isActive);
67+
});
68+
69+
auto& screenSaver = screen->mScreenSaver;
70+
ptr->mSetNeedsUpdateCB = [ptr, weakBtn = std::weak_ptr<UIButton>(button), &screenSaver, icons](const bool state) {
71+
const auto textColor = state ? Color::ActiveBgColor() : Color::InactiveTextColor();
72+
const auto imagePath = state ? icons.first : icons.second;
73+
auto button = weakBtn.lock();
74+
if (!button)
75+
{
76+
return;
77+
}
78+
button->setTextColor(textColor);
79+
button->setImage(imagePath);
80+
screenSaver.activate();
81+
};
82+
return button;
83+
};
84+
5285
auto operator()(MQTTSensorGroupPtr ptr, ScreenType screen) -> std::shared_ptr<UIWidget>
5386
{
5487
auto button = std::make_shared<UISensorComboWidget>(&(screen->mTft), Frame(), ptr->groupId);
@@ -127,4 +160,4 @@ namespace util
127160
}
128161
};
129162
} // namespace util
130-
} // namespace gfx
163+
} // namespace gfx

0 commit comments

Comments
 (0)