2018-10-20 12:58:52 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
from esphomeyaml.components import sensor, uart
|
2018-10-20 12:58:52 +02:00
|
|
|
from esphomeyaml.components.uart import UARTComponent
|
|
|
|
import esphomeyaml.config_validation as cv
|
|
|
|
from esphomeyaml.const import CONF_FORMALDEHYDE, CONF_HUMIDITY, CONF_ID, CONF_NAME, CONF_PM_10_0, \
|
|
|
|
CONF_PM_1_0, CONF_PM_2_5, CONF_TEMPERATURE, CONF_TYPE, CONF_UART_ID
|
2018-11-12 23:30:31 +01:00
|
|
|
from esphomeyaml.helpers import App, Pvariable, get_variable, setup_component, Component
|
2018-10-20 12:58:52 +02:00
|
|
|
|
|
|
|
DEPENDENCIES = ['uart']
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
PMSX003Component = sensor.sensor_ns.class_('PMSX003Component', uart.UARTDevice, Component)
|
|
|
|
PMSX003Sensor = sensor.sensor_ns.class_('PMSX003Sensor', sensor.Sensor)
|
2018-10-20 12:58:52 +02:00
|
|
|
|
|
|
|
CONF_PMSX003 = 'PMSX003'
|
|
|
|
CONF_PMS5003T = 'PMS5003T'
|
|
|
|
CONF_PMS5003ST = 'PMS5003ST'
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
PMSX003Type = sensor.sensor_ns.enum('PMSX003Type')
|
2018-10-20 12:58:52 +02:00
|
|
|
PMSX003_TYPES = {
|
2018-11-12 23:30:31 +01:00
|
|
|
CONF_PMSX003: PMSX003Type.PMSX003_TYPE_X003,
|
|
|
|
CONF_PMS5003T: PMSX003Type.PMSX003_TYPE_5003T,
|
|
|
|
CONF_PMS5003ST: PMSX003Type.PMSX003_TYPE_5003ST,
|
2018-10-20 12:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SENSORS_TO_TYPE = {
|
|
|
|
CONF_PM_1_0: [CONF_PMSX003],
|
|
|
|
CONF_PM_2_5: [CONF_PMSX003, CONF_PMS5003T, CONF_PMS5003ST],
|
|
|
|
CONF_PM_10_0: [CONF_PMSX003],
|
|
|
|
CONF_TEMPERATURE: [CONF_PMS5003T, CONF_PMS5003ST],
|
|
|
|
CONF_HUMIDITY: [CONF_PMS5003T, CONF_PMS5003ST],
|
|
|
|
CONF_FORMALDEHYDE: [CONF_PMS5003ST],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def validate_pmsx003_sensors(value):
|
|
|
|
for key, types in SENSORS_TO_TYPE.iteritems():
|
|
|
|
if key in value and value[CONF_TYPE] not in types:
|
|
|
|
raise vol.Invalid(u"{} does not have {} sensor!".format(value[CONF_TYPE], key))
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
PMSX003_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({
|
|
|
|
cv.GenerateID(): cv.declare_variable_id(PMSX003Sensor),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-10-20 12:58:52 +02:00
|
|
|
PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({
|
|
|
|
cv.GenerateID(): cv.declare_variable_id(PMSX003Component),
|
|
|
|
cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent),
|
2018-11-23 13:51:22 +01:00
|
|
|
vol.Required(CONF_TYPE): cv.one_of(*PMSX003_TYPES, upper=True),
|
2018-10-20 12:58:52 +02:00
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Optional(CONF_PM_1_0): cv.nameable(PMSX003_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_PM_2_5): cv.nameable(PMSX003_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_PM_10_0): cv.nameable(PMSX003_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_TEMPERATURE): cv.nameable(PMSX003_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_HUMIDITY): cv.nameable(PMSX003_SENSOR_SCHEMA),
|
|
|
|
vol.Optional(CONF_FORMALDEHYDE): cv.nameable(PMSX003_SENSOR_SCHEMA),
|
|
|
|
}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSORS_TO_TYPE))
|
2018-10-20 12:58:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def to_code(config):
|
2018-11-12 23:30:31 +01:00
|
|
|
for uart_ in get_variable(config[CONF_UART_ID]):
|
2018-10-20 12:58:52 +02:00
|
|
|
yield
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
rhs = App.make_pmsx003(uart_, PMSX003_TYPES[config[CONF_TYPE]])
|
2018-10-20 12:58:52 +02:00
|
|
|
pms = Pvariable(config[CONF_ID], rhs)
|
|
|
|
|
|
|
|
if CONF_PM_1_0 in config:
|
|
|
|
conf = config[CONF_PM_1_0]
|
|
|
|
sensor.register_sensor(pms.make_pm_1_0_sensor(conf[CONF_NAME]), conf)
|
|
|
|
|
|
|
|
if CONF_PM_2_5 in config:
|
|
|
|
conf = config[CONF_PM_2_5]
|
|
|
|
sensor.register_sensor(pms.make_pm_2_5_sensor(conf[CONF_NAME]), conf)
|
|
|
|
|
|
|
|
if CONF_PM_10_0 in config:
|
|
|
|
conf = config[CONF_PM_10_0]
|
|
|
|
sensor.register_sensor(pms.make_pm_10_0_sensor(conf[CONF_NAME]), conf)
|
|
|
|
|
|
|
|
if CONF_TEMPERATURE in config:
|
|
|
|
conf = config[CONF_TEMPERATURE]
|
|
|
|
sensor.register_sensor(pms.make_temperature_sensor(conf[CONF_NAME]), conf)
|
|
|
|
|
|
|
|
if CONF_HUMIDITY in config:
|
|
|
|
conf = config[CONF_HUMIDITY]
|
|
|
|
sensor.register_sensor(pms.make_humidity_sensor(conf[CONF_NAME]), conf)
|
|
|
|
|
|
|
|
if CONF_FORMALDEHYDE in config:
|
|
|
|
conf = config[CONF_FORMALDEHYDE]
|
|
|
|
sensor.register_sensor(pms.make_formaldehyde_sensor(conf[CONF_NAME]), conf)
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
setup_component(pms, config)
|
|
|
|
|
2018-10-20 12:58:52 +02:00
|
|
|
|
|
|
|
BUILD_FLAGS = '-DUSE_PMSX003'
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def to_hass_config(data, config):
|
|
|
|
ret = []
|
|
|
|
for key in (CONF_PM_1_0, CONF_PM_2_5, CONF_PM_10_0, CONF_TEMPERATURE, CONF_HUMIDITY,
|
|
|
|
CONF_FORMALDEHYDE):
|
|
|
|
if key in config:
|
|
|
|
ret.append(sensor.core_to_hass_config(data, config[key]))
|
|
|
|
return ret
|