Skip to content

Commit

Permalink
Add demo integration (esphome#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter authored Jul 29, 2021
1 parent af8d048 commit 16dbbfa
Show file tree
Hide file tree
Showing 11 changed files with 969 additions and 0 deletions.
451 changes: 451 additions & 0 deletions esphome/components/demo/__init__.py

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions esphome/components/demo/demo_binary_sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"

namespace esphome {
namespace demo {

class DemoBinarySensor : public binary_sensor::BinarySensor, public PollingComponent {
public:
void setup() override { this->publish_initial_state(false); }
void update() override {
bool new_state = last_state_ = !last_state_;
this->publish_state(new_state);
}

protected:
bool last_state_ = false;
};

} // namespace demo
} // namespace esphome
157 changes: 157 additions & 0 deletions esphome/components/demo/demo_climate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/climate/climate.h"

namespace esphome {
namespace demo {

enum class DemoClimateType {
TYPE_1,
TYPE_2,
TYPE_3,
};

class DemoClimate : public climate::Climate, public Component {
public:
void set_type(DemoClimateType type) { type_ = type; }
void setup() override {
switch (type_) {
case DemoClimateType::TYPE_1:
this->current_temperature = 20.0;
this->target_temperature = 21.0;
this->mode = climate::CLIMATE_MODE_HEAT;
this->action = climate::CLIMATE_ACTION_HEATING;
break;
case DemoClimateType::TYPE_2:
this->target_temperature = 21.5;
this->mode = climate::CLIMATE_MODE_AUTO;
this->action = climate::CLIMATE_ACTION_COOLING;
this->fan_mode = climate::CLIMATE_FAN_HIGH;
this->custom_preset = {"My Preset"};
break;
case DemoClimateType::TYPE_3:
this->current_temperature = 21.5;
this->target_temperature_low = 21.0;
this->target_temperature_high = 22.5;
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
this->custom_fan_mode = {"Auto Low"};
this->swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
this->preset = climate::CLIMATE_PRESET_AWAY;
break;
}
this->publish_state();
}

protected:
void control(const climate::ClimateCall &call) override {
if (call.get_mode().has_value()) {
this->mode = *call.get_mode();
}
if (call.get_target_temperature().has_value()) {
this->target_temperature = *call.get_target_temperature();
}
if (call.get_target_temperature_low().has_value()) {
this->target_temperature_low = *call.get_target_temperature_low();
}
if (call.get_target_temperature_high().has_value()) {
this->target_temperature_high = *call.get_target_temperature_high();
}
if (call.get_fan_mode().has_value()) {
this->fan_mode = *call.get_fan_mode();
this->custom_fan_mode.reset();
}
if (call.get_swing_mode().has_value()) {
this->swing_mode = *call.get_swing_mode();
}
if (call.get_custom_fan_mode().has_value()) {
this->custom_fan_mode = *call.get_custom_fan_mode();
this->fan_mode.reset();
}
if (call.get_preset().has_value()) {
this->preset = *call.get_preset();
this->custom_preset.reset();
}
if (call.get_custom_preset().has_value()) {
this->custom_preset = *call.get_custom_preset();
this->preset.reset();
}
this->publish_state();
}
climate::ClimateTraits traits() override {
climate::ClimateTraits traits{};
switch (type_) {
case DemoClimateType::TYPE_1:
traits.set_supports_current_temperature(true);
traits.set_supported_modes({
climate::CLIMATE_MODE_OFF,
climate::CLIMATE_MODE_HEAT,
});
traits.set_supports_action(true);
traits.set_visual_temperature_step(0.5);
break;
case DemoClimateType::TYPE_2:
traits.set_supports_current_temperature(false);
traits.set_supported_modes({
climate::CLIMATE_MODE_OFF,
climate::CLIMATE_MODE_HEAT,
climate::CLIMATE_MODE_COOL,
climate::CLIMATE_MODE_AUTO,
climate::CLIMATE_MODE_DRY,
climate::CLIMATE_MODE_FAN_ONLY,
});
traits.set_supports_action(true);
traits.set_supported_fan_modes({
climate::CLIMATE_FAN_ON,
climate::CLIMATE_FAN_OFF,
climate::CLIMATE_FAN_AUTO,
climate::CLIMATE_FAN_LOW,
climate::CLIMATE_FAN_MEDIUM,
climate::CLIMATE_FAN_HIGH,
climate::CLIMATE_FAN_MIDDLE,
climate::CLIMATE_FAN_FOCUS,
climate::CLIMATE_FAN_DIFFUSE,
});
traits.set_supported_custom_fan_modes({"Auto Low", "Auto High"});
traits.set_supported_swing_modes({
climate::CLIMATE_SWING_OFF,
climate::CLIMATE_SWING_BOTH,
climate::CLIMATE_SWING_VERTICAL,
climate::CLIMATE_SWING_HORIZONTAL,
});
traits.set_supported_custom_presets({"My Preset"});
break;
case DemoClimateType::TYPE_3:
traits.set_supports_current_temperature(true);
traits.set_supports_two_point_target_temperature(true);
traits.set_supported_modes({
climate::CLIMATE_MODE_OFF,
climate::CLIMATE_MODE_COOL,
climate::CLIMATE_MODE_HEAT,
climate::CLIMATE_MODE_HEAT_COOL,
});
traits.set_supported_custom_fan_modes({"Auto Low", "Auto High"});
traits.set_supported_swing_modes({
climate::CLIMATE_SWING_OFF,
climate::CLIMATE_SWING_HORIZONTAL,
});
traits.set_supported_presets({
climate::CLIMATE_PRESET_NONE,
climate::CLIMATE_PRESET_HOME,
climate::CLIMATE_PRESET_AWAY,
climate::CLIMATE_PRESET_BOOST,
climate::CLIMATE_PRESET_COMFORT,
climate::CLIMATE_PRESET_ECO,
climate::CLIMATE_PRESET_SLEEP,
climate::CLIMATE_PRESET_ACTIVITY,
});
break;
}
return traits;
}

DemoClimateType type_;
};

} // namespace demo
} // namespace esphome
86 changes: 86 additions & 0 deletions esphome/components/demo/demo_cover.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/cover/cover.h"

namespace esphome {
namespace demo {

enum class DemoCoverType {
TYPE_1,
TYPE_2,
TYPE_3,
TYPE_4,
};

class DemoCover : public cover::Cover, public Component {
public:
void set_type(DemoCoverType type) { type_ = type; }
void setup() override {
switch (type_) {
case DemoCoverType::TYPE_1:
this->position = cover::COVER_OPEN;
break;
case DemoCoverType::TYPE_2:
this->position = 0.7;
break;
case DemoCoverType::TYPE_3:
this->position = 0.1;
this->tilt = 0.8;
break;
case DemoCoverType::TYPE_4:
this->position = cover::COVER_CLOSED;
this->tilt = 1.0;
break;
}
this->publish_state();
}

protected:
void control(const cover::CoverCall &call) override {
if (call.get_position().has_value()) {
float target = *call.get_position();
this->current_operation =
target > this->position ? cover::COVER_OPERATION_OPENING : cover::COVER_OPERATION_CLOSING;

this->set_timeout("move", 2000, [this, target]() {
this->current_operation = cover::COVER_OPERATION_IDLE;
this->position = target;
this->publish_state();
});
}
if (call.get_tilt().has_value()) {
this->tilt = *call.get_tilt();
}
if (call.get_stop()) {
this->cancel_timeout("move");
}

this->publish_state();
}
cover::CoverTraits get_traits() override {
cover::CoverTraits traits{};
switch (type_) {
case DemoCoverType::TYPE_1:
traits.set_is_assumed_state(true);
break;
case DemoCoverType::TYPE_2:
traits.set_supports_position(true);
break;
case DemoCoverType::TYPE_3:
traits.set_supports_position(true);
traits.set_supports_tilt(true);
break;
case DemoCoverType::TYPE_4:
traits.set_is_assumed_state(true);
traits.set_supports_tilt(true);
break;
}
return traits;
}

DemoCoverType type_;
};

} // namespace demo
} // namespace esphome
54 changes: 54 additions & 0 deletions esphome/components/demo/demo_fan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/fan/fan_state.h"

namespace esphome {
namespace demo {

enum class DemoFanType {
TYPE_1,
TYPE_2,
TYPE_3,
TYPE_4,
};

class DemoFan : public Component {
public:
void set_type(DemoFanType type) { type_ = type; }
void set_fan(fan::FanState *fan) { fan_ = fan; }
void setup() override {
fan::FanTraits traits{};

// oscillation
// speed
// direction
// speed_count
switch (type_) {
case DemoFanType::TYPE_1:
break;
case DemoFanType::TYPE_2:
traits.set_oscillation(true);
break;
case DemoFanType::TYPE_3:
traits.set_direction(true);
traits.set_speed(true);
traits.set_supported_speed_count(5);
break;
case DemoFanType::TYPE_4:
traits.set_direction(true);
traits.set_speed(true);
traits.set_supported_speed_count(100);
traits.set_oscillation(true);
break;
}

this->fan_->set_traits(traits);
}

fan::FanState *fan_;
DemoFanType type_;
};

} // namespace demo
} // namespace esphome
Loading

0 comments on commit 16dbbfa

Please sign in to comment.