Refactor Freebox : add config flow + temperature sensor + signal dispatch (#30334)

* Add config flow to Freebox

* Add manufacturer in device_tracker info

* Add device_info to sensor + switch

* Add device_info: connections

* Add config_flow test + update .coveragerc

* Typing

* Add device_type icon

* Remove one error log

* Fix pylint

* Add myself as CODEOWNER

* Handle sync in one place

* Separate the Freebox[Router/Device/Sensor] from __init__.py

* Add link step to config flow

* Make temperature sensors auto-discovered

* Use device activity instead of reachablility for device_tracker

* Store token file in .storage

Depending on host if list of Freebox integration on the future without breaking change

* Remove IP sensors + add Freebox router as a device with attrs : IPs, conection type, uptime, version & serial

* Add sensor should_poll=False

* Test typing

* Handle devices with no name

* None is the default for data

* Fix comment

* Use config_entry.unique_id

* Add async_unload_entry with asyncio

* Add and use bunch of data size and rate related constants (#31781)

* Review

* Remove useless "already_configured" error string

* Review : merge 2 device & 2 sensor classes

* Entities from platforms

* Fix unload + add device after setup + clean loggers

* async_add_entities True

* Review

* Use pathlib + refactor get_api

* device_tracker set + tests with CoroutineMock()

* Removing active & reachable from tracker attrs

* Review

* Fix pipeline

* typing

* typing

* typing

* Raise ConfigEntryNotReady when HttpRequestError at setup

* Review

* Multiple Freebox s

* Review: store sensors in router

* Freebox: a sensor story
This commit is contained in:
Quentame authored and GitHub committed 2020-03-11 22:15:59 +01:00
1 parent 180bcad477
commit 19be4a5d6d
17 files changed
+917 -165

No files matched your search

+64 -51
View File
@@ -1,29 +1,26 @@
"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
import asyncio
import logging
import socket
from aiofreepybox import Freepybox
from aiofreepybox.exceptions import HttpRequestError
import voluptuous as vol
from homeassistant.components.discovery import SERVICE_FREEBOX
from homeassistant.config_entries import SOURCE_DISCOVERY, SOURCE_IMPORT, ConfigEntry
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
from homeassistant.helpers.typing import HomeAssistantType
from .const import DOMAIN, PLATFORMS
from .router import FreeboxRouter
_LOGGER = logging.getLogger(__name__)
DOMAIN = "freebox"
DATA_FREEBOX = DOMAIN
FREEBOX_CONFIG_FILE = "freebox.conf"
FREEBOX_SCHEMA = vol.Schema(
{vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PORT): cv.port}
)
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PORT): cv.port}
)
},
{DOMAIN: vol.Schema(vol.All(cv.ensure_list, [FREEBOX_SCHEMA]))},
extra=vol.ALLOW_EXTRA,
)
@@ -37,54 +34,70 @@ async def async_setup(hass, config):
host = discovery_info.get("properties", {}).get("api_domain")
port = discovery_info.get("properties", {}).get("https_port")
_LOGGER.info("Discovered Freebox server: %s:%s", host, port)
await async_setup_freebox(hass, config, host, port)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_DISCOVERY},
data={CONF_HOST: host, CONF_PORT: port},
)
)
discovery.async_listen(hass, SERVICE_FREEBOX, discovery_dispatch)
if conf is not None:
host = conf.get(CONF_HOST)
port = conf.get(CONF_PORT)
await async_setup_freebox(hass, config, host, port)
if conf is None:
return True
for freebox_conf in conf:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=freebox_conf,
)
)
return True
async def async_setup_freebox(hass, config, host, port):
"""Start up the Freebox component platforms."""
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Set up Freebox component."""
router = FreeboxRouter(hass, entry)
await router.setup()
app_desc = {
"app_id": "hass",
"app_name": "Home Assistant",
"app_version": "0.65",
"device_name": socket.gethostname(),
}
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = router
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))
for platform in PLATFORMS:
hass.async_create_task(
async_load_platform(hass, "device_tracker", DOMAIN, {}, config)
hass.config_entries.async_forward_entry_setup(entry, platform)
)
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()
# Services
async def async_reboot(call):
"""Handle reboot service call."""
await router.reboot()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_fbx)
hass.services.async_register(DOMAIN, "reboot", async_reboot)
async def async_close_connection(event):
"""Close Freebox connection on HA Stop."""
await router.close()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
return True
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok:
router = hass.data[DOMAIN].pop(entry.unique_id)
await router.close()
return unload_ok