Code cleaning
This commit is contained in:
parent
a8fff06d00
commit
5df2efac05
1 changed files with 29 additions and 15 deletions
|
@ -61,7 +61,9 @@ parser.add_argument(
|
|||
'-r', '--retry',
|
||||
action="store",
|
||||
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,
|
||||
default=DEFAULT_RETRY_COUNT
|
||||
)
|
||||
|
@ -70,14 +72,19 @@ parser.add_argument(
|
|||
'-D', '--delay',
|
||||
action="store",
|
||||
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,
|
||||
default=DEFAULT_RETRY_DELAY
|
||||
)
|
||||
|
||||
|
||||
def exclude_pattern(value):
|
||||
""" Check and compile exclusion pattern parameter """
|
||||
return re.compile(value)
|
||||
|
||||
|
||||
parser.add_argument(
|
||||
'-x', '--exclude',
|
||||
action="append",
|
||||
|
@ -95,14 +102,17 @@ logging.basicConfig(
|
|||
if 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:
|
||||
if pattern.search(dev):
|
||||
logging.debug('Device %s is excluded', dev)
|
||||
if pattern.search(name):
|
||||
logging.debug('Device %s is excluded', name)
|
||||
return True
|
||||
logging.debug('Device %s is not excluded', dev)
|
||||
logging.debug('Device %s is not excluded', name)
|
||||
return False
|
||||
|
||||
|
||||
r = requests.get(f'{options.host}/devices')
|
||||
devices_data = r.json()
|
||||
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')
|
||||
sys.exit(STATUS['UNKNOWN'])
|
||||
|
||||
count = 0
|
||||
while count < options.retry_count:
|
||||
COUNT = 0
|
||||
while COUNT < options.retry_count:
|
||||
r = requests.get(f'{options.host}/ping')
|
||||
count += 1
|
||||
COUNT += 1
|
||||
ping_data = r.json()
|
||||
logging.debug('Ping data: %s (%s)', ping_data, type(ping_data))
|
||||
|
||||
if ping_data:
|
||||
unreachable = False
|
||||
UNREACHABLE = False
|
||||
for dev in ping_data:
|
||||
if not ping_data[dev] and not is_excluded(dev.replace('.yaml', '')):
|
||||
unreachable = True
|
||||
if (
|
||||
not ping_data[dev]
|
||||
and not is_excluded(dev.replace('.yaml', ''))
|
||||
):
|
||||
UNREACHABLE = True
|
||||
break
|
||||
|
||||
if not unreachable:
|
||||
if not UNREACHABLE:
|
||||
break
|
||||
|
||||
logging.debug('Wait %d seconds before retry...', options.retry_delay)
|
||||
|
@ -179,8 +192,9 @@ else:
|
|||
|
||||
print("\nDevices:\n" + "\n".join([
|
||||
(
|
||||
f'- {name} (version = {dev["deployed_version"] if dev["deployed_version"] else "unknown"}, '
|
||||
f'address = {dev["address"] if dev["address"] else "unknown"})'
|
||||
f'- {name} (version = '
|
||||
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()
|
||||
]))
|
||||
|
|
Loading…
Reference in a new issue