From e72ef9e0619541f257af27bd80f3605676cff288 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sat, 5 Jan 2019 20:47:33 +0100 Subject: [PATCH] Add ESP32 Ethernet Support (#301) * Fix * Build flags * Fix --- esphomeyaml/__main__.py | 3 +- esphomeyaml/components/ethernet.py | 73 ++++++++++++++++++++++++++++++ esphomeyaml/config.py | 5 +- esphomeyaml/core.py | 8 +++- esphomeyaml/storage_json.py | 2 +- tests/test2.yaml | 17 +++++-- 6 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 esphomeyaml/components/ethernet.py diff --git a/esphomeyaml/__main__.py b/esphomeyaml/__main__.py index 7aa274e614..61eea83d40 100644 --- a/esphomeyaml/__main__.py +++ b/esphomeyaml/__main__.py @@ -23,7 +23,8 @@ from esphomeyaml.util import run_external_command, safe_print _LOGGER = logging.getLogger(__name__) -PRE_INITIALIZE = ['esphomeyaml', 'logger', 'wifi', 'ota', 'mqtt', 'web_server', 'api', 'i2c'] +PRE_INITIALIZE = ['esphomeyaml', 'logger', 'wifi', 'ethernet', 'ota', 'mqtt', 'web_server', 'api', + 'i2c'] def get_serial_ports(): diff --git a/esphomeyaml/components/ethernet.py b/esphomeyaml/components/ethernet.py new file mode 100644 index 0000000000..51ff308bbc --- /dev/null +++ b/esphomeyaml/components/ethernet.py @@ -0,0 +1,73 @@ +import voluptuous as vol + +from esphomeyaml import pins +from esphomeyaml.components import wifi +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_DOMAIN, CONF_HOSTNAME, CONF_ID, CONF_MANUAL_IP, CONF_TYPE, \ + ESP_PLATFORM_ESP32 +from esphomeyaml.cpp_generator import Pvariable, add +from esphomeyaml.cpp_helpers import gpio_output_pin_expression +from esphomeyaml.cpp_types import App, Component, esphomelib_ns, global_ns + +CONFLICTS_WITH = ['wifi'] +ESP_PLATFORMS = [ESP_PLATFORM_ESP32] + +CONF_PHY_ADDR = 'phy_addr' +CONF_MDC_PIN = 'mdc_pin' +CONF_MDIO_PIN = 'mdio_pin' +CONF_CLK_MODE = 'clk_mode' +CONF_POWER_PIN = 'power_pin' + +EthernetType = esphomelib_ns.enum('EthernetType') +ETHERNET_TYPES = { + 'LAN8720': EthernetType.ETHERNET_TYPE_LAN8720, + 'TLK110': EthernetType.ETHERNET_TYPE_TLK110, +} + +eth_clock_mode_t = global_ns.enum('eth_clock_mode_t') +CLK_MODES = { + 'GPIO0_IN': eth_clock_mode_t.ETH_CLOCK_GPIO0_IN, + 'GPIO0_OUT': eth_clock_mode_t.ETH_CLOCK_GPIO0_OUT, + 'GPIO16_OUT': eth_clock_mode_t.ETH_CLOCK_GPIO16_OUT, + 'GPIO17_OUT': eth_clock_mode_t.ETH_CLOCK_GPIO17_OUT, +} + +EthernetComponent = esphomelib_ns.class_('EthernetComponent', Component) + +CONFIG_SCHEMA = vol.Schema({ + cv.GenerateID(): cv.declare_variable_id(EthernetComponent), + vol.Required(CONF_TYPE): cv.one_of(*ETHERNET_TYPES, upper=True), + vol.Required(CONF_MDC_PIN): pins.output_pin, + vol.Required(CONF_MDIO_PIN): pins.input_output_pin, + vol.Optional(CONF_CLK_MODE, default='GPIO0_IN'): cv.one_of(*CLK_MODES, upper=True, space='_'), + vol.Optional(CONF_PHY_ADDR, default=0): vol.All(cv.int_, vol.Range(min=0, max=31)), + vol.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema, + vol.Optional(CONF_MANUAL_IP): wifi.STA_MANUAL_IP_SCHEMA, + vol.Optional(CONF_HOSTNAME): cv.hostname, + vol.Optional(CONF_DOMAIN, default='.local'): cv.domain_name, +}) + + +def to_code(config): + rhs = App.init_ethernet() + eth = Pvariable(config[CONF_ID], rhs) + + add(eth.set_phy_addr(config[CONF_PHY_ADDR])) + add(eth.set_mdc_pin(config[CONF_MDC_PIN])) + add(eth.set_mdio_pin(config[CONF_MDIO_PIN])) + add(eth.set_type(ETHERNET_TYPES[config[CONF_TYPE]])) + add(eth.set_clk_mode(CLK_MODES[config[CONF_CLK_MODE]])) + + if CONF_POWER_PIN in config: + for pin in gpio_output_pin_expression(config[CONF_POWER_PIN]): + yield + add(eth.set_power_pin(pin)) + + if CONF_HOSTNAME in config: + add(eth.set_hostname(config[CONF_HOSTNAME])) + + if CONF_MANUAL_IP in config: + add(eth.set_manual_ip(wifi.manual_ip(config[CONF_MANUAL_IP]))) + + +REQUIRED_BUILD_FLAGS = '-DUSE_ETHERNET' diff --git a/esphomeyaml/config.py b/esphomeyaml/config.py index 0402a4d66f..bac42aaf63 100644 --- a/esphomeyaml/config.py +++ b/esphomeyaml/config.py @@ -79,7 +79,6 @@ def _path_begins_with_(path, other): # type: (ConfigPath, ConfigPath) -> bool def _path_begins_with(path, other): # type: (ConfigPath, ConfigPath) -> bool ret = _path_begins_with_(path, other) - # print('_path_begins_with({}, {}) -> {}'.format(path, other, ret)) return ret @@ -262,7 +261,7 @@ def validate_config(config): success = True conflicts_with = getattr(component, 'CONFLICTS_WITH', []) for conflict in conflicts_with: - if conflict not in config: + if conflict in config: result.add_error(u"Component {} cannot be used together with component {}" u"".format(domain, conflict), [domain]) success = False @@ -317,7 +316,7 @@ def validate_config(config): success = True conflicts_with = getattr(platform, 'CONFLICTS_WITH', []) for conflict in conflicts_with: - if conflict not in config: + if conflict in config: result.add_error(u"Platform {} cannot be used together with component {}" u"".format(p_domain, conflict), [domain, i]) success = False diff --git a/esphomeyaml/core.py b/esphomeyaml/core.py index 7518f62802..bc2cee62fe 100644 --- a/esphomeyaml/core.py +++ b/esphomeyaml/core.py @@ -315,7 +315,13 @@ class EsphomeyamlCore(object): def address(self): # type: () -> str from esphomeyaml.components import wifi - return wifi.get_upload_host(self.config[CONF_WIFI]) + if 'wifi' in self.config: + return wifi.get_upload_host(self.config[CONF_WIFI]) + + if 'ethernet' in self.config: + return wifi.get_upload_host(self.config['ethernet']) + + return None @property def esphomelib_version(self): # type: () -> Dict[str, str] diff --git a/esphomeyaml/storage_json.py b/esphomeyaml/storage_json.py index 82caafc490..d693b93950 100644 --- a/esphomeyaml/storage_json.py +++ b/esphomeyaml/storage_json.py @@ -291,6 +291,6 @@ class CheckForUpdateThread(threading.Thread): def start_update_check_thread(path): # dummy call to strptime as python 2.7 has a bug with strptime when importing from threads datetime.strptime('20180101', '%Y%m%d') - thread = CheckForUpdateThread(path) + thread = CheckForUpdateThread(os.path.abspath(path)) thread.start() return thread diff --git a/tests/test2.yaml b/tests/test2.yaml index 4619008b73..f2b8beaec0 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -11,10 +11,19 @@ esphomeyaml: substitutions: devicename: test2 -wifi: - ssid: 'MySSID' - password: 'password1' - reboot_timeout: 120s +ethernet: + type: LAN8720 + mdc_pin: GPIO23 + mdio_pin: GPIO24 + clk_mode: GPIO0_IN + phy_addr: 0 + power_pin: GPIO25 + manual_ip: + static_ip: 192.168.178.56 + gateway: 192.168.178.1 + subnet: 255.255.255.0 + hostname: helloworld + domain: .local mqtt: broker: '192.168.178.84'