Skip to content

Commit f58828c

Browse files
authored
Support setting manual_ip under networks option (esphome#2839)
1 parent 11330af commit f58828c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

esphome/components/wifi/__init__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,22 @@ def _validate(config):
221221
raise cv.Invalid("Fast connect can only be used with one network!")
222222

223223
if CONF_USE_ADDRESS not in config:
224+
use_address = CORE.name + config[CONF_DOMAIN]
224225
if CONF_MANUAL_IP in config:
225226
use_address = str(config[CONF_MANUAL_IP][CONF_STATIC_IP])
226-
else:
227-
use_address = CORE.name + config[CONF_DOMAIN]
227+
elif CONF_NETWORKS in config:
228+
ips = set(
229+
str(net[CONF_MANUAL_IP][CONF_STATIC_IP])
230+
for net in config[CONF_NETWORKS]
231+
if CONF_MANUAL_IP in net
232+
)
233+
if len(ips) > 1:
234+
raise cv.Invalid(
235+
"Must specify use_address when using multiple static IP addresses."
236+
)
237+
if len(ips) == 1:
238+
use_address = next(iter(ips))
239+
228240
config[CONF_USE_ADDRESS] = use_address
229241

230242
return config
@@ -334,7 +346,8 @@ async def to_code(config):
334346
cg.add(var.set_use_address(config[CONF_USE_ADDRESS]))
335347

336348
for network in config.get(CONF_NETWORKS, []):
337-
cg.add(var.add_sta(wifi_network(network, config.get(CONF_MANUAL_IP))))
349+
ip_config = network.get(CONF_MANUAL_IP, config.get(CONF_MANUAL_IP))
350+
cg.add(var.add_sta(wifi_network(network, ip_config)))
338351

339352
if CONF_AP in config:
340353
conf = config[CONF_AP]

tests/test5.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ wifi:
1616
networks:
1717
- ssid: 'MySSID'
1818
password: 'password1'
19+
manual_ip:
20+
static_ip: 192.168.1.23
21+
gateway: 192.168.1.1
22+
subnet: 255.255.255.0
1923

2024
api:
2125

0 commit comments

Comments
 (0)