Skip to content

Commit 6492dd0

Browse files
implement carbon dioxide and monoxide sensors (#1210)
1 parent cd1a7e0 commit 6492dd0

9 files changed

+222
-4
lines changed

fs_src/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ <h1 id="head">Input</h1>
550550
<option id="type_10" value="10">Doorbell</option>
551551
<option id="type_13" value="13">Leak Sensor</option>
552552
<option id="type_14" value="14">Smoke Sensor</option>
553+
<option id="type_15" value="15">Carbon Monoxide Sensor</option>
554+
<option id="type_16" value="16">Carbon Dioxide Sensor</option>
553555
</select>
554556
</div>
555557
<div class="form-control">
@@ -601,6 +603,8 @@ <h1 id="head">Disabled Input</h1>
601603
<option id="type_10" value="10">Doorbell</option>
602604
<option id="type_13" value="13">Leak Sensor</option>
603605
<option id="type_14" value="14">Smoke Sensor</option>
606+
<option id="type_15" value="15">Carbon Monoxide Sensor</option>
607+
<option id="type_16" value="16">Carbon Dioxide Sensor</option>
604608
</select>
605609
</div>
606610
<div class="button-container">
@@ -630,6 +634,8 @@ <h1 id="head">Sensor</h1>
630634
<option id="type_10" value="10">Doorbell</option>
631635
<option id="type_13" value="13">Leak Sensor</option>
632636
<option id="type_14" value="14">Smoke Sensor</option>
637+
<option id="type_15" value="15">Carbon Monoxide Sensor</option>
638+
<option id="type_16" value="16">Carbon Dioxide Sensor</option>
633639
</select>
634640
</div>
635641
<div class="form-control">

fs_src/script.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class Component_Type {
6060
static kTemperatureSensor = 12;
6161
static kLeakSensor = 13;
6262
static kSmokeSensor = 14;
63-
static kMax = 15;
63+
static kCarbonMonoxideSensor = 15;
64+
static kCarbonDioxideSensor = 16;
65+
static kMax = 17;
6466
};
6567

6668
// Keep in sync with shelly::LightBulbController::BulbType.
@@ -592,6 +594,8 @@ function findOrAddContainer(cd) {
592594
case Component_Type.kContactSensor:
593595
case Component_Type.kLeakSensor:
594596
case Component_Type.kSmokeSensor:
597+
case Component_Type.kCarbonMonoxideSensor:
598+
case Component_Type.kCarbonDioxideSensor:
595599
c = el("sensor_template").cloneNode(true);
596600
c.id = elId;
597601
el(c, "save_btn").onclick = function() {
@@ -662,6 +666,7 @@ function rgbState(c, newState) {
662666

663667
function updateComponent(cd) {
664668
let c = findOrAddContainer(cd);
669+
let whatSensor;
665670
if (!c) return;
666671
switch (cd.type) {
667672
case Component_Type.kSwitch:
@@ -864,10 +869,19 @@ function updateComponent(cd) {
864869
break;
865870
}
866871
case Component_Type.kMotionSensor:
872+
whatSensor || = "motion";
867873
case Component_Type.kOccupancySensor:
874+
whatSensor || = "occupancy";
868875
case Component_Type.kContactSensor:
876+
whatSensor || = "contact";
869877
case Component_Type.kLeakSensor:
870-
case Component_Type.kSmokeSensor: {
878+
whatSensor || = "leak";
879+
case Component_Type.kSmokeSensor:
880+
whatSensor || = "smoke";
881+
case Component_Type.kCarbonMonoxideSensor:
882+
whatSensor || = "carbon monoxide";
883+
case Component_Type.kCarbonDioxideSensor: {
884+
whatSensor || = "carbon dioxide";
871885
let headText = `Input ${cd.id}`;
872886
if (cd.name) headText += ` (${cd.name})`;
873887
updateInnerText(el(c, "head"), headText);
@@ -878,8 +892,8 @@ function updateComponent(cd) {
878892
setValueIfNotModified(el(c, "idle_time"), cd.idle_time);
879893
el(c, "idle_time_container").style.display =
880894
(cd.in_mode == 0 ? "none" : "block");
881-
let what = (cd.type == 7 ? "motion" : "occupancy");
882-
let statusText = (cd.state ? `${what} detected` : `no ${what} detected`);
895+
let statusText =
896+
(cd.state ? `${whatSensor} detected` : `no ${whatSensor} detected`);
883897
if (cd.last_ev_age > 0) {
884898
statusText += `; last ${secondsToDateString(cd.last_ev_age)} ago`;
885899
}

src/shelly_common.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#define SHELLY_HAP_AID_BASE_TEMPERATURE_SENSOR 0xc00
4242
#define SHELLY_HAP_AID_BASE_LEAK_SENSOR 0xe00
4343
#define SHELLY_HAP_AID_BASE_SMOKE_SENSOR 0xf00
44+
#define SHELLY_HAP_AID_BASE_CARBON_MONOXIDE_SENSOR 0x1000
45+
#define SHELLY_HAP_AID_BASE_CARBON_DIOXIDE_SENSOR 0x1100
4446

4547
#define SHELLY_HAP_IID_BASE_SWITCH 0x100
4648
#define SHELLY_HAP_IID_STEP_SWITCH 4
@@ -67,6 +69,8 @@
6769
#define SHELLY_HAP_IID_BASE_LEAK_SENSOR 0xe00
6870
#define SHELLY_HAP_IID_BASE_SMOKE_SENSOR 0xf00
6971
#define SHELLY_HAP_IID_BASE_ADAPTIVE_LIGHTING 0x1000
72+
#define SHELLY_HAP_IID_BASE_CARBON_MONOXIDE_SENSOR 0x1100
73+
#define SHELLY_HAP_IID_BASE_CARBON_DIOXIDE_SENSOR 0x1200
7074

7175
#define kChangeReasonAuto "AUTO"
7276
#define kChangeReasonAutoWithNotification "AUTO_NOTIFICATION"

src/shelly_component.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Component {
4040
kTemperatureSensor = 12,
4141
kLeakSensor = 13,
4242
kSmokeSensor = 14,
43+
kCarbonMonoxideSensor = 15,
44+
kCarbonDioxideSensor = 16,
4345
kMax,
4446
};
4547

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) Shelly-HomeKit Contributors
3+
* All rights reserved
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "shelly_hap_carbon_dioxide_sensor.hpp"
19+
20+
namespace shelly {
21+
namespace hap {
22+
23+
CarbonDioxideSensor::CarbonDioxideSensor(int id, Input *in,
24+
struct mgos_config_in_sensor *cfg)
25+
: SensorBase(id, in, cfg, SHELLY_HAP_IID_BASE_CARBON_DIOXIDE_SENSOR,
26+
&kHAPServiceType_CarbonDioxideSensor,
27+
kHAPServiceDebugDescription_CarbonDioxideSensor) {
28+
}
29+
30+
CarbonDioxideSensor::~CarbonDioxideSensor() {
31+
}
32+
33+
Component::Type CarbonDioxideSensor::type() const {
34+
return Type::kCarbonDioxideSensor;
35+
}
36+
37+
Status CarbonDioxideSensor::Init() {
38+
const Status &st = SensorBase::Init();
39+
if (!st.ok()) return st;
40+
AddChar(new mgos::hap::UInt8Characteristic(
41+
svc_.iid + 2, &kHAPCharacteristicType_CarbonDioxideDetected, 0, 1, 1,
42+
std::bind(&mgos::hap::ReadUInt8<bool>, _1, _2, _3, &state_),
43+
true /* supports_notification */, nullptr /* write_handler */,
44+
kHAPCharacteristicDebugDescription_CarbonDioxideDetected));
45+
return Status::OK();
46+
}
47+
48+
} // namespace hap
49+
} // namespace shelly
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Shelly-HomeKit Contributors
3+
* All rights reserved
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include "shelly_hap_sensor_base.hpp"
21+
22+
namespace shelly {
23+
namespace hap {
24+
25+
class CarbonDioxideSensor : public SensorBase {
26+
public:
27+
CarbonDioxideSensor(int id, Input *in, struct mgos_config_in_sensor *cfg);
28+
virtual ~CarbonDioxideSensor();
29+
30+
// Component interface impl.
31+
Status Init() override;
32+
virtual Type type() const override;
33+
};
34+
35+
} // namespace hap
36+
} // namespace shelly
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) Shelly-HomeKit Contributors
3+
* All rights reserved
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "shelly_hap_carbon_monoxide_sensor.hpp"
19+
20+
namespace shelly {
21+
namespace hap {
22+
23+
CarbonMonoxideSensor::CarbonMonoxideSensor(int id, Input *in,
24+
struct mgos_config_in_sensor *cfg)
25+
: SensorBase(id, in, cfg, SHELLY_HAP_IID_BASE_CARBON_MONOXIDE_SENSOR,
26+
&kHAPServiceType_CarbonMonoxideSensor,
27+
kHAPServiceDebugDescription_CarbonMonoxideSensor) {
28+
}
29+
30+
CarbonMonoxideSensor::~CarbonMonoxideSensor() {
31+
}
32+
33+
Component::Type CarbonMonoxideSensor::type() const {
34+
return Type::kCarbonMonoxideSensor;
35+
}
36+
37+
Status CarbonMonoxideSensor::Init() {
38+
const Status &st = SensorBase::Init();
39+
if (!st.ok()) return st;
40+
AddChar(new mgos::hap::UInt8Characteristic(
41+
svc_.iid + 2, &kHAPCharacteristicType_CarbonMonoxideDetected, 0, 1, 1,
42+
std::bind(&mgos::hap::ReadUInt8<bool>, _1, _2, _3, &state_),
43+
true /* supports_notification */, nullptr /* write_handler */,
44+
kHAPCharacteristicDebugDescription_CarbonMonoxideDetected));
45+
return Status::OK();
46+
}
47+
48+
} // namespace hap
49+
} // namespace shelly
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Shelly-HomeKit Contributors
3+
* All rights reserved
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include "shelly_hap_sensor_base.hpp"
21+
22+
namespace shelly {
23+
namespace hap {
24+
25+
class CarbonMonoxideSensor : public SensorBase {
26+
public:
27+
CarbonMonoxideSensor(int id, Input *in, struct mgos_config_in_sensor *cfg);
28+
virtual ~CarbonMonoxideSensor();
29+
30+
// Component interface impl.
31+
Status Init() override;
32+
virtual Type type() const override;
33+
};
34+
35+
} // namespace hap
36+
} // namespace shelly

src/shelly_hap_input.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "mgos.hpp"
2121
#include "mgos_hap.h"
2222

23+
#include "shelly_hap_carbon_dioxide_sensor.hpp"
24+
#include "shelly_hap_carbon_monoxide_sensor.hpp"
2325
#include "shelly_hap_contact_sensor.hpp"
2426
#include "shelly_hap_doorbell.hpp"
2527
#include "shelly_hap_leak_sensor.hpp"
@@ -156,6 +158,20 @@ Status ShellyInput::Init() {
156158
s_ = cs;
157159
break;
158160
}
161+
case Type::kCarbonMonoxideSensor: {
162+
auto *cs = new hap::CarbonMonoxideSensor(
163+
id(), in_, (struct mgos_config_in_sensor *) &cfg_->sensor);
164+
c_.reset(cs);
165+
s_ = cs;
166+
break;
167+
}
168+
case Type::kCarbonDioxideSensor: {
169+
auto *cs = new hap::CarbonDioxideSensor(
170+
id(), in_, (struct mgos_config_in_sensor *) &cfg_->sensor);
171+
c_.reset(cs);
172+
s_ = cs;
173+
break;
174+
}
159175
default: {
160176
return mgos::Errorf(STATUS_INVALID_ARGUMENT, "Invalid type %d",
161177
(int) initial_type_);
@@ -225,6 +241,10 @@ uint16_t ShellyInput::GetAIDBase() const {
225241
return SHELLY_HAP_AID_BASE_LEAK_SENSOR;
226242
case Type::kSmokeSensor:
227243
return SHELLY_HAP_AID_BASE_SMOKE_SENSOR;
244+
case Type::kCarbonMonoxideSensor:
245+
return SHELLY_HAP_AID_BASE_CARBON_MONOXIDE_SENSOR;
246+
case Type::kCarbonDioxideSensor:
247+
return SHELLY_HAP_AID_BASE_CARBON_DIOXIDE_SENSOR;
228248
default:
229249
return 0;
230250
}
@@ -245,6 +265,8 @@ bool ShellyInput::IsValidType(int type) {
245265
case (int) Type::kDoorbell:
246266
case (int) Type::kLeakSensor:
247267
case (int) Type::kSmokeSensor:
268+
case (int) Type::kCarbonMonoxideSensor:
269+
case (int) Type::kCarbonDioxideSensor:
248270
return true;
249271
}
250272
return false;

0 commit comments

Comments
 (0)