Code cleaning

This commit is contained in:
Benjamin Renard 2022-12-03 15:25:07 +01:00 committed by root
parent a8fff06d00
commit 5df2efac05

View file

@ -61,7 +61,9 @@ parser.add_argument(
'-r', '--retry', '-r', '--retry',
action="store", action="store",
dest="retry_count", dest="retry_count",
help=f'Number of retry to retreive device status (default: {DEFAULT_RETRY_COUNT})', help=(
'Number of retry to retreive device status '
f'(default: {DEFAULT_RETRY_COUNT})'),
type=int, type=int,
default=DEFAULT_RETRY_COUNT default=DEFAULT_RETRY_COUNT
) )
@ -70,14 +72,19 @@ parser.add_argument(
'-D', '--delay', '-D', '--delay',
action="store", action="store",
dest="retry_delay", dest="retry_delay",
help=f'Delay in second between two retry to retreive device status (default: {DEFAULT_RETRY_DELAY}s)', help=(
'Delay in second between two retry to retreive device status '
f'(default: {DEFAULT_RETRY_DELAY}s)'),
type=int, type=int,
default=DEFAULT_RETRY_DELAY default=DEFAULT_RETRY_DELAY
) )
def exclude_pattern(value): def exclude_pattern(value):
""" Check and compile exclusion pattern parameter """
return re.compile(value) return re.compile(value)
parser.add_argument( parser.add_argument(
'-x', '--exclude', '-x', '--exclude',
action="append", action="append",
@ -95,14 +102,17 @@ logging.basicConfig(
if options.host[-1] == '/': if options.host[-1] == '/':
options.host = options.host[-1] options.host = options.host[-1]
def is_excluded(dev):
def is_excluded(name):
""" Check if device is excluded """
for pattern in options.exclude: for pattern in options.exclude:
if pattern.search(dev): if pattern.search(name):
logging.debug('Device %s is excluded', dev) logging.debug('Device %s is excluded', name)
return True return True
logging.debug('Device %s is not excluded', dev) logging.debug('Device %s is not excluded', name)
return False return False
r = requests.get(f'{options.host}/devices') r = requests.get(f'{options.host}/devices')
devices_data = r.json() devices_data = r.json()
logging.debug('Devices data: %s (%s)', devices_data, type(devices_data)) logging.debug('Devices data: %s (%s)', devices_data, type(devices_data))
@ -111,21 +121,24 @@ if not devices_data:
print('UNKNOWN - Fail to retreive devices using ESPHome Dashboard API') print('UNKNOWN - Fail to retreive devices using ESPHome Dashboard API')
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS['UNKNOWN'])
count = 0 COUNT = 0
while count < options.retry_count: while COUNT < options.retry_count:
r = requests.get(f'{options.host}/ping') r = requests.get(f'{options.host}/ping')
count += 1 COUNT += 1
ping_data = r.json() ping_data = r.json()
logging.debug('Ping data: %s (%s)', ping_data, type(ping_data)) logging.debug('Ping data: %s (%s)', ping_data, type(ping_data))
if ping_data: if ping_data:
unreachable = False UNREACHABLE = False
for dev in ping_data: for dev in ping_data:
if not ping_data[dev] and not is_excluded(dev.replace('.yaml', '')): if (
unreachable = True not ping_data[dev]
and not is_excluded(dev.replace('.yaml', ''))
):
UNREACHABLE = True
break break
if not unreachable: if not UNREACHABLE:
break break
logging.debug('Wait %d seconds before retry...', options.retry_delay) logging.debug('Wait %d seconds before retry...', options.retry_delay)
@ -179,8 +192,9 @@ else:
print("\nDevices:\n" + "\n".join([ print("\nDevices:\n" + "\n".join([
( (
f'- {name} (version = {dev["deployed_version"] if dev["deployed_version"] else "unknown"}, ' f'- {name} (version = '
f'address = {dev["address"] if dev["address"] else "unknown"})' f'{dev["deployed_version"] if dev["deployed_version"] else "unknown"}'
f', address = {dev["address"] if dev["address"] else "unknown"})'
) )
for name, dev in devices.items() for name, dev in devices.items()
])) ]))