Advertisement
Guest User

/usr/local/bin/hdhomerun_check.py

a guest
Jan 27th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. ''' See if the HD Homerun box is accessable and running '''
  4.  
  5. import subprocess
  6. import sys
  7. import logging
  8. from time import sleep
  9. from datetime import datetime
  10.  
  11. ATTEMPTS = 21
  12. DELAY = 2
  13.  
  14.  
  15. def get_elapsed_time(start):
  16.     ''' Calculate the time spent waiting for the HDHR to come up '''
  17.  
  18.     delta = datetime.utcnow() - start
  19.     rounded_delta = '{:.3f}'.format(delta.seconds +
  20.                                     (delta.microseconds / 1000000))
  21.     return rounded_delta
  22.  
  23.  
  24. def main():
  25.     ''' Try to discover the HDHR '''
  26.  
  27.     attempt = 0  # Shut up pylint.
  28.     command = 'hdhomerun_config discover'
  29.     logger = logging.getLogger(command)
  30.  
  31.     logging.basicConfig(filename='/var/log/mythtv/hdhr_discovery.log',
  32.                         filemode='a',
  33.                         format='%(asctime)s %(levelname)s\t%(message)s',
  34.                         datefmt='%Y-%m-%d %H:%M:%S',
  35.                         level=logging.INFO)
  36.  
  37.     logger.info('Starting HD Homerun discovery')
  38.  
  39.     start = datetime.utcnow()
  40.  
  41.     for attempt in range(1, ATTEMPTS):
  42.         try:
  43.             sproc = subprocess.Popen(command, stdout=subprocess.PIPE,
  44.                                      shell=True)
  45.             sproc.communicate()
  46.  
  47.         except KeyboardInterrupt:
  48.             sys.exit(2)
  49.  
  50.         if sproc.returncode != 0:
  51.             logger.warning('%s failed, return code = %d.', command,
  52.                            sproc.returncode)
  53.             sleep(DELAY)
  54.         else:
  55.             logger.info('Found HD Homerun. Seconds=%s, attempts=%d.',
  56.                         get_elapsed_time(start), attempt)
  57.             sys.exit(0)
  58.  
  59.     logger.error('Couldn\'t find HD Homerun. Seconds=%s, attempts=%d.',
  60.                  get_elapsed_time(start), attempt)
  61.  
  62.  
  63. if __name__ == '__main__':
  64.     main()
  65.  
  66. # vim: set expandtab tabstop=4 shiftwidth=4 smartindent colorcolumn=80:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement