-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from CarsonF/number
- Loading branch information
Showing
12 changed files
with
164 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Optional, OrderedDict | ||
|
||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.components import number | ||
from esphome.const import ( | ||
CONF_ID, | ||
CONF_RESTORE_VALUE, | ||
) | ||
|
||
PersistedNumber = number.number_ns.class_( | ||
"PersistedNumber", number.Number, cg.Component | ||
) | ||
|
||
PERSISTED_NUMBER_SCHEMA = number.NUMBER_SCHEMA.extend( | ||
{ | ||
cv.GenerateID(): cv.declare_id(PersistedNumber), | ||
cv.Optional(CONF_RESTORE_VALUE, default=True): cv.boolean, | ||
} | ||
) | ||
|
||
|
||
async def new_persisted_number( | ||
config: OrderedDict, | ||
min_value: float, | ||
max_value: float, | ||
step: Optional[float] = None, | ||
): | ||
var = await number.new_number( | ||
config, min_value=min_value, max_value=max_value, step=step | ||
) | ||
await cg.register_component(var, config) | ||
if CONF_RESTORE_VALUE in config: | ||
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE])) | ||
return var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "persisted_number.h" | ||
#include "esphome/core/log.h" | ||
|
||
namespace esphome { | ||
namespace number { | ||
|
||
auto PersistedNumber::control(float newValue) -> void { | ||
this->publish_state(newValue); | ||
if (this->restore_value_) { | ||
this->pref_.save(&newValue); | ||
} | ||
} | ||
|
||
auto PersistedNumber::setup() -> void { | ||
float value; | ||
if (!this->restore_value_) { | ||
value = this->traits.get_min_value(); | ||
} else { | ||
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash()); | ||
if (this->pref_.load(&value)) { | ||
ESP_LOGI("number", "'%s': Restored state %f", this->get_name().c_str(), value); | ||
} else { | ||
ESP_LOGI("number", "'%s': No previous state found", this->get_name().c_str()); | ||
value = this->traits.get_min_value(); | ||
} | ||
} | ||
this->publish_state(value); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "esphome/components/number/number.h" | ||
#include "esphome/core/component.h" | ||
#include "esphome/core/preferences.h" | ||
|
||
namespace esphome { | ||
namespace number { | ||
|
||
class PersistedNumber : public number::Number, public Component { | ||
public: | ||
float get_setup_priority() const override { return setup_priority::HARDWARE; } | ||
void set_restore_value(bool restore) { this->restore_value_ = restore; } | ||
void setup() override; | ||
|
||
protected: | ||
void control(float value) override; | ||
|
||
bool restore_value_{false}; | ||
ESPPreferenceObject pref_; | ||
}; | ||
|
||
} // namespace roode | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import OrderedDict | ||
|
||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.const import CONF_ICON, CONF_MAX_VALUE | ||
from esphome.cpp_generator import MockObj | ||
|
||
from ..persisted_number import PERSISTED_NUMBER_SCHEMA, new_persisted_number | ||
from . import Roode, CONF_ROODE_ID | ||
|
||
DEPENDENCIES = ["roode"] | ||
AUTO_LOAD = ["number", "persisted_number"] | ||
|
||
CONF_PEOPLE_COUNTER = "people_counter" | ||
|
||
CONFIG_SCHEMA = cv.Schema( | ||
{ | ||
cv.GenerateID(CONF_ROODE_ID): cv.use_id(Roode), | ||
cv.Optional(CONF_PEOPLE_COUNTER): PERSISTED_NUMBER_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_ICON, default="mdi:counter"): cv.icon, # new default | ||
cv.Optional(CONF_MAX_VALUE, 10): cv.int_range(1, 255), | ||
} | ||
), | ||
} | ||
) | ||
|
||
|
||
async def setup_people_counter(config: OrderedDict, hub: MockObj): | ||
counter = await new_persisted_number( | ||
config, min_value=0, step=1, max_value=config[CONF_MAX_VALUE] | ||
) | ||
cg.add(hub.set_people_counter(counter)) | ||
|
||
|
||
async def to_code(config: OrderedDict): | ||
hub = await cg.get_variable(config[CONF_ROODE_ID]) | ||
if CONF_PEOPLE_COUNTER in config: | ||
await setup_people_counter(config[CONF_PEOPLE_COUNTER], hub) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.