1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-25 13:13:48 +01:00

Add QMC5883L Sensor + Improvements to HMC5883L (#671)

* Add QMC5883L and Updated HMC5883L

* add tests

* changed to oversampling

* fix pylint

* fix private method

* typo fix

* fix protected method

* Clean up code and PR recomendations

* fix tests

* remote file

* fix qmc oversampling unit

* Remove hmc5883l config logging

Either the units are converted to the user values like 1x, 8x oversampling or not printed at all. Printing the machine-value of these is only confusing users.

* Changes for validate_enum

Move stuff that can be done beforehand out of the bound function, use text_type for py2/3 compatability.

* Remove unused constant

* Remove duplicate tests

* Repeat remove config print

* remove changes to test2 since bin is to large

* Add comment to HMC5583L


Co-authored-by: Timothy Purchas <timothy@TPF.local>
Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
Tim P
2019-11-27 04:43:11 +11:00
committed by Otto Winter
parent 36ffef083b
commit fa1adfd934
8 changed files with 378 additions and 15 deletions

View File

@@ -2,8 +2,10 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_RANGE, ICON_MAGNET, UNIT_MICROTESLA, \
UNIT_DEGREES, ICON_SCREEN_ROTATION
from esphome.const import (CONF_ADDRESS, CONF_ID, CONF_OVERSAMPLING, CONF_RANGE, ICON_MAGNET,
UNIT_MICROTESLA, UNIT_DEGREES, ICON_SCREEN_ROTATION,
CONF_UPDATE_INTERVAL)
from esphome.py_compat import text_type
DEPENDENCIES = ['i2c']
@@ -16,6 +18,25 @@ CONF_HEADING = 'heading'
HMC5883LComponent = hmc5883l_ns.class_('HMC5883LComponent', cg.PollingComponent, i2c.I2CDevice)
HMC5883LOversampling = hmc5883l_ns.enum('HMC5883LOversampling')
HMC5883LOversamplings = {
1: HMC5883LOversampling.HMC5883L_OVERSAMPLING_1,
2: HMC5883LOversampling.HMC5883L_OVERSAMPLING_2,
4: HMC5883LOversampling.HMC5883L_OVERSAMPLING_4,
8: HMC5883LOversampling.HMC5883L_OVERSAMPLING_8,
}
HMC5883LDatarate = hmc5883l_ns.enum('HMC5883LDatarate')
HMC5883LDatarates = {
0.75: HMC5883LDatarate.HMC5883L_DATARATE_0_75_HZ,
1.5: HMC5883LDatarate.HMC5883L_DATARATE_1_5_HZ,
3.0: HMC5883LDatarate.HMC5883L_DATARATE_3_0_HZ,
7.5: HMC5883LDatarate.HMC5883L_DATARATE_7_5_HZ,
15: HMC5883LDatarate.HMC5883L_DATARATE_15_0_HZ,
30: HMC5883LDatarate.HMC5883L_DATARATE_30_0_HZ,
75: HMC5883LDatarate.HMC5883L_DATARATE_75_0_HZ,
}
HMC5883LRange = hmc5883l_ns.enum('HMC5883LRange')
HMC5883L_RANGES = {
88: HMC5883LRange.HMC5883L_RANGE_88_UT,
@@ -29,11 +50,21 @@ HMC5883L_RANGES = {
}
def validate_range(value):
value = cv.string(value)
if value.endswith(u'µT') or value.endswith('uT'):
value = value[:-2]
return cv.enum(HMC5883L_RANGES, int=True)(value)
def validate_enum(enum_values, units=None, int=True):
_units = []
if units is not None:
_units = units if isinstance(units, list) else [units]
_units = [text_type(x) for x in _units]
enum_bound = cv.enum(enum_values, int=int)
def validate_enum_bound(value):
value = cv.string(value)
for unit in _units:
if value.endswith(unit):
value = value[:-len(unit)]
break
return enum_bound(value)
return validate_enum_bound
field_strength_schema = sensor.sensor_schema(UNIT_MICROTESLA, ICON_MAGNET, 1)
@@ -42,19 +73,31 @@ heading_schema = sensor.sensor_schema(UNIT_DEGREES, ICON_SCREEN_ROTATION, 1)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(HMC5883LComponent),
cv.Optional(CONF_ADDRESS): cv.i2c_address,
cv.Optional(CONF_OVERSAMPLING, default='1x'): validate_enum(HMC5883LOversamplings, units="x"),
cv.Optional(CONF_RANGE, default=u'130µT'): validate_enum(HMC5883L_RANGES, units=["uT", u"µT"]),
cv.Optional(CONF_FIELD_STRENGTH_X): field_strength_schema,
cv.Optional(CONF_FIELD_STRENGTH_Y): field_strength_schema,
cv.Optional(CONF_FIELD_STRENGTH_Z): field_strength_schema,
cv.Optional(CONF_HEADING): heading_schema,
cv.Optional(CONF_RANGE, default='130uT'): validate_range,
}).extend(cv.polling_component_schema('60s')).extend(i2c.i2c_device_schema(0x1E))
def auto_data_rate(config):
interval_sec = config[CONF_UPDATE_INTERVAL].seconds
interval_hz = 1.0/interval_sec
for datarate in sorted(HMC5883LDatarates.keys()):
if float(datarate) >= interval_hz:
return HMC5883LDatarates[datarate]
return HMC5883LDatarates[75]
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config)
cg.add(var.set_oversampling(config[CONF_OVERSAMPLING]))
cg.add(var.set_datarate(auto_data_rate(config)))
cg.add(var.set_range(config[CONF_RANGE]))
if CONF_FIELD_STRENGTH_X in config:
sens = yield sensor.new_sensor(config[CONF_FIELD_STRENGTH_X])