Add use_address (#417)

This commit is contained in:
Otto Winter 2019-02-10 23:55:13 +01:00 committed by GitHub
parent 4b7a41922c
commit feab956ea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 24 deletions

View file

@ -3,8 +3,9 @@ import voluptuous as vol
from esphomeyaml import pins from esphomeyaml import pins
from esphomeyaml.components import wifi from esphomeyaml.components import wifi
import esphomeyaml.config_validation as cv import esphomeyaml.config_validation as cv
from esphomeyaml.const import CONF_DOMAIN, CONF_ID, CONF_MANUAL_IP, CONF_TYPE, \ from esphomeyaml.const import CONF_DOMAIN, CONF_ID, CONF_MANUAL_IP, CONF_TYPE, ESP_PLATFORM_ESP32, \
ESP_PLATFORM_ESP32 CONF_USE_ADDRESS, CONF_STATIC_IP
from esphomeyaml.core import CORE
from esphomeyaml.cpp_generator import Pvariable, add from esphomeyaml.cpp_generator import Pvariable, add
from esphomeyaml.cpp_helpers import gpio_output_pin_expression from esphomeyaml.cpp_helpers import gpio_output_pin_expression
from esphomeyaml.cpp_types import App, Component, esphomelib_ns, global_ns from esphomeyaml.cpp_types import App, Component, esphomelib_ns, global_ns
@ -34,7 +35,18 @@ CLK_MODES = {
EthernetComponent = esphomelib_ns.class_('EthernetComponent', Component) EthernetComponent = esphomelib_ns.class_('EthernetComponent', Component)
CONFIG_SCHEMA = vol.Schema({
def validate(config):
if CONF_USE_ADDRESS not in config:
if CONF_MANUAL_IP in config:
use_address = str(config[CONF_MANUAL_IP][CONF_STATIC_IP])
else:
use_address = CORE.name + config[CONF_DOMAIN]
config[CONF_USE_ADDRESS] = use_address
return config
CONFIG_SCHEMA = vol.All(vol.Schema({
cv.GenerateID(): cv.declare_variable_id(EthernetComponent), cv.GenerateID(): cv.declare_variable_id(EthernetComponent),
vol.Required(CONF_TYPE): cv.one_of(*ETHERNET_TYPES, upper=True), vol.Required(CONF_TYPE): cv.one_of(*ETHERNET_TYPES, upper=True),
vol.Required(CONF_MDC_PIN): pins.output_pin, vol.Required(CONF_MDC_PIN): pins.output_pin,
@ -44,7 +56,10 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema,
vol.Optional(CONF_MANUAL_IP): wifi.STA_MANUAL_IP_SCHEMA, vol.Optional(CONF_MANUAL_IP): wifi.STA_MANUAL_IP_SCHEMA,
vol.Optional(CONF_DOMAIN, default='.local'): cv.domain_name, vol.Optional(CONF_DOMAIN, default='.local'): cv.domain_name,
}) vol.Optional(CONF_USE_ADDRESS): cv.string_strict,
vol.Optional('hostname'): cv.invalid("The hostname option has been removed in 1.11.0"),
}), validate)
def to_code(config): def to_code(config):
@ -56,6 +71,7 @@ def to_code(config):
add(eth.set_mdio_pin(config[CONF_MDIO_PIN])) add(eth.set_mdio_pin(config[CONF_MDIO_PIN]))
add(eth.set_type(ETHERNET_TYPES[config[CONF_TYPE]])) add(eth.set_type(ETHERNET_TYPES[config[CONF_TYPE]]))
add(eth.set_clk_mode(CLK_MODES[config[CONF_CLK_MODE]])) add(eth.set_clk_mode(CLK_MODES[config[CONF_CLK_MODE]]))
add(eth.set_use_address(config[CONF_USE_ADDRESS]))
if CONF_POWER_PIN in config: if CONF_POWER_PIN in config:
for pin in gpio_output_pin_expression(config[CONF_POWER_PIN]): for pin in gpio_output_pin_expression(config[CONF_POWER_PIN]):

View file

@ -2,9 +2,9 @@ import voluptuous as vol
import esphomeyaml.config_validation as cv import esphomeyaml.config_validation as cv
from esphomeyaml.const import CONF_AP, CONF_BSSID, CONF_CHANNEL, CONF_DNS1, CONF_DNS2, \ from esphomeyaml.const import CONF_AP, CONF_BSSID, CONF_CHANNEL, CONF_DNS1, CONF_DNS2, \
CONF_DOMAIN, \ CONF_DOMAIN, CONF_FAST_CONNECT, CONF_GATEWAY, CONF_ID, CONF_MANUAL_IP, CONF_NETWORKS, \
CONF_FAST_CONNECT, CONF_GATEWAY, CONF_HOSTNAME, CONF_ID, CONF_MANUAL_IP, CONF_NETWORKS, \ CONF_PASSWORD, CONF_POWER_SAVE_MODE, CONF_REBOOT_TIMEOUT, CONF_SSID, CONF_STATIC_IP, \
CONF_PASSWORD, CONF_POWER_SAVE_MODE, CONF_REBOOT_TIMEOUT, CONF_SSID, CONF_STATIC_IP, CONF_SUBNET CONF_SUBNET, CONF_USE_ADDRESS
from esphomeyaml.core import CORE, HexInt from esphomeyaml.core import CORE, HexInt
from esphomeyaml.cpp_generator import Pvariable, StructInitializer, add, variable from esphomeyaml.cpp_generator import Pvariable, StructInitializer, add, variable
from esphomeyaml.cpp_types import App, Component, esphomelib_ns, global_ns from esphomeyaml.cpp_types import App, Component, esphomelib_ns, global_ns
@ -94,6 +94,13 @@ def validate(config):
if len(networks) != 1: if len(networks) != 1:
raise vol.Invalid("Fast connect can only be used with one network!") raise vol.Invalid("Fast connect can only be used with one network!")
if CONF_USE_ADDRESS not in config:
if CONF_MANUAL_IP in config:
use_address = str(config[CONF_MANUAL_IP][CONF_STATIC_IP])
else:
use_address = CORE.name + config[CONF_DOMAIN]
config[CONF_USE_ADDRESS] = use_address
return config return config
@ -106,14 +113,13 @@ CONFIG_SCHEMA = vol.All(vol.Schema({
vol.Optional(CONF_MANUAL_IP): STA_MANUAL_IP_SCHEMA, vol.Optional(CONF_MANUAL_IP): STA_MANUAL_IP_SCHEMA,
vol.Optional(CONF_AP): WIFI_NETWORK_AP, vol.Optional(CONF_AP): WIFI_NETWORK_AP,
vol.Optional(CONF_HOSTNAME): cv.hostname,
vol.Optional(CONF_DOMAIN, default='.local'): cv.domain_name, vol.Optional(CONF_DOMAIN, default='.local'): cv.domain_name,
vol.Optional(CONF_REBOOT_TIMEOUT): cv.positive_time_period_milliseconds, vol.Optional(CONF_REBOOT_TIMEOUT): cv.positive_time_period_milliseconds,
vol.Optional(CONF_POWER_SAVE_MODE): cv.one_of(*WIFI_POWER_SAVE_MODES, upper=True), vol.Optional(CONF_POWER_SAVE_MODE): cv.one_of(*WIFI_POWER_SAVE_MODES, upper=True),
vol.Optional(CONF_FAST_CONNECT): cv.boolean, vol.Optional(CONF_FAST_CONNECT): cv.boolean,
vol.Optional(CONF_USE_ADDRESS): cv.string_strict,
vol.Optional(CONF_HOSTNAME): cv.invalid("The hostname option has been removed in 1.11.0, " vol.Optional('hostname'): cv.invalid("The hostname option has been removed in 1.11.0"),
"now it's always the node name.")
}), validate) }), validate)
@ -152,16 +158,10 @@ def wifi_network(config, static_ip):
return ap return ap
def get_upload_host(config):
if CONF_MANUAL_IP in config:
return str(config[CONF_MANUAL_IP][CONF_STATIC_IP])
hostname = config.get(CONF_HOSTNAME) or CORE.name
return hostname + config[CONF_DOMAIN]
def to_code(config): def to_code(config):
rhs = App.init_wifi() rhs = App.init_wifi()
wifi = Pvariable(config[CONF_ID], rhs) wifi = Pvariable(config[CONF_ID], rhs)
add(wifi.set_use_address(config[CONF_USE_ADDRESS]))
for network in config.get(CONF_NETWORKS, []): for network in config.get(CONF_NETWORKS, []):
add(wifi.add_sta(wifi_network(network, config.get(CONF_MANUAL_IP)))) add(wifi.add_sta(wifi_network(network, config.get(CONF_MANUAL_IP))))

