fritzbox_guestwifi.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import logging
  2. DOMAIN = 'fritzbox_guestwifi'
  3. REQUIREMENTS = ['fritzconnection==0.6.5']
  4. _LOGGER = logging.getLogger(__name__)
  5. def setup(hass, config):
  6. _LOGGER.debug('Setting up GuestWifi Component')
  7. host = config[DOMAIN].get('host', '192.168.178.1')
  8. port = config[DOMAIN].get('port', 49000)
  9. username = config[DOMAIN].get('username', '')
  10. password = config[DOMAIN].get('password', None)
  11. if not password:
  12. raise ValueError('Password is not set in configuration')
  13. guest_wifi = FritzBoxGuestWifi(
  14. host=host,
  15. port=port,
  16. username=username,
  17. password=password
  18. )
  19. hass.services.register(DOMAIN, 'turn_on', guest_wifi.turn_on)
  20. hass.services.register(DOMAIN, 'turn_off', guest_wifi.turn_off)
  21. return True
  22. class FritzBoxGuestWifi(object):
  23. def __init__(self, host, port, username, password):
  24. # pylint: disable=import-error
  25. import fritzconnection as fc
  26. self._connection = fc.FritzConnection(
  27. address=host,
  28. port=port,
  29. user=username,
  30. password=password
  31. )
  32. def turn_on(self, call):
  33. _LOGGER.info('Turning on guest wifi.')
  34. self._handle_turn_on_off(True)
  35. def turn_off(self, call):
  36. _LOGGER.info('Turning off guest wifi.')
  37. self._handle_turn_on_off(False)
  38. def _handle_turn_on_off(self, turn_on):
  39. from fritzconnection.fritzconnection import ServiceError, ActionError
  40. new_state = '1' if turn_on else '0'
  41. try:
  42. self._connection.call_action('WLANConfiguration:3', 'SetEnable', NewEnable=new_state)
  43. except ServiceError or ActionError:
  44. _LOGGER.error('Home Assistant cannot call the wished service on the FRITZ!Box. '
  45. 'Are credentials, address and port correct?')