mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[config validation] Add more ip address / network validators (#9181)
This commit is contained in:
		| @@ -3,7 +3,15 @@ | ||||
| from contextlib import contextmanager | ||||
| from dataclasses import dataclass | ||||
| from datetime import datetime | ||||
| from ipaddress import AddressValueError, IPv4Address, ip_address | ||||
| from ipaddress import ( | ||||
|     AddressValueError, | ||||
|     IPv4Address, | ||||
|     IPv4Network, | ||||
|     IPv6Address, | ||||
|     IPv6Network, | ||||
|     ip_address, | ||||
|     ip_network, | ||||
| ) | ||||
| import logging | ||||
| import os | ||||
| import re | ||||
| @@ -1176,6 +1184,14 @@ def ipv4address(value): | ||||
|     return address | ||||
|  | ||||
|  | ||||
| def ipv6address(value): | ||||
|     try: | ||||
|         address = IPv6Address(value) | ||||
|     except AddressValueError as exc: | ||||
|         raise Invalid(f"{value} is not a valid IPv6 address") from exc | ||||
|     return address | ||||
|  | ||||
|  | ||||
| def ipv4address_multi_broadcast(value): | ||||
|     address = ipv4address(value) | ||||
|     if not (address.is_multicast or (address == IPv4Address("255.255.255.255"))): | ||||
| @@ -1193,6 +1209,33 @@ def ipaddress(value): | ||||
|     return address | ||||
|  | ||||
|  | ||||
| def ipv4network(value): | ||||
|     """Validate that the value is a valid IPv4 network.""" | ||||
|     try: | ||||
|         network = IPv4Network(value, strict=False) | ||||
|     except ValueError as exc: | ||||
|         raise Invalid(f"{value} is not a valid IPv4 network") from exc | ||||
|     return network | ||||
|  | ||||
|  | ||||
| def ipv6network(value): | ||||
|     """Validate that the value is a valid IPv6 network.""" | ||||
|     try: | ||||
|         network = IPv6Network(value, strict=False) | ||||
|     except ValueError as exc: | ||||
|         raise Invalid(f"{value} is not a valid IPv6 network") from exc | ||||
|     return network | ||||
|  | ||||
|  | ||||
| def ipnetwork(value): | ||||
|     """Validate that the value is a valid IP network.""" | ||||
|     try: | ||||
|         network = ip_network(value, strict=False) | ||||
|     except ValueError as exc: | ||||
|         raise Invalid(f"{value} is not a valid IP network") from exc | ||||
|     return network | ||||
|  | ||||
|  | ||||
| def _valid_topic(value): | ||||
|     """Validate that this is a valid topic name/filter.""" | ||||
|     if value is None:  # Used to disable publishing and subscribing | ||||
|   | ||||
| @@ -5,7 +5,7 @@ import fnmatch | ||||
| import functools | ||||
| import inspect | ||||
| from io import BytesIO, TextIOBase, TextIOWrapper | ||||
| from ipaddress import _BaseAddress | ||||
| from ipaddress import _BaseAddress, _BaseNetwork | ||||
| import logging | ||||
| import math | ||||
| import os | ||||
| @@ -621,6 +621,7 @@ ESPHomeDumper.add_multi_representer(str, ESPHomeDumper.represent_stringify) | ||||
| ESPHomeDumper.add_multi_representer(int, ESPHomeDumper.represent_int) | ||||
| ESPHomeDumper.add_multi_representer(float, ESPHomeDumper.represent_float) | ||||
| ESPHomeDumper.add_multi_representer(_BaseAddress, ESPHomeDumper.represent_stringify) | ||||
| ESPHomeDumper.add_multi_representer(_BaseNetwork, ESPHomeDumper.represent_stringify) | ||||
| ESPHomeDumper.add_multi_representer(MACAddress, ESPHomeDumper.represent_stringify) | ||||
| ESPHomeDumper.add_multi_representer(TimePeriod, ESPHomeDumper.represent_stringify) | ||||
| ESPHomeDumper.add_multi_representer(Lambda, ESPHomeDumper.represent_lambda) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user