mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-29 22:24:26 +00:00 
			
		
		
		
	Let esphomeyaml know about class inheritance (#229)
* Allow overriding setup priority * Add inheritance tree * Global variables * Tests and better validation * Fix * Lint
This commit is contained in:
		| @@ -6,21 +6,21 @@ import esphomeyaml.config_validation as cv | ||||
| from esphomeyaml.const import CONF_ICON, CONF_ID, CONF_INVERTED, CONF_MQTT_ID, CONF_INTERNAL, \ | ||||
|     CONF_OPTIMISTIC | ||||
| from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, setup_mqtt_component, \ | ||||
|     TemplateArguments, get_variable | ||||
|     TemplateArguments, get_variable, Nameable, Action | ||||
|  | ||||
| PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ | ||||
|  | ||||
| }) | ||||
|  | ||||
| switch_ns = esphomelib_ns.namespace('switch_') | ||||
| Switch = switch_ns.Switch | ||||
| MQTTSwitchComponent = switch_ns.MQTTSwitchComponent | ||||
| ToggleAction = switch_ns.ToggleAction | ||||
| TurnOffAction = switch_ns.TurnOffAction | ||||
| TurnOnAction = switch_ns.TurnOnAction | ||||
| Switch = switch_ns.class_('Switch', Nameable) | ||||
| MQTTSwitchComponent = switch_ns.class_('MQTTSwitchComponent', mqtt.MQTTComponent) | ||||
|  | ||||
| ToggleAction = switch_ns.class_('ToggleAction', Action) | ||||
| TurnOffAction = switch_ns.class_('TurnOffAction', Action) | ||||
| TurnOnAction = switch_ns.class_('TurnOnAction', Action) | ||||
|  | ||||
| SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ | ||||
|     cv.GenerateID(): cv.declare_variable_id(Switch), | ||||
|     cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTSwitchComponent), | ||||
|     vol.Optional(CONF_ICON): cv.icon, | ||||
|     vol.Optional(CONF_INVERTED): cv.boolean, | ||||
| @@ -58,7 +58,7 @@ BUILD_FLAGS = '-DUSE_SWITCH' | ||||
|  | ||||
| CONF_SWITCH_TOGGLE = 'switch.toggle' | ||||
| SWITCH_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ | ||||
|     vol.Required(CONF_ID): cv.use_variable_id(None), | ||||
|     vol.Required(CONF_ID): cv.use_variable_id(Switch), | ||||
| }) | ||||
|  | ||||
|  | ||||
| @@ -74,7 +74,7 @@ def switch_toggle_to_code(config, action_id, arg_type): | ||||
|  | ||||
| CONF_SWITCH_TURN_OFF = 'switch.turn_off' | ||||
| SWITCH_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ | ||||
|     vol.Required(CONF_ID): cv.use_variable_id(None), | ||||
|     vol.Required(CONF_ID): cv.use_variable_id(Switch), | ||||
| }) | ||||
|  | ||||
|  | ||||
| @@ -90,7 +90,7 @@ def switch_turn_off_to_code(config, action_id, arg_type): | ||||
|  | ||||
| CONF_SWITCH_TURN_ON = 'switch.turn_on' | ||||
| SWITCH_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ | ||||
|     vol.Required(CONF_ID): cv.use_variable_id(None), | ||||
|     vol.Required(CONF_ID): cv.use_variable_id(Switch), | ||||
| }) | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -4,26 +4,28 @@ from esphomeyaml import pins | ||||
| from esphomeyaml.components import switch | ||||
| import esphomeyaml.config_validation as cv | ||||
| from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_PIN | ||||
| from esphomeyaml.helpers import App, Application, gpio_output_pin_expression, variable | ||||
| from esphomeyaml.helpers import App, Application, gpio_output_pin_expression, variable, \ | ||||
|     setup_component, Component | ||||
|  | ||||
| MakeGPIOSwitch = Application.MakeGPIOSwitch | ||||
| GPIOSwitch = switch.switch_ns.GPIOSwitch | ||||
| MakeGPIOSwitch = Application.struct('MakeGPIOSwitch') | ||||
| GPIOSwitch = switch.switch_ns.class_('GPIOSwitch', switch.Switch, Component) | ||||
|  | ||||
| PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|     cv.GenerateID(): cv.declare_variable_id(GPIOSwitch), | ||||
|     cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeGPIOSwitch), | ||||
|     vol.Required(CONF_PIN): pins.gpio_output_pin_schema, | ||||
| })) | ||||
| }).extend(cv.COMPONENT_SCHEMA.schema)) | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|     pin = None | ||||
|     for pin in gpio_output_pin_expression(config[CONF_PIN]): | ||||
|         yield | ||||
|     rhs = App.make_gpio_switch(config[CONF_NAME], pin) | ||||
|     gpio = variable(config[CONF_MAKE_ID], rhs) | ||||
|     make = variable(config[CONF_MAKE_ID], rhs) | ||||
|     gpio = make.Pswitch_ | ||||
|  | ||||
|     switch.setup_switch(gpio.Pswitch_, gpio.Pmqtt, config) | ||||
|     switch.setup_switch(gpio, make.Pmqtt, config) | ||||
|     setup_component(gpio, config) | ||||
|  | ||||
|  | ||||
| BUILD_FLAGS = '-DUSE_GPIO_SWITCH' | ||||
|   | ||||
| @@ -1,27 +1,29 @@ | ||||
| import voluptuous as vol | ||||
|  | ||||
| from esphomeyaml.components import output, switch | ||||
| import esphomeyaml.config_validation as cv | ||||
| from esphomeyaml.components import switch | ||||
| from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_OUTPUT | ||||
| from esphomeyaml.helpers import App, Application, get_variable, variable | ||||
| from esphomeyaml.helpers import App, Application, Component, get_variable, setup_component, variable | ||||
|  | ||||
| MakeOutputSwitch = Application.MakeOutputSwitch | ||||
| OutputSwitch = switch.switch_ns.OutputSwitch | ||||
| MakeOutputSwitch = Application.struct('MakeOutputSwitch') | ||||
| OutputSwitch = switch.switch_ns.class_('OutputSwitch', switch.Switch, Component) | ||||
|  | ||||
| PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|     cv.GenerateID(): cv.declare_variable_id(OutputSwitch), | ||||
|     cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeOutputSwitch), | ||||
|     vol.Required(CONF_OUTPUT): cv.use_variable_id(None), | ||||
| })) | ||||
|     vol.Required(CONF_OUTPUT): cv.use_variable_id(output.BinaryOutput), | ||||
| }).extend(cv.COMPONENT_SCHEMA.schema)) | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|     output = None | ||||
|     for output in get_variable(config[CONF_OUTPUT]): | ||||
|     for output_ in get_variable(config[CONF_OUTPUT]): | ||||
|         yield | ||||
|     rhs = App.make_output_switch(config[CONF_NAME], output) | ||||
|     gpio = variable(config[CONF_MAKE_ID], rhs) | ||||
|     switch.setup_switch(gpio.Pswitch_, gpio.Pmqtt, config) | ||||
|     rhs = App.make_output_switch(config[CONF_NAME], output_) | ||||
|     make = variable(config[CONF_MAKE_ID], rhs) | ||||
|     switch_ = make.Pswitch_ | ||||
|  | ||||
|     switch.setup_switch(switch_, make.Pmqtt, config) | ||||
|     setup_component(switch, config) | ||||
|  | ||||
|  | ||||
| BUILD_FLAGS = '-DUSE_OUTPUT_SWITCH' | ||||
|   | ||||
| @@ -1,18 +1,18 @@ | ||||
| import voluptuous as vol | ||||
|  | ||||
| import esphomeyaml.config_validation as cv | ||||
| from esphomeyaml.components import switch | ||||
| from esphomeyaml.components.remote_transmitter import RC_SWITCH_RAW_SCHEMA, \ | ||||
|     RC_SWITCH_TYPE_A_SCHEMA, RC_SWITCH_TYPE_B_SCHEMA, RC_SWITCH_TYPE_C_SCHEMA, \ | ||||
|     RC_SWITCH_TYPE_D_SCHEMA, RemoteTransmitterComponent, binary_code, build_rc_switch_protocol, \ | ||||
|     remote_ns | ||||
| import esphomeyaml.config_validation as cv | ||||
| from esphomeyaml.const import CONF_ADDRESS, CONF_CARRIER_FREQUENCY, CONF_CHANNEL, CONF_CODE, \ | ||||
|     CONF_COMMAND, CONF_DATA, CONF_DEVICE, CONF_FAMILY, CONF_GROUP, CONF_INVERTED, CONF_LG, \ | ||||
|     CONF_NAME, CONF_NBITS, CONF_NEC, CONF_PANASONIC, CONF_PROTOCOL, CONF_RAW, CONF_RC_SWITCH_RAW, \ | ||||
|     CONF_RC_SWITCH_TYPE_A, CONF_RC_SWITCH_TYPE_B, CONF_RC_SWITCH_TYPE_C, CONF_RC_SWITCH_TYPE_D, \ | ||||
|     CONF_REPEAT, CONF_SAMSUNG, CONF_SONY, CONF_STATE, CONF_TIMES, \ | ||||
|     CONF_WAIT_TIME | ||||
| from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, add, get_variable | ||||
| from esphomeyaml.helpers import ArrayInitializer, Pvariable, add, get_variable | ||||
|  | ||||
| DEPENDENCIES = ['remote_transmitter'] | ||||
|  | ||||
| @@ -23,18 +23,18 @@ REMOTE_KEYS = [CONF_NEC, CONF_LG, CONF_SAMSUNG, CONF_SONY, CONF_PANASONIC, CONF_ | ||||
| CONF_REMOTE_TRANSMITTER_ID = 'remote_transmitter_id' | ||||
| CONF_TRANSMITTER_ID = 'transmitter_id' | ||||
|  | ||||
| RemoteTransmitter = remote_ns.RemoteTransmitter | ||||
| LGTransmitter = remote_ns.LGTransmitter | ||||
| NECTransmitter = remote_ns.NECTransmitter | ||||
| PanasonicTransmitter = remote_ns.PanasonicTransmitter | ||||
| RawTransmitter = remote_ns.RawTransmitter | ||||
| SamsungTransmitter = remote_ns.SamsungTransmitter | ||||
| SonyTransmitter = remote_ns.SonyTransmitter | ||||
| RCSwitchRawTransmitter = remote_ns.RCSwitchRawTransmitter | ||||
| RCSwitchTypeATransmitter = remote_ns.RCSwitchTypeATransmitter | ||||
| RCSwitchTypeBTransmitter = remote_ns.RCSwitchTypeBTransmitter | ||||
| RCSwitchTypeCTransmitter = remote_ns.RCSwitchTypeCTransmitter | ||||
| RCSwitchTypeDTransmitter = remote_ns.RCSwitchTypeDTransmitter | ||||
| RemoteTransmitter = remote_ns.class_('RemoteTransmitter', switch.Switch) | ||||
| LGTransmitter = remote_ns.class_('LGTransmitter', RemoteTransmitter) | ||||
| NECTransmitter = remote_ns.class_('NECTransmitter', RemoteTransmitter) | ||||
| PanasonicTransmitter = remote_ns.class_('PanasonicTransmitter', RemoteTransmitter) | ||||
| RawTransmitter = remote_ns.class_('RawTransmitter', RemoteTransmitter) | ||||
| SamsungTransmitter = remote_ns.class_('SamsungTransmitter', RemoteTransmitter) | ||||
| SonyTransmitter = remote_ns.class_('SonyTransmitter', RemoteTransmitter) | ||||
| RCSwitchRawTransmitter = remote_ns.class_('RCSwitchRawTransmitter', RemoteTransmitter) | ||||
| RCSwitchTypeATransmitter = remote_ns.class_('RCSwitchTypeATransmitter', RCSwitchRawTransmitter) | ||||
| RCSwitchTypeBTransmitter = remote_ns.class_('RCSwitchTypeBTransmitter', RCSwitchRawTransmitter) | ||||
| RCSwitchTypeCTransmitter = remote_ns.class_('RCSwitchTypeCTransmitter', RCSwitchRawTransmitter) | ||||
| RCSwitchTypeDTransmitter = remote_ns.class_('RCSwitchTypeDTransmitter', RCSwitchRawTransmitter) | ||||
|  | ||||
| validate_raw_data = [vol.Any(vol.Coerce(int), cv.time_period_microseconds)] | ||||
|  | ||||
| @@ -128,7 +128,6 @@ def transmitter_base(full_config): | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|     remote = None | ||||
|     for remote in get_variable(config[CONF_REMOTE_TRANSMITTER_ID]): | ||||
|         yield | ||||
|     rhs = transmitter_base(config) | ||||
|   | ||||
| @@ -5,8 +5,8 @@ from esphomeyaml.components import switch | ||||
| from esphomeyaml.const import CONF_INVERTED, CONF_MAKE_ID, CONF_NAME | ||||
| from esphomeyaml.helpers import App, Application, variable | ||||
|  | ||||
| MakeRestartSwitch = Application.MakeRestartSwitch | ||||
| RestartSwitch = switch.switch_ns.RestartSwitch | ||||
| MakeRestartSwitch = Application.struct('MakeRestartSwitch') | ||||
| RestartSwitch = switch.switch_ns.class_('RestartSwitch', switch.Switch) | ||||
|  | ||||
| PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|     cv.GenerateID(): cv.declare_variable_id(RestartSwitch), | ||||
|   | ||||
| @@ -5,8 +5,8 @@ from esphomeyaml.components import switch | ||||
| from esphomeyaml.const import CONF_INVERTED, CONF_MAKE_ID, CONF_NAME | ||||
| from esphomeyaml.helpers import App, Application, variable | ||||
|  | ||||
| MakeShutdownSwitch = Application.MakeShutdownSwitch | ||||
| ShutdownSwitch = switch.switch_ns.ShutdownSwitch | ||||
| MakeShutdownSwitch = Application.struct('MakeShutdownSwitch') | ||||
| ShutdownSwitch = switch.switch_ns.class_('ShutdownSwitch', switch.Switch) | ||||
|  | ||||
| PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|     cv.GenerateID(): cv.declare_variable_id(ShutdownSwitch), | ||||
|   | ||||
| @@ -6,10 +6,10 @@ from esphomeyaml.components import switch | ||||
| from esphomeyaml.const import CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME, CONF_TURN_OFF_ACTION, \ | ||||
|     CONF_TURN_ON_ACTION, CONF_OPTIMISTIC, CONF_RESTORE_STATE | ||||
| from esphomeyaml.helpers import App, Application, process_lambda, variable, NoArg, add, bool_, \ | ||||
|     optional | ||||
|     optional, setup_component, Component | ||||
|  | ||||
| MakeTemplateSwitch = Application.MakeTemplateSwitch | ||||
| TemplateSwitch = switch.switch_ns.TemplateSwitch | ||||
| MakeTemplateSwitch = Application.struct('MakeTemplateSwitch') | ||||
| TemplateSwitch = switch.switch_ns.class_('TemplateSwitch', switch.Switch, Component) | ||||
|  | ||||
| PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|     cv.GenerateID(): cv.declare_variable_id(TemplateSwitch), | ||||
| @@ -19,32 +19,34 @@ PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|     vol.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(single=True), | ||||
|     vol.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(single=True), | ||||
|     vol.Optional(CONF_RESTORE_STATE): cv.boolean, | ||||
| }), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC)) | ||||
| }).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC)) | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|     rhs = App.make_template_switch(config[CONF_NAME]) | ||||
|     make = variable(config[CONF_MAKE_ID], rhs) | ||||
|     template = make.Ptemplate_ | ||||
|  | ||||
|     switch.setup_switch(make.Ptemplate_, make.Pmqtt, config) | ||||
|     switch.setup_switch(template, make.Pmqtt, config) | ||||
|  | ||||
|     if CONF_LAMBDA in config: | ||||
|         template_ = None | ||||
|         for template_ in process_lambda(config[CONF_LAMBDA], [], | ||||
|                                         return_type=optional.template(bool_)): | ||||
|             yield | ||||
|         add(make.Ptemplate_.set_state_lambda(template_)) | ||||
|         add(template.set_state_lambda(template_)) | ||||
|     if CONF_TURN_OFF_ACTION in config: | ||||
|         automation.build_automation(make.Ptemplate_.get_turn_off_trigger(), NoArg, | ||||
|         automation.build_automation(template.get_turn_off_trigger(), NoArg, | ||||
|                                     config[CONF_TURN_OFF_ACTION]) | ||||
|     if CONF_TURN_ON_ACTION in config: | ||||
|         automation.build_automation(make.Ptemplate_.get_turn_on_trigger(), NoArg, | ||||
|         automation.build_automation(template.get_turn_on_trigger(), NoArg, | ||||
|                                     config[CONF_TURN_ON_ACTION]) | ||||
|     if CONF_OPTIMISTIC in config: | ||||
|         add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) | ||||
|         add(template.set_optimistic(config[CONF_OPTIMISTIC])) | ||||
|  | ||||
|     if CONF_RESTORE_STATE in config: | ||||
|         add(make.Ptemplate_.set_restore_state(config[CONF_RESTORE_STATE])) | ||||
|         add(template.set_restore_state(config[CONF_RESTORE_STATE])) | ||||
|  | ||||
|     setup_component(template, config) | ||||
|  | ||||
|  | ||||
| BUILD_FLAGS = '-DUSE_TEMPLATE_SWITCH' | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| import voluptuous as vol | ||||
|  | ||||
| import esphomeyaml.config_validation as cv | ||||
| from esphomeyaml.components import switch | ||||
| from esphomeyaml.components import switch, uart | ||||
| from esphomeyaml.components.uart import UARTComponent | ||||
| from esphomeyaml.const import CONF_DATA, CONF_INVERTED, CONF_MAKE_ID, CONF_NAME, CONF_UART_ID | ||||
| from esphomeyaml.core import HexInt | ||||
| @@ -9,8 +9,8 @@ from esphomeyaml.helpers import App, Application, ArrayInitializer, get_variable | ||||
|  | ||||
| DEPENDENCIES = ['uart'] | ||||
|  | ||||
| MakeUARTSwitch = Application.MakeUARTSwitch | ||||
| UARTSwitch = switch.switch_ns.UARTSwitch | ||||
| MakeUARTSwitch = Application.struct('MakeUARTSwitch') | ||||
| UARTSwitch = switch.switch_ns.class_('UARTSwitch', switch.Switch, uart.UARTDevice) | ||||
|  | ||||
|  | ||||
| def validate_data(value): | ||||
| @@ -33,13 +33,12 @@ PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|     uart = None | ||||
|     for uart in get_variable(config[CONF_UART_ID]): | ||||
|     for uart_ in get_variable(config[CONF_UART_ID]): | ||||
|         yield | ||||
|     data = config[CONF_DATA] | ||||
|     if isinstance(data, str): | ||||
|         data = [HexInt(ord(x)) for x in data] | ||||
|     rhs = App.make_uart_switch(uart, config[CONF_NAME], ArrayInitializer(*data, multiline=False)) | ||||
|     rhs = App.make_uart_switch(uart_, config[CONF_NAME], ArrayInitializer(*data, multiline=False)) | ||||
|     restart = variable(config[CONF_MAKE_ID], rhs) | ||||
|     switch.setup_switch(restart.Puart, restart.Pmqtt, config) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user