Files
esphome-dev/esphome/components/hx711/sensor.py
T
Otto Winter 6682c43dfa 🏗 Merge C++ into python codebase (#504)
## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-17 12:06:00 +02:00

38 lines
1.4 KiB
Python

from esphome import pins
from esphome.components import sensor
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_CLK_PIN, CONF_GAIN, CONF_ID, CONF_NAME, CONF_UPDATE_INTERVAL, \
ICON_SCALE
hx711_ns = cg.esphome_ns.namespace('hx711')
HX711Sensor = hx711_ns.class_('HX711Sensor', sensor.PollingSensorComponent)
CONF_DOUT_PIN = 'dout_pin'
HX711Gain = hx711_ns.enum('HX711Gain')
GAINS = {
128: HX711Gain.HX711_GAIN_128,
32: HX711Gain.HX711_GAIN_32,
64: HX711Gain.HX711_GAIN_64,
}
CONFIG_SCHEMA = cv.nameable(sensor.sensor_schema('', ICON_SCALE, 0).extend({
cv.GenerateID(): cv.declare_variable_id(HX711Sensor),
cv.Required(CONF_DOUT_PIN): pins.gpio_input_pin_schema,
cv.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_GAIN, default=128): cv.one_of(*GAINS, int=True),
cv.Optional(CONF_UPDATE_INTERVAL, default='60s'): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA))
def to_code(config):
dout_pin = yield cg.gpio_pin_expression(config[CONF_DOUT_PIN])
sck_pin = yield cg.gpio_pin_expression(config[CONF_CLK_PIN])
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], dout_pin, sck_pin,
config[CONF_UPDATE_INTERVAL])
yield cg.register_component(var, config)
yield sensor.register_sensor(var, config)
cg.add(var.set_gain(GAINS[config[CONF_GAIN]]))