mirror of
https://github.com/esphome/esphome.git
synced 2025-09-05 21:02:20 +01:00
Convert IPAddress to use Pythonmodule ipaddress (#8072)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from ipaddress import AddressValueError, IPv4Address, ip_address
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
@@ -67,7 +68,6 @@ from esphome.const import (
|
||||
from esphome.core import (
|
||||
CORE,
|
||||
HexInt,
|
||||
IPAddress,
|
||||
Lambda,
|
||||
TimePeriod,
|
||||
TimePeriodMicroseconds,
|
||||
@@ -1130,7 +1130,7 @@ def domain(value):
|
||||
if re.match(vol.DOMAIN_REGEX, value) is not None:
|
||||
return value
|
||||
try:
|
||||
return str(ipv4(value))
|
||||
return str(ipaddress(value))
|
||||
except Invalid as err:
|
||||
raise Invalid(f"Invalid domain: {value}") from err
|
||||
|
||||
@@ -1160,21 +1160,20 @@ def ssid(value):
|
||||
return value
|
||||
|
||||
|
||||
def ipv4(value):
|
||||
if isinstance(value, list):
|
||||
parts = value
|
||||
elif isinstance(value, str):
|
||||
parts = value.split(".")
|
||||
elif isinstance(value, IPAddress):
|
||||
return value
|
||||
else:
|
||||
raise Invalid("IPv4 address must consist of either string or integer list")
|
||||
if len(parts) != 4:
|
||||
raise Invalid("IPv4 address must consist of four point-separated integers")
|
||||
parts_ = list(map(int, parts))
|
||||
if not all(0 <= x < 256 for x in parts_):
|
||||
raise Invalid("IPv4 address parts must be in range from 0 to 255")
|
||||
return IPAddress(*parts_)
|
||||
def ipv4address(value):
|
||||
try:
|
||||
address = IPv4Address(value)
|
||||
except AddressValueError as exc:
|
||||
raise Invalid(f"{value} is not a valid IPv4 address") from exc
|
||||
return address
|
||||
|
||||
|
||||
def ipaddress(value):
|
||||
try:
|
||||
address = ip_address(value)
|
||||
except ValueError as exc:
|
||||
raise Invalid(f"{value} is not a valid IP address") from exc
|
||||
return address
|
||||
|
||||
|
||||
def _valid_topic(value):
|
||||
|
Reference in New Issue
Block a user