View file

@ -468,7 +468,9 @@ def domain(value):
def domain_name(value): def domain_name(value):
value = string(value) value = string_strict(value)
if not value:
return value
if not value.startswith('.'): if not value.startswith('.'):
raise vol.Invalid("Domain name must start with .") raise vol.Invalid("Domain name must start with .")
if value.startswith('..'): if value.startswith('..'):

View file

@ -112,7 +112,6 @@ CONF_PAYLOAD = 'payload'
CONF_QOS = 'qos' CONF_QOS = 'qos'
CONF_DISCOVERY_RETAIN = 'discovery_retain' CONF_DISCOVERY_RETAIN = 'discovery_retain'
CONF_TOPIC_PREFIX = 'topic_prefix' CONF_TOPIC_PREFIX = 'topic_prefix'
CONF_HOSTNAME = 'hostname'
CONF_PHASE_BALANCER = 'phase_balancer' CONF_PHASE_BALANCER = 'phase_balancer'
CONF_ADDRESS = 'address' CONF_ADDRESS = 'address'
CONF_ENABLE_TIME = 'enable_time' CONF_ENABLE_TIME = 'enable_time'
@ -403,6 +402,7 @@ CONF_FAST_CONNECT = 'fast_connect'
CONF_INTERLOCK = 'interlock' CONF_INTERLOCK = 'interlock'
CONF_ON_TURN_ON = 'on_turn_on' CONF_ON_TURN_ON = 'on_turn_on'
CONF_ON_TURN_OFF = 'on_turn_off' CONF_ON_TURN_OFF = 'on_turn_off'
CONF_USE_ADDRESS = 'use_address'
ALLOWED_NAME_CHARS = u'abcdefghijklmnopqrstuvwxyz0123456789_' ALLOWED_NAME_CHARS = u'abcdefghijklmnopqrstuvwxyz0123456789_'
ARDUINO_VERSION_ESP32_DEV = 'https://github.com/platformio/platform-espressif32.git#feature/stage' ARDUINO_VERSION_ESP32_DEV = 'https://github.com/platformio/platform-espressif32.git#feature/stage'

