Skip to content

Commit

Permalink
implement carbon dioxide and monoxide sensors (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
timoschilling authored Feb 1, 2023
1 parent cd1a7e0 commit 6492dd0
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 4 deletions.
6 changes: 6 additions & 0 deletions fs_src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ <h1 id="head">Input</h1>
<option id="type_10" value="10">Doorbell</option>
<option id="type_13" value="13">Leak Sensor</option>
<option id="type_14" value="14">Smoke Sensor</option>
<option id="type_15" value="15">Carbon Monoxide Sensor</option>
<option id="type_16" value="16">Carbon Dioxide Sensor</option>
</select>
</div>
<div class="form-control">
Expand Down Expand Up @@ -601,6 +603,8 @@ <h1 id="head">Disabled Input</h1>
<option id="type_10" value="10">Doorbell</option>
<option id="type_13" value="13">Leak Sensor</option>
<option id="type_14" value="14">Smoke Sensor</option>
<option id="type_15" value="15">Carbon Monoxide Sensor</option>
<option id="type_16" value="16">Carbon Dioxide Sensor</option>
</select>
</div>
<div class="button-container">
Expand Down Expand Up @@ -630,6 +634,8 @@ <h1 id="head">Sensor</h1>
<option id="type_10" value="10">Doorbell</option>
<option id="type_13" value="13">Leak Sensor</option>
<option id="type_14" value="14">Smoke Sensor</option>
<option id="type_15" value="15">Carbon Monoxide Sensor</option>
<option id="type_16" value="16">Carbon Dioxide Sensor</option>
</select>
</div>
<div class="form-control">
Expand Down
22 changes: 18 additions & 4 deletions fs_src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class Component_Type {
static kTemperatureSensor = 12;
static kLeakSensor = 13;
static kSmokeSensor = 14;
static kMax = 15;
static kCarbonMonoxideSensor = 15;
static kCarbonDioxideSensor = 16;
static kMax = 17;
};

// Keep in sync with shelly::LightBulbController::BulbType.
Expand Down Expand Up @@ -592,6 +594,8 @@ function findOrAddContainer(cd) {
case Component_Type.kContactSensor:
case Component_Type.kLeakSensor:
case Component_Type.kSmokeSensor:
case Component_Type.kCarbonMonoxideSensor:
case Component_Type.kCarbonDioxideSensor:
c = el("sensor_template").cloneNode(true);
c.id = elId;
el(c, "save_btn").onclick = function() {
Expand Down Expand Up @@ -662,6 +666,7 @@ function rgbState(c, newState) {

function updateComponent(cd) {
let c = findOrAddContainer(cd);
let whatSensor;
if (!c) return;
switch (cd.type) {
case Component_Type.kSwitch:
Expand Down Expand Up @@ -864,10 +869,19 @@ function updateComponent(cd) {
break;
}
case Component_Type.kMotionSensor:
whatSensor || = "motion";
case Component_Type.kOccupancySensor:
whatSensor || = "occupancy";
case Component_Type.kContactSensor:
whatSensor || = "contact";
case Component_Type.kLeakSensor:
case Component_Type.kSmokeSensor: {
whatSensor || = "leak";
case Component_Type.kSmokeSensor:
whatSensor || = "smoke";
case Component_Type.kCarbonMonoxideSensor:
whatSensor || = "carbon monoxide";
case Component_Type.kCarbonDioxideSensor: {
whatSensor || = "carbon dioxide";
let headText = `Input ${cd.id}`;
if (cd.name) headText += ` (${cd.name})`;
updateInnerText(el(c, "head"), headText);
Expand All @@ -878,8 +892,8 @@ function updateComponent(cd) {
setValueIfNotModified(el(c, "idle_time"), cd.idle_time);
el(c, "idle_time_container").style.display =
(cd.in_mode == 0 ? "none" : "block");
let what = (cd.type == 7 ? "motion" : "occupancy");
let statusText = (cd.state ? `${what} detected` : `no ${what} detected`);
let statusText =
(cd.state ? `${whatSensor} detected` : `no ${whatSensor} detected`);
if (cd.last_ev_age > 0) {
statusText += `; last ${secondsToDateString(cd.last_ev_age)} ago`;
}
Expand Down
4 changes: 4 additions & 0 deletions src/shelly_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#define SHELLY_HAP_AID_BASE_TEMPERATURE_SENSOR 0xc00
#define SHELLY_HAP_AID_BASE_LEAK_SENSOR 0xe00
#define SHELLY_HAP_AID_BASE_SMOKE_SENSOR 0xf00
#define SHELLY_HAP_AID_BASE_CARBON_MONOXIDE_SENSOR 0x1000
#define SHELLY_HAP_AID_BASE_CARBON_DIOXIDE_SENSOR 0x1100

#define SHELLY_HAP_IID_BASE_SWITCH 0x100
#define SHELLY_HAP_IID_STEP_SWITCH 4
Expand All @@ -67,6 +69,8 @@
#define SHELLY_HAP_IID_BASE_LEAK_SENSOR 0xe00
#define SHELLY_HAP_IID_BASE_SMOKE_SENSOR 0xf00
#define SHELLY_HAP_IID_BASE_ADAPTIVE_LIGHTING 0x1000
#define SHELLY_HAP_IID_BASE_CARBON_MONOXIDE_SENSOR 0x1100
#define SHELLY_HAP_IID_BASE_CARBON_DIOXIDE_SENSOR 0x1200

#define kChangeReasonAuto "AUTO"
#define kChangeReasonAutoWithNotification "AUTO_NOTIFICATION"
Expand Down
2 changes: 2 additions & 0 deletions src/shelly_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Component {
kTemperatureSensor = 12,
kLeakSensor = 13,
kSmokeSensor = 14,
kCarbonMonoxideSensor = 15,
kCarbonDioxideSensor = 16,
kMax,
};

Expand Down
49 changes: 49 additions & 0 deletions src/shelly_hap_carbon_dioxide_sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) Shelly-HomeKit Contributors
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "shelly_hap_carbon_dioxide_sensor.hpp"

namespace shelly {
namespace hap {

CarbonDioxideSensor::CarbonDioxideSensor(int id, Input *in,
struct mgos_config_in_sensor *cfg)
: SensorBase(id, in, cfg, SHELLY_HAP_IID_BASE_CARBON_DIOXIDE_SENSOR,
&kHAPServiceType_CarbonDioxideSensor,
kHAPServiceDebugDescription_CarbonDioxideSensor) {
}

CarbonDioxideSensor::~CarbonDioxideSensor() {
}

Component::Type CarbonDioxideSensor::type() const {
return Type::kCarbonDioxideSensor;
}

Status CarbonDioxideSensor::Init() {
const Status &st = SensorBase::Init();
if (!st.ok()) return st;
AddChar(new mgos::hap::UInt8Characteristic(
svc_.iid + 2, &kHAPCharacteristicType_CarbonDioxideDetected, 0, 1, 1,
std::bind(&mgos::hap::ReadUInt8<bool>, _1, _2, _3, &state_),
true /* supports_notification */, nullptr /* write_handler */,
kHAPCharacteristicDebugDescription_CarbonDioxideDetected));
return Status::OK();
}

} // namespace hap
} // namespace shelly
36 changes: 36 additions & 0 deletions src/shelly_hap_carbon_dioxide_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) Shelly-HomeKit Contributors
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "shelly_hap_sensor_base.hpp"

namespace shelly {
namespace hap {

class CarbonDioxideSensor : public SensorBase {
public:
CarbonDioxideSensor(int id, Input *in, struct mgos_config_in_sensor *cfg);
virtual ~CarbonDioxideSensor();

// Component interface impl.
Status Init() override;
virtual Type type() const override;
};

} // namespace hap
} // namespace shelly
49 changes: 49 additions & 0 deletions src/shelly_hap_carbon_monoxide_sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) Shelly-HomeKit Contributors
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "shelly_hap_carbon_monoxide_sensor.hpp"

namespace shelly {
namespace hap {

CarbonMonoxideSensor::CarbonMonoxideSensor(int id, Input *in,
struct mgos_config_in_sensor *cfg)
: SensorBase(id, in, cfg, SHELLY_HAP_IID_BASE_CARBON_MONOXIDE_SENSOR,
&kHAPServiceType_CarbonMonoxideSensor,
kHAPServiceDebugDescription_CarbonMonoxideSensor) {
}

CarbonMonoxideSensor::~CarbonMonoxideSensor() {
}

Component::Type CarbonMonoxideSensor::type() const {
return Type::kCarbonMonoxideSensor;
}

Status CarbonMonoxideSensor::Init() {
const Status &st = SensorBase::Init();
if (!st.ok()) return st;
AddChar(new mgos::hap::UInt8Characteristic(
svc_.iid + 2, &kHAPCharacteristicType_CarbonMonoxideDetected, 0, 1, 1,
std::bind(&mgos::hap::ReadUInt8<bool>, _1, _2, _3, &state_),
true /* supports_notification */, nullptr /* write_handler */,
kHAPCharacteristicDebugDescription_CarbonMonoxideDetected));
return Status::OK();
}

} // namespace hap
} // namespace shelly
36 changes: 36 additions & 0 deletions src/shelly_hap_carbon_monoxide_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) Shelly-HomeKit Contributors
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "shelly_hap_sensor_base.hpp"

namespace shelly {
namespace hap {

class CarbonMonoxideSensor : public SensorBase {
public:
CarbonMonoxideSensor(int id, Input *in, struct mgos_config_in_sensor *cfg);
virtual ~CarbonMonoxideSensor();

// Component interface impl.
Status Init() override;
virtual Type type() const override;
};

} // namespace hap
} // namespace shelly
22 changes: 22 additions & 0 deletions src/shelly_hap_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "mgos.hpp"
#include "mgos_hap.h"

#include "shelly_hap_carbon_dioxide_sensor.hpp"
#include "shelly_hap_carbon_monoxide_sensor.hpp"
#include "shelly_hap_contact_sensor.hpp"
#include "shelly_hap_doorbell.hpp"
#include "shelly_hap_leak_sensor.hpp"
Expand Down Expand Up @@ -156,6 +158,20 @@ Status ShellyInput::Init() {
s_ = cs;
break;
}
case Type::kCarbonMonoxideSensor: {
auto *cs = new hap::CarbonMonoxideSensor(
id(), in_, (struct mgos_config_in_sensor *) &cfg_->sensor);
c_.reset(cs);
s_ = cs;
break;
}
case Type::kCarbonDioxideSensor: {
auto *cs = new hap::CarbonDioxideSensor(
id(), in_, (struct mgos_config_in_sensor *) &cfg_->sensor);
c_.reset(cs);
s_ = cs;
break;
}
default: {
return mgos::Errorf(STATUS_INVALID_ARGUMENT, "Invalid type %d",
(int) initial_type_);
Expand Down Expand Up @@ -225,6 +241,10 @@ uint16_t ShellyInput::GetAIDBase() const {
return SHELLY_HAP_AID_BASE_LEAK_SENSOR;
case Type::kSmokeSensor:
return SHELLY_HAP_AID_BASE_SMOKE_SENSOR;
case Type::kCarbonMonoxideSensor:
return SHELLY_HAP_AID_BASE_CARBON_MONOXIDE_SENSOR;
case Type::kCarbonDioxideSensor:
return SHELLY_HAP_AID_BASE_CARBON_DIOXIDE_SENSOR;
default:
return 0;
}
Expand All @@ -245,6 +265,8 @@ bool ShellyInput::IsValidType(int type) {
case (int) Type::kDoorbell:
case (int) Type::kLeakSensor:
case (int) Type::kSmokeSensor:
case (int) Type::kCarbonMonoxideSensor:
case (int) Type::kCarbonDioxideSensor:
return true;
}
return false;
Expand Down

0 comments on commit 6492dd0

Please sign in to comment.