2019-03-23 14:32:53 +00:00
|
|
|
"""Support for Freebox Delta, Revolution and Mini 4K."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
|
|
|
|
from . import DATA_FREEBOX
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-03-23 14:32:53 +00:00
|
|
|
"""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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "Freebox WiFi"
|
2019-03-23 14:32:53 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Home Assistant does not have permissions to"
|
|
|
|
" modify the Freebox settings. Please refer"
|
|
|
|
" to documentation."
|
|
|
|
)
|
2019-03-23 14:32:53 +00:00
|
|
|
|
|
|
|
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()
|
2019-07-31 19:25:30 +00:00
|
|
|
active = datas["enabled"]
|
2019-03-23 14:32:53 +00:00
|
|
|
self._state = bool(active)
|