Add config flow and device registry to fritzbox integration (#31240)

* add config flow

* fix pylint

* update lib

* Update config_flow.py

* remote devices layer in config

* add default host

* avoid double setups of entities

* remove async_setup_platform

* store entities in hass.data

* pass fritz connection together with config_entry

* fritz connections try no4 (or is it even more)

* fix comments

* add unloading

* fixed comments

* Update config_flow.py

* Update const.py

* Update config_flow.py

* Update __init__.py

* Update config_flow.py

* Update __init__.py

* Update __init__.py

* Update config_flow.py

* Update __init__.py

* Update __init__.py

* Update __init__.py

* Update config_flow.py

* add init tests

* test unloading

* add switch tests

* add sensor tests

* add climate tests

* test target temperature

* mock config to package

* comments

* test binary sensor state

* add config flow tests

* comments

* add missing tests

* minor

* remove string title

* deprecate yaml

* don't change yaml

* get devices async

* minor

* add devices again

* comments fixed

* unique_id fixes

* fix patches

* Fix schema

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
escoandandMartin Hjelmare authored and GitHub committed 2020-04-20 15:00:07 +02:00
1 parent 2123f6f133
commit c87ecf0ff6
22 files changed
+1403 -271

No files matched your search

+43 -21
View File
@@ -1,34 +1,40 @@
"""Support for AVM Fritz!Box smarthome switch devices."""
import logging
import requests
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import ATTR_TEMPERATURE, ENERGY_KILO_WATT_HOUR, TEMP_CELSIUS
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_DEVICES,
ENERGY_KILO_WATT_HOUR,
TEMP_CELSIUS,
)
from . import ATTR_STATE_DEVICE_LOCKED, ATTR_STATE_LOCKED, DOMAIN as FRITZBOX_DOMAIN
from .const import (
ATTR_STATE_DEVICE_LOCKED,
ATTR_STATE_LOCKED,
ATTR_TEMPERATURE_UNIT,
ATTR_TOTAL_CONSUMPTION,
ATTR_TOTAL_CONSUMPTION_UNIT,
CONF_CONNECTIONS,
DOMAIN as FRITZBOX_DOMAIN,
LOGGER,
)
_LOGGER = logging.getLogger(__name__)
ATTR_TOTAL_CONSUMPTION = "total_consumption"
ATTR_TOTAL_CONSUMPTION_UNIT = "total_consumption_unit"
ATTR_TOTAL_CONSUMPTION_UNIT_VALUE = ENERGY_KILO_WATT_HOUR
ATTR_TEMPERATURE_UNIT = "temperature_unit"
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Fritzbox smarthome switch from config_entry."""
entities = []
devices = hass.data[FRITZBOX_DOMAIN][CONF_DEVICES]
fritz = hass.data[FRITZBOX_DOMAIN][CONF_CONNECTIONS][config_entry.entry_id]
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Fritzbox smarthome switch platform."""
devices = []
fritz_list = hass.data[FRITZBOX_DOMAIN]
for device in await hass.async_add_executor_job(fritz.get_devices):
if device.has_switch and device.ain not in devices:
entities.append(FritzboxSwitch(device, fritz))
devices.add(device.ain)
for fritz in fritz_list:
device_list = fritz.get_devices()
for device in device_list:
if device.has_switch:
devices.append(FritzboxSwitch(device, fritz))
add_entities(devices)
async_add_entities(entities)
class FritzboxSwitch(SwitchDevice):
@@ -39,6 +45,22 @@ class FritzboxSwitch(SwitchDevice):
self._device = device
self._fritz = fritz
@property
def device_info(self):
"""Return device specific attributes."""
return {
"name": self.name,
"identifiers": {(FRITZBOX_DOMAIN, self._device.ain)},
"manufacturer": self._device.manufacturer,
"model": self._device.productname,
"sw_version": self._device.fw_version,
}
@property
def unique_id(self):
"""Return the unique ID of the device."""
return self._device.ain
@property
def available(self):
"""Return if switch is available."""
@@ -67,7 +89,7 @@ class FritzboxSwitch(SwitchDevice):
try:
self._device.update()
except requests.exceptions.HTTPError as ex:
_LOGGER.warning("Fritzhome connection error: %s", ex)
LOGGER.warning("Fritzhome connection error: %s", ex)
self._fritz.login()
@property