#2 Fritz!Box Guest Wifi

Egyesítve
felix 1 commit egyesítve innen: cobra/master ide: felix/master ekkor: 2 éve

+ 3 - 0
README.md

@@ -1,4 +1,7 @@
 # Version History
 # Version History
 
 
+## 0.2
+* new custom component to call Fritz!Box to turn on/off the configured Guest Wifi
+
 ## 0.1
 ## 0.1
 * wakeup package for Philips Hue Lights
 * wakeup package for Philips Hue Lights

+ 57 - 0
custom_components/fritzbox_guestwifi.py

@@ -0,0 +1,57 @@
+import logging
+
+DOMAIN = 'fritzbox_guestwifi'
+REQUIREMENTS = ['fritzconnection==0.6.5']
+_LOGGER = logging.getLogger(__name__)
+
+
+def setup(hass, config):
+    _LOGGER.debug('Setting up GuestWifi Component')
+    host = config[DOMAIN].get('host', '192.168.178.1')
+    port = config[DOMAIN].get('port', 49000)
+    username = config[DOMAIN].get('username', '')
+    password = config[DOMAIN].get('password', None)
+
+    if not password:
+        raise ValueError('Password is not set in configuration')
+
+    guest_wifi = FritzBoxGuestWifi(
+        host=host,
+        port=port,
+        username=username,
+        password=password
+    )
+
+    hass.services.register(DOMAIN, 'turn_on', guest_wifi.turn_on)
+    hass.services.register(DOMAIN, 'turn_off', guest_wifi.turn_off)
+    return True
+
+
+class FritzBoxGuestWifi(object):
+
+    def __init__(self, host, port, username, password):
+        # pylint: disable=import-error
+        import fritzconnection as fc
+        self._connection = fc.FritzConnection(
+            address=host,
+            port=port,
+            user=username,
+            password=password
+        )
+
+    def turn_on(self, call):
+        _LOGGER.info('Turning on guest wifi.')
+        self._handle_turn_on_off(True)
+
+    def turn_off(self, call):
+        _LOGGER.info('Turning off guest wifi.')
+        self._handle_turn_on_off(False)
+
+    def _handle_turn_on_off(self, turn_on):
+        from fritzconnection.fritzconnection import ServiceError, ActionError
+        new_state = '1' if turn_on else '0'
+        try:
+            self._connection.call_action('WLANConfiguration:3', 'SetEnable', NewEnable=new_state)
+        except ServiceError or ActionError:
+            _LOGGER.error('Home Assistant cannot call the wished service on the FRITZ!Box. '
+                          'Are credentials, address and port correct?')

+ 26 - 0
packages/fritzbox_guestwifi.yaml

@@ -0,0 +1,26 @@
+input_boolean:
+  guest_wifi:
+    name: "Wifi Guest"
+    initial: off
+    icon: mdi:access-point-network
+    
+fritzbox_guestwifi:
+  host: "192.168.178.1"
+  username: !secret fb_username
+  password: !secret fb_password
+
+group:
+  fritzbox:
+    name: Fritz!Box
+    entities:
+      - input_boolean.guest_wifi
+
+automation:
+  - action:
+    - service_template: fritzbox_guestwifi.turn_{{ states.input_boolean.guest_wifi.state }}
+    alias: "Wifi Guest"
+    hide_entity: True
+    condition: []
+    trigger:
+    - entity_id: input_boolean.guest_wifi
+      platform: state