mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Fix validation of serial port on windows (#5749)
* Fix validation of serial port on windows * Use pyserial to check serial ports. * Check that persistence file ends with either `.json` or `.pickle`. * Change fix to not rely on pyserial * Use generator expr instead of list comprehension
This commit is contained in:
parent
02dfd9660e
commit
82c99f81fc
@ -7,6 +7,7 @@ https://home-assistant.io/components/sensor.mysensors/
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -81,15 +82,37 @@ def has_all_unique_files(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def is_persistence_file(value):
|
||||||
|
"""Validate that persistence file path ends in either .pickle or .json."""
|
||||||
|
if value.endswith(('.json', '.pickle')):
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
raise vol.Invalid(
|
||||||
|
'{} does not end in either `.json` or `.pickle`'.format(value))
|
||||||
|
|
||||||
|
|
||||||
|
def is_serial_port(value):
|
||||||
|
"""Validate that value is a windows serial port or a unix device."""
|
||||||
|
if sys.platform.startswith('win'):
|
||||||
|
ports = ('COM{}'.format(idx + 1) for idx in range(256))
|
||||||
|
if value in ports:
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
raise vol.Invalid(
|
||||||
|
'{} is not a serial port'.format(value))
|
||||||
|
else:
|
||||||
|
return cv.isdevice(value)
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
vol.Required(CONF_GATEWAYS): vol.All(
|
vol.Required(CONF_GATEWAYS): vol.All(
|
||||||
cv.ensure_list, has_all_unique_files,
|
cv.ensure_list, has_all_unique_files,
|
||||||
[{
|
[{
|
||||||
vol.Required(CONF_DEVICE):
|
vol.Required(CONF_DEVICE):
|
||||||
vol.Any(cv.isdevice, MQTT_COMPONENT, is_socket_address),
|
vol.Any(MQTT_COMPONENT, is_socket_address, is_serial_port),
|
||||||
vol.Optional(CONF_PERSISTENCE_FILE):
|
vol.Optional(CONF_PERSISTENCE_FILE):
|
||||||
vol.All(cv.string, has_parent_dir),
|
vol.All(cv.string, is_persistence_file, has_parent_dir),
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
CONF_BAUD_RATE,
|
CONF_BAUD_RATE,
|
||||||
default=DEFAULT_BAUD_RATE): cv.positive_int,
|
default=DEFAULT_BAUD_RATE): cv.positive_int,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user