View file

@ -7,7 +7,7 @@ import os
import re import re
from esphomeyaml.const import CONF_ARDUINO_VERSION, CONF_ESPHOMELIB_VERSION, CONF_ESPHOMEYAML, \ from esphomeyaml.const import CONF_ARDUINO_VERSION, CONF_ESPHOMELIB_VERSION, CONF_ESPHOMEYAML, \
CONF_LOCAL, CONF_WIFI, ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266 CONF_LOCAL, CONF_WIFI, ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_USE_ADDRESS
from esphomeyaml.helpers import ensure_unique_string from esphomeyaml.helpers import ensure_unique_string
# pylint: disable=unused-import, wrong-import-order # pylint: disable=unused-import, wrong-import-order
@ -316,13 +316,11 @@ class EsphomeyamlCore(object):
@property @property
def address(self): # type: () -> str def address(self): # type: () -> str
from esphomeyaml.components import wifi
if 'wifi' in self.config: if 'wifi' in self.config:
return wifi.get_upload_host(self.config[CONF_WIFI]) return self.config[CONF_WIFI][CONF_USE_ADDRESS]
if 'ethernet' in self.config: if 'ethernet' in self.config:
return wifi.get_upload_host(self.config['ethernet']) return self.config['ethernet'][CONF_USE_ADDRESS]
return None return None