esphome/esphomeyaml/components/sensor/mpu6050.py

72 lines
2.8 KiB
Python
Raw Normal View History

2018-05-06 15:56:12 +02:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
from esphomeyaml.components import sensor
2018-06-02 22:22:20 +02:00
from esphomeyaml.const import CONF_ADDRESS, CONF_ID, CONF_NAME, CONF_TEMPERATURE, \
2018-05-06 15:56:12 +02:00
CONF_UPDATE_INTERVAL
from esphomeyaml.helpers import App, Pvariable
DEPENDENCIES = ['i2c']
CONF_ACCEL_X = 'accel_x'
CONF_ACCEL_Y = 'accel_y'
CONF_ACCEL_Z = 'accel_z'
CONF_GYRO_X = 'gyro_x'
CONF_GYRO_Y = 'gyro_y'
CONF_GYRO_Z = 'gyro_z'
2018-06-02 22:22:20 +02:00
MPU6050Component = sensor.sensor_ns.MPU6050Component
MPU6050AccelSensor = sensor.sensor_ns.MPU6050AccelSensor
MPU6050GyroSensor = sensor.sensor_ns.MPU6050GyroSensor
MPU6050TemperatureSensor = sensor.sensor_ns.MPU6050TemperatureSensor
2018-05-20 12:41:52 +02:00
PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({
2018-06-02 22:22:20 +02:00
cv.GenerateID(): cv.declare_variable_id(MPU6050Component),
2018-05-06 15:56:12 +02:00
vol.Optional(CONF_ADDRESS, default=0x68): cv.i2c_address,
vol.Optional(CONF_ACCEL_X): cv.nameable(sensor.SENSOR_SCHEMA),
vol.Optional(CONF_ACCEL_Y): cv.nameable(sensor.SENSOR_SCHEMA),
vol.Optional(CONF_ACCEL_Z): cv.nameable(sensor.SENSOR_SCHEMA),
vol.Optional(CONF_GYRO_X): cv.nameable(sensor.SENSOR_SCHEMA),
vol.Optional(CONF_GYRO_Y): cv.nameable(sensor.SENSOR_SCHEMA),
vol.Optional(CONF_GYRO_Z): cv.nameable(sensor.SENSOR_SCHEMA),
vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA),
2018-08-24 22:44:15 +02:00
vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
2018-05-20 12:41:52 +02:00
}), cv.has_at_least_one_key(CONF_ACCEL_X, CONF_ACCEL_Y, CONF_ACCEL_Z,
CONF_GYRO_X, CONF_GYRO_Y, CONF_GYRO_Z))
2018-05-06 15:56:12 +02:00
def to_code(config):
rhs = App.make_mpu6050_sensor(config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL))
2018-06-02 22:22:20 +02:00
mpu = Pvariable(config[CONF_ID], rhs)
2018-05-06 15:56:12 +02:00
if CONF_ACCEL_X in config:
conf = config[CONF_ACCEL_X]
rhs = mpu.Pmake_accel_x_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
if CONF_ACCEL_Y in config:
conf = config[CONF_ACCEL_Y]
rhs = mpu.Pmake_accel_y_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
if CONF_ACCEL_Z in config:
conf = config[CONF_ACCEL_Z]
rhs = mpu.Pmake_accel_z_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
if CONF_GYRO_X in config:
conf = config[CONF_GYRO_X]
rhs = mpu.Pmake_gyro_x_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
if CONF_GYRO_Y in config:
conf = config[CONF_GYRO_Y]
rhs = mpu.Pmake_gyro_y_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
if CONF_GYRO_Z in config:
conf = config[CONF_GYRO_Z]
rhs = mpu.Pmake_gyro_z_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
if CONF_TEMPERATURE in config:
conf = config[CONF_TEMPERATURE]
rhs = mpu.Pmake_temperature_sensor(conf[CONF_NAME])
2018-06-03 07:11:11 +02:00
sensor.register_sensor(rhs, conf)
2018-05-06 15:56:12 +02:00
BUILD_FLAGS = '-DUSE_MPU6050'