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
This commit is contained in:
SNoof85 2019-03-23 15:32:53 +01:00 committed by Anders Melchiorsen
parent 112ed88d64
commit d81df1f0ae
3 changed files with 68 additions and 3 deletions

View file

@ -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."""

View file

@ -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)

View file

@ -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