From d81df1f0ae5b6373a0e3620cb78eff1a527fd866 Mon Sep 17 00:00:00 2001 From: SNoof85 <snoof85@gmail.com> Date: Sat, 23 Mar 2019 15:32:53 +0100 Subject: [PATCH] Add Freebox switch platform (#21710) * Added Freebox switch and bump aiofreepybox version * Added missing modified files * removed unused import * gen_requirements_all passed * removed unused import * Remove unused code * lint fixes * More lint fixe * Bump aiofreepybox version and API version to Freebox * Remove URL from log entry * import relative * Sort imports --- homeassistant/components/freebox/__init__.py | 6 +- homeassistant/components/freebox/switch.py | 63 ++++++++++++++++++++ requirements_all.txt | 2 +- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 homeassistant/components/freebox/switch.py diff --git a/homeassistant/components/freebox/__init__.py b/homeassistant/components/freebox/__init__.py index 41e60d884ce..7accf7820f4 100644 --- a/homeassistant/components/freebox/__init__.py +++ b/homeassistant/components/freebox/__init__.py @@ -9,7 +9,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.discovery import async_load_platform -REQUIREMENTS = ['aiofreepybox==0.0.6'] +REQUIREMENTS = ['aiofreepybox==0.0.8'] _LOGGER = logging.getLogger(__name__) @@ -60,7 +60,7 @@ async def async_setup_freebox(hass, config, host, port): } token_file = hass.config.path(FREEBOX_CONFIG_FILE) - api_version = 'v1' + api_version = 'v4' fbx = Freepybox( app_desc=app_desc, @@ -78,6 +78,8 @@ async def async_setup_freebox(hass, config, host, port): hass, 'sensor', DOMAIN, {}, config)) hass.async_create_task(async_load_platform( hass, 'device_tracker', DOMAIN, {}, config)) + hass.async_create_task(async_load_platform( + hass, 'switch', DOMAIN, {}, config)) async def close_fbx(event): """Close Freebox connection on HA Stop.""" diff --git a/homeassistant/components/freebox/switch.py b/homeassistant/components/freebox/switch.py new file mode 100644 index 00000000000..4de194fc902 --- /dev/null +++ b/homeassistant/components/freebox/switch.py @@ -0,0 +1,63 @@ +"""Support for Freebox Delta, Revolution and Mini 4K.""" +import logging + +from homeassistant.components.switch import SwitchDevice + +from . import DATA_FREEBOX + +DEPENDENCIES = ['freebox'] + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_platform( + hass, config, async_add_entities, discovery_info=None): + """Set up the switch.""" + fbx = hass.data[DATA_FREEBOX] + async_add_entities([FbxWifiSwitch(fbx)], True) + + +class FbxWifiSwitch(SwitchDevice): + """Representation of a freebox wifi switch.""" + + def __init__(self, fbx): + """Initilize the Wifi switch.""" + self._name = 'Freebox WiFi' + self._state = None + self._fbx = fbx + + @property + def name(self): + """Return the name of the switch.""" + return self._name + + @property + def is_on(self): + """Return true if device is on.""" + return self._state + + async def _async_set_state(self, enabled): + """Turn the switch on or off.""" + from aiofreepybox.exceptions import InsufficientPermissionsError + + wifi_config = {"enabled": enabled} + try: + await self._fbx.wifi.set_global_config(wifi_config) + except InsufficientPermissionsError: + _LOGGER.warning('Home Assistant does not have permissions to' + ' modify the Freebox settings. Please refer' + ' to documentation.') + + async def async_turn_on(self, **kwargs): + """Turn the switch on.""" + await self._async_set_state(True) + + async def async_turn_off(self, **kwargs): + """Turn the switch off.""" + await self._async_set_state(False) + + async def async_update(self): + """Get the state and update it.""" + datas = await self._fbx.wifi.get_global_config() + active = datas['enabled'] + self._state = bool(active) diff --git a/requirements_all.txt b/requirements_all.txt index ac20e50cd8f..cd74cbc0dd4 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -112,7 +112,7 @@ aiodns==1.1.1 aioesphomeapi==1.7.0 # homeassistant.components.freebox -aiofreepybox==0.0.6 +aiofreepybox==0.0.8 # homeassistant.components.yi.camera aioftp==0.12.0