esphome/esphomeyaml/components/sensor/mpu6050.py

102 lines
4.1 KiB
Python
Raw Normal View History

2018-05-06 15:56:12 +02:00
import voluptuous as vol
from esphomeyaml.components import i2c, sensor
2018-05-06 15:56:12 +02:00
import esphomeyaml.config_validation as cv
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
2019-01-06 19:38:23 +01:00
from esphomeyaml.cpp_generator import Pvariable
from esphomeyaml.cpp_helpers import setup_component
from esphomeyaml.cpp_types import App, PollingComponent
2018-05-06 15:56:12 +02:00
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'
MPU6050Component = sensor.sensor_ns.class_('MPU6050Component', PollingComponent, i2c.I2CDevice)
MPU6050AccelSensor = sensor.sensor_ns.class_('MPU6050AccelSensor', sensor.EmptyPollingParentSensor)
MPU6050GyroSensor = sensor.sensor_ns.class_('MPU6050GyroSensor', sensor.EmptyPollingParentSensor)
MPU6050TemperatureSensor = sensor.sensor_ns.class_('MPU6050TemperatureSensor',
sensor.EmptyPollingParentSensor)
SENSOR_KEYS = [CONF_ACCEL_X, CONF_ACCEL_Y, CONF_ACCEL_Z,
CONF_GYRO_X, CONF_GYRO_Y, CONF_GYRO_Z]
2018-06-02 22:22:20 +02:00
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.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050AccelSensor),
})),
vol.Optional(CONF_ACCEL_Y): cv.nameable(sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050AccelSensor),
})),
vol.Optional(CONF_ACCEL_Z): cv.nameable(sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050AccelSensor),
})),
vol.Optional(CONF_GYRO_X): cv.nameable(sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050GyroSensor),
})),
vol.Optional(CONF_GYRO_Y): cv.nameable(sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050GyroSensor),
})),
vol.Optional(CONF_GYRO_Z): cv.nameable(sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050GyroSensor),
})),
vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MPU6050TemperatureSensor),
})),
2018-08-24 22:44:15 +02:00
vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSOR_KEYS))
2018-05-20 12:41:52 +02:00
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
setup_component(mpu, config)
2018-05-06 15:56:12 +02:00
BUILD_FLAGS = '-DUSE_MPU6050'
def to_hass_config(data, config):
ret = []
for key in (CONF_ACCEL_X, CONF_ACCEL_Y, CONF_ACCEL_Z, CONF_GYRO_X, CONF_GYRO_Y, CONF_GYRO_Z,
CONF_TEMPERATURE):
if key in config:
ret.append(sensor.core_to_hass_config(data, config[key]))
return ret