def __init__(self, hass, config, async_see): """Initialize the scanner.""" from aiofreepybox import Freepybox self.host = config[CONF_HOST] self.port = config[CONF_PORT] self.token_file = hass.config.path(FREEBOX_CONFIG_FILE) self.async_see = async_see # Hardcode the app description to avoid invalidating the authentication # file at each new version. # The version can be changed if we want the user to re-authorize HASS # on her Freebox. app_desc = { 'app_id': 'hass', 'app_name': 'Home Assistant', 'app_version': '0.65', 'device_name': socket.gethostname() } api_version = 'v1' # Use the lowest working version. self.fbx = Freepybox( app_desc=app_desc, token_file=self.token_file, api_version=api_version)
async def get_api(hass: HomeAssistantType, host: str) -> Freepybox: """Get the Freebox API.""" freebox_path = Path( hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY).path) freebox_path.mkdir(exist_ok=True) token_file = Path(f"{freebox_path}/{slugify(host)}.conf") return Freepybox(APP_DESC, token_file, API_VERSION)
async def async_setup_freebox(hass, config, host, port): """Start up the Freebox component platforms.""" from aiofreepybox import Freepybox from aiofreepybox.exceptions import HttpRequestError app_desc = { "app_id": "hass", "app_name": "Home Assistant", "app_version": "0.65", "device_name": socket.gethostname(), } token_file = hass.config.path(FREEBOX_CONFIG_FILE) api_version = "v6" fbx = Freepybox(app_desc=app_desc, token_file=token_file, api_version=api_version) try: await fbx.open(host, port) except HttpRequestError: _LOGGER.exception("Failed to connect to Freebox") else: hass.data[DATA_FREEBOX] = fbx async def async_freebox_reboot(call): "Handle reboot service call." await fbx.system.reboot() hass.services.async_register(DOMAIN, 'reboot', async_freebox_reboot) hass.async_create_task( async_load_platform(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.""" await fbx.close() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_fbx)
async def async_setup_freebox(hass, config, host, port): """Start up the Freebox component platforms.""" from aiofreepybox import Freepybox from aiofreepybox.exceptions import HttpRequestError app_desc = { 'app_id': 'hass', 'app_name': 'Home Assistant', 'app_version': '0.65', 'device_name': socket.gethostname() } token_file = hass.config.path(FREEBOX_CONFIG_FILE) api_version = 'v4' fbx = Freepybox(app_desc=app_desc, token_file=token_file, api_version=api_version) try: await fbx.open(host, port) except HttpRequestError: _LOGGER.exception('Failed to connect to Freebox') else: hass.data[DATA_FREEBOX] = fbx hass.async_create_task( async_load_platform(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.""" await fbx.close() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_fbx)