2018-04-07 01:23:03 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
from esphomeyaml.automation import ACTION_REGISTRY, maybe_simple_id
|
2018-11-03 14:08:31 +01:00
|
|
|
from esphomeyaml.components import mqtt
|
2018-12-05 21:22:06 +01:00
|
|
|
from esphomeyaml.components.mqtt import setup_mqtt_component
|
2018-04-07 01:23:03 +02:00
|
|
|
import esphomeyaml.config_validation as cv
|
2018-12-05 21:22:06 +01:00
|
|
|
from esphomeyaml.const import CONF_ID, CONF_INTERNAL, CONF_MQTT_ID, CONF_NAME, CONF_OSCILLATING, \
|
|
|
|
CONF_OSCILLATION_COMMAND_TOPIC, CONF_OSCILLATION_OUTPUT, CONF_OSCILLATION_STATE_TOPIC, \
|
|
|
|
CONF_SPEED, CONF_SPEED_COMMAND_TOPIC, CONF_SPEED_STATE_TOPIC
|
2019-01-29 17:29:21 +01:00
|
|
|
from esphomeyaml.core import CORE
|
2018-12-05 21:22:06 +01:00
|
|
|
from esphomeyaml.cpp_generator import add, Pvariable, get_variable, templatable
|
|
|
|
from esphomeyaml.cpp_types import Application, Component, Nameable, esphomelib_ns, Action, bool_
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
fan_ns = esphomelib_ns.namespace('fan')
|
2018-11-12 23:30:31 +01:00
|
|
|
FanState = fan_ns.class_('FanState', Nameable, Component)
|
|
|
|
MQTTFanComponent = fan_ns.class_('MQTTFanComponent', mqtt.MQTTComponent)
|
|
|
|
MakeFan = Application.struct('MakeFan')
|
|
|
|
|
|
|
|
# Actions
|
|
|
|
TurnOnAction = fan_ns.class_('TurnOnAction', Action)
|
|
|
|
TurnOffAction = fan_ns.class_('TurnOffAction', Action)
|
|
|
|
ToggleAction = fan_ns.class_('ToggleAction', Action)
|
|
|
|
|
|
|
|
FanSpeed = fan_ns.enum('FanSpeed')
|
|
|
|
FAN_SPEED_OFF = FanSpeed.FAN_SPEED_OFF
|
|
|
|
FAN_SPEED_LOW = FanSpeed.FAN_SPEED_LOW
|
|
|
|
FAN_SPEED_MEDIUM = FanSpeed.FAN_SPEED_MEDIUM
|
|
|
|
FAN_SPEED_HIGH = FanSpeed.FAN_SPEED_HIGH
|
2018-05-21 17:01:47 +02:00
|
|
|
|
2018-06-02 22:22:20 +02:00
|
|
|
FAN_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
|
|
|
|
cv.GenerateID(): cv.declare_variable_id(FanState),
|
|
|
|
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTFanComponent),
|
2019-01-06 09:47:47 +01:00
|
|
|
vol.Optional(CONF_OSCILLATION_STATE_TOPIC): vol.All(cv.requires_component('mqtt'),
|
|
|
|
cv.publish_topic),
|
|
|
|
vol.Optional(CONF_OSCILLATION_COMMAND_TOPIC): vol.All(cv.requires_component('mqtt'),
|
|
|
|
cv.subscribe_topic),
|
2018-06-02 22:22:20 +02:00
|
|
|
})
|
|
|
|
|
2018-06-11 10:01:54 +02:00
|
|
|
FAN_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(FAN_SCHEMA.schema)
|
|
|
|
|
2018-05-21 17:01:47 +02:00
|
|
|
FAN_SPEEDS = {
|
|
|
|
'OFF': FAN_SPEED_OFF,
|
|
|
|
'LOW': FAN_SPEED_LOW,
|
|
|
|
'MEDIUM': FAN_SPEED_MEDIUM,
|
|
|
|
'HIGH': FAN_SPEED_HIGH,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-29 17:29:21 +01:00
|
|
|
def setup_fan_core_(fan_var, config):
|
2018-06-11 10:01:54 +02:00
|
|
|
if CONF_INTERNAL in config:
|
|
|
|
add(fan_var.set_internal(config[CONF_INTERNAL]))
|
|
|
|
|
2019-01-29 17:29:21 +01:00
|
|
|
mqtt_ = fan_var.Pget_mqtt()
|
2018-04-07 01:23:03 +02:00
|
|
|
if CONF_OSCILLATION_STATE_TOPIC in config:
|
2019-01-29 17:29:21 +01:00
|
|
|
add(mqtt_.set_custom_oscillation_state_topic(config[CONF_OSCILLATION_STATE_TOPIC]))
|
2018-04-07 01:23:03 +02:00
|
|
|
if CONF_OSCILLATION_COMMAND_TOPIC in config:
|
2019-01-29 17:29:21 +01:00
|
|
|
add(mqtt_.set_custom_oscillation_command_topic(config[CONF_OSCILLATION_COMMAND_TOPIC]))
|
2018-04-07 01:23:03 +02:00
|
|
|
if CONF_SPEED_STATE_TOPIC in config:
|
2019-01-29 17:29:21 +01:00
|
|
|
add(mqtt_.set_custom_speed_state_topic(config[CONF_SPEED_STATE_TOPIC]))
|
2018-04-07 01:23:03 +02:00
|
|
|
if CONF_SPEED_COMMAND_TOPIC in config:
|
2019-01-29 17:29:21 +01:00
|
|
|
add(mqtt_.set_custom_speed_command_topic(config[CONF_SPEED_COMMAND_TOPIC]))
|
|
|
|
setup_mqtt_component(mqtt_, config)
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
|
2019-01-29 17:29:21 +01:00
|
|
|
def setup_fan(fan_obj, config):
|
2018-06-02 22:22:20 +02:00
|
|
|
fan_var = Pvariable(config[CONF_ID], fan_obj, has_side_effects=False)
|
2019-01-29 17:29:21 +01:00
|
|
|
CORE.add_job(setup_fan_core_, fan_var, config)
|
2018-04-18 18:43:13 +02:00
|
|
|
|
|
|
|
|
2018-05-06 15:56:12 +02:00
|
|
|
BUILD_FLAGS = '-DUSE_FAN'
|
2018-10-16 23:16:06 +02:00
|
|
|
|
|
|
|
CONF_FAN_TOGGLE = 'fan.toggle'
|
|
|
|
FAN_TOGGLE_ACTION_SCHEMA = maybe_simple_id({
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(FanState),
|
2018-10-16 23:16:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_FAN_TOGGLE, FAN_TOGGLE_ACTION_SCHEMA)
|
2018-12-05 21:22:06 +01:00
|
|
|
def fan_toggle_to_code(config, action_id, arg_type, template_arg):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_toggle_action(template_arg)
|
|
|
|
type = ToggleAction.template(arg_type)
|
|
|
|
yield Pvariable(action_id, rhs, type=type)
|
|
|
|
|
|
|
|
|
|
|
|
CONF_FAN_TURN_OFF = 'fan.turn_off'
|
|
|
|
FAN_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(FanState),
|
2018-10-16 23:16:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_FAN_TURN_OFF, FAN_TURN_OFF_ACTION_SCHEMA)
|
2018-12-05 21:22:06 +01:00
|
|
|
def fan_turn_off_to_code(config, action_id, arg_type, template_arg):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_turn_off_action(template_arg)
|
|
|
|
type = TurnOffAction.template(arg_type)
|
|
|
|
yield Pvariable(action_id, rhs, type=type)
|
|
|
|
|
|
|
|
|
|
|
|
CONF_FAN_TURN_ON = 'fan.turn_on'
|
|
|
|
FAN_TURN_ON_ACTION_SCHEMA = maybe_simple_id({
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(FanState),
|
2018-10-16 23:16:06 +02:00
|
|
|
vol.Optional(CONF_OSCILLATING): cv.templatable(cv.boolean),
|
2018-11-23 13:51:22 +01:00
|
|
|
vol.Optional(CONF_SPEED): cv.templatable(cv.one_of(*FAN_SPEEDS, upper=True)),
|
2018-10-16 23:16:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_FAN_TURN_ON, FAN_TURN_ON_ACTION_SCHEMA)
|
2018-12-05 21:22:06 +01:00
|
|
|
def fan_turn_on_to_code(config, action_id, arg_type, template_arg):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_turn_on_action(template_arg)
|
|
|
|
type = TurnOnAction.template(arg_type)
|
|
|
|
action = Pvariable(action_id, rhs, type=type)
|
|
|
|
if CONF_OSCILLATING in config:
|
|
|
|
for template_ in templatable(config[CONF_OSCILLATING], arg_type, bool_):
|
|
|
|
yield None
|
|
|
|
add(action.set_oscillating(template_))
|
|
|
|
if CONF_SPEED in config:
|
|
|
|
for template_ in templatable(config[CONF_SPEED], arg_type, FanSpeed):
|
|
|
|
yield None
|
|
|
|
add(action.set_speed(template_))
|
|
|
|
yield action
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def core_to_hass_config(data, config):
|
|
|
|
ret = mqtt.build_hass_config(data, 'fan', config, include_state=True, include_command=True)
|
|
|
|
if ret is None:
|
|
|
|
return None
|
|
|
|
if CONF_OSCILLATION_OUTPUT in config:
|
|
|
|
default = mqtt.get_default_topic_for(data, 'fan', config[CONF_NAME], 'oscillation/state')
|
|
|
|
ret['oscillation_state_topic'] = config.get(CONF_OSCILLATION_STATE_TOPIC, default)
|
|
|
|
default = mqtt.get_default_topic_for(data, 'fan', config[CONF_NAME], 'oscillation/command')
|
|
|
|
ret['oscillation_command__topic'] = config.get(CONF_OSCILLATION_COMMAND_TOPIC, default)
|
|
|
|
return ret
|