* Fix webhook registration * Only load camera platform with valid scope * Add initial data handler and netatmo base class * Update camera to use data handler * Update init * Parallelize API calls * Remove cruft * Minor tweaks * Refactor data handler * Update climate to use data handler * Fix pylint error * Fix climate update not getting fresh data * Update climate data * update to pyatmo 4.0.0 * Refactor for pyatmo 4.0.0 * Exclude from coverage until tests are written * Fix typo * Reduce parallel calls * Add heating request attr * Async get_entities * Undo parallel updates * Fix camera issue * Introduce individual scan interval per device class * Some cleanup * Add basic webhook support for climate to improve responsiveness * Replace ClimateDevice by ClimateEntity * Add support for turning camera on/off * Update camera state upon webhook events * Guard data class registration with lock * Capture errors * Add light platform * Add dis-/connect handling * Fix set schedule service * Remove extra calls * Add service to set person(s) home/away * Add service descriptions * Improve service descriptions * Use LightEntity instead of Light * Add guard if no data is retrieved * Make services entity based * Only raise platform not ready if there is a NOC * Register webhook even during runtime * Fix turning off event * Fix linter error * Fix linter error * Exclude light platform from coverage * Change log level * Refactor public weather sensor to use data handler * Prevent too short coordinates * Ignore modules without _id * Code cleanup * Fix test * Exit early if no home data is retrieved * Prevent discovery if already active * Add services to (un-)register webhook * Fix tests * Not actually a coroutine * Move methods to base class * Address pylint comment * Address pylint complaints * Address comments * Address more comments * Add docstring * Use single instance allowed * Extract method * Remove cruft * Write state directly * Fix test * Add file to coverage * Move nested function * Move nested function * Update docstring * Clean up code * Fix webhook bug * Clean up listeners * Use deque * Clean up prints * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/camera.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/camera.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/camera.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/camera.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/camera.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netatmo/camera.py Co-authored-by: J. Nick Koston <nick@koston.org> * Rename data_class variable * Break when match * Extract method * Extract methods * Rename variable * Improve comment * Some refinements * Extra * Extract method * Simplify code * Improve reability * Code simplification * Simplify code * Simplify code * Code cleanup * Fix import * Clean up * Clean up magic strings * Replace data_class_name with CAMERA_DATA_CLASS_NAME * Replace data_class_name with CAMERA_DATA_CLASS_NAME * Replace data_class_name with HOMEDATA_DATA_CLASS_NAME * Replace data_class_name in public weather sensor * Clean up * Remove deprecated config options * Schedule immediate update on camera reconnect * Use UUID to clearly identify public weather areas * Use subscription mode * Move clean up of temporary data classes * Delay data class removal * Fix linter complaints * Adjust test * Only setup lights if webhook are registered * Prevent crash with old config entries * Don't cache home ids * Remove stale code * Fix coordinates if entered mixed up by the user * Move nested function * Add test case for swapped coordinates * Only wait for discovery entries * Only use what I need * Bring stuff closer to where it's used * Auto clean up setup data classes * Code cleanup * Remove unneccessary lock * Update homeassistant/components/netatmo/sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update tests/components/netatmo/test_config_flow.py Co-authored-by: J. Nick Koston <nick@koston.org> * Clean up dead code * Fix formating * Extend coverage * Extend coverage Co-authored-by: J. Nick Koston <nick@koston.org>
213 lines
7.1 KiB
Python
213 lines
7.1 KiB
Python
"""Config flow for Netatmo."""
|
|
import logging
|
|
import uuid
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_SHOW_ON_MAP
|
|
from homeassistant.core import callback
|
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
|
|
|
from .const import (
|
|
CONF_AREA_NAME,
|
|
CONF_LAT_NE,
|
|
CONF_LAT_SW,
|
|
CONF_LON_NE,
|
|
CONF_LON_SW,
|
|
CONF_NEW_AREA,
|
|
CONF_PUBLIC_MODE,
|
|
CONF_UUID,
|
|
CONF_WEATHER_AREAS,
|
|
DOMAIN,
|
|
)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class NetatmoFlowHandler(
|
|
config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
|
|
):
|
|
"""Config flow to handle Netatmo OAuth2 authentication."""
|
|
|
|
DOMAIN = DOMAIN
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
|
|
|
@staticmethod
|
|
@callback
|
|
def async_get_options_flow(config_entry):
|
|
"""Get the options flow for this handler."""
|
|
return NetatmoOptionsFlowHandler(config_entry)
|
|
|
|
@property
|
|
def logger(self) -> logging.Logger:
|
|
"""Return logger."""
|
|
return logging.getLogger(__name__)
|
|
|
|
@property
|
|
def extra_authorize_data(self) -> dict:
|
|
"""Extra data that needs to be appended to the authorize url."""
|
|
scopes = [
|
|
"read_camera",
|
|
"read_homecoach",
|
|
"read_presence",
|
|
"read_smokedetector",
|
|
"read_station",
|
|
"read_thermostat",
|
|
"write_camera",
|
|
"write_presence",
|
|
"write_thermostat",
|
|
]
|
|
|
|
if self.flow_impl.name != "Home Assistant Cloud":
|
|
scopes.extend(["access_camera", "access_presence"])
|
|
scopes.sort()
|
|
|
|
return {"scope": " ".join(scopes)}
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
"""Handle a flow start."""
|
|
await self.async_set_unique_id(DOMAIN)
|
|
|
|
if self.hass.config_entries.async_entries(DOMAIN):
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
return await super().async_step_user(user_input)
|
|
|
|
async def async_step_homekit(self, homekit_info):
|
|
"""Handle HomeKit discovery."""
|
|
return await self.async_step_user()
|
|
|
|
|
|
class NetatmoOptionsFlowHandler(config_entries.OptionsFlow):
|
|
"""Handle Netatmo options."""
|
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry):
|
|
"""Initialize Netatmo options flow."""
|
|
self.config_entry = config_entry
|
|
self.options = dict(config_entry.options)
|
|
self.options.setdefault(CONF_WEATHER_AREAS, {})
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
"""Manage the Netatmo options."""
|
|
return await self.async_step_public_weather_areas()
|
|
|
|
async def async_step_public_weather_areas(self, user_input=None):
|
|
"""Manage configuration of Netatmo public weather areas."""
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
new_client = user_input.pop(CONF_NEW_AREA, None)
|
|
areas = user_input.pop(CONF_WEATHER_AREAS, None)
|
|
user_input[CONF_WEATHER_AREAS] = {
|
|
area: self.options[CONF_WEATHER_AREAS][area] for area in areas
|
|
}
|
|
self.options.update(user_input)
|
|
if new_client:
|
|
return await self.async_step_public_weather(
|
|
user_input={CONF_NEW_AREA: new_client}
|
|
)
|
|
|
|
return self._update_options()
|
|
|
|
weather_areas = list(self.options[CONF_WEATHER_AREAS])
|
|
|
|
data_schema = vol.Schema(
|
|
{
|
|
vol.Optional(
|
|
CONF_WEATHER_AREAS, default=weather_areas,
|
|
): cv.multi_select(weather_areas),
|
|
vol.Optional(CONF_NEW_AREA): str,
|
|
}
|
|
)
|
|
return self.async_show_form(
|
|
step_id="public_weather_areas", data_schema=data_schema, errors=errors,
|
|
)
|
|
|
|
async def async_step_public_weather(self, user_input=None):
|
|
"""Manage configuration of Netatmo public weather sensors."""
|
|
if user_input is not None and CONF_NEW_AREA not in user_input:
|
|
self.options[CONF_WEATHER_AREAS][
|
|
user_input[CONF_AREA_NAME]
|
|
] = fix_coordinates(user_input)
|
|
|
|
self.options[CONF_WEATHER_AREAS][user_input[CONF_AREA_NAME]][
|
|
CONF_UUID
|
|
] = str(uuid.uuid4())
|
|
|
|
return await self.async_step_public_weather_areas()
|
|
|
|
orig_options = self.config_entry.options.get(CONF_WEATHER_AREAS, {}).get(
|
|
user_input[CONF_NEW_AREA], {}
|
|
)
|
|
|
|
default_longitude = self.hass.config.longitude
|
|
default_latitude = self.hass.config.latitude
|
|
default_size = 0.04
|
|
|
|
data_schema = vol.Schema(
|
|
{
|
|
vol.Optional(CONF_AREA_NAME, default=user_input[CONF_NEW_AREA]): str,
|
|
vol.Optional(
|
|
CONF_LAT_NE,
|
|
default=orig_options.get(
|
|
CONF_LAT_NE, default_latitude + default_size
|
|
),
|
|
): cv.latitude,
|
|
vol.Optional(
|
|
CONF_LON_NE,
|
|
default=orig_options.get(
|
|
CONF_LON_NE, default_longitude + default_size
|
|
),
|
|
): cv.longitude,
|
|
vol.Optional(
|
|
CONF_LAT_SW,
|
|
default=orig_options.get(
|
|
CONF_LAT_SW, default_latitude - default_size
|
|
),
|
|
): cv.latitude,
|
|
vol.Optional(
|
|
CONF_LON_SW,
|
|
default=orig_options.get(
|
|
CONF_LON_SW, default_longitude - default_size
|
|
),
|
|
): cv.longitude,
|
|
vol.Required(
|
|
CONF_PUBLIC_MODE, default=orig_options.get(CONF_PUBLIC_MODE, "avg"),
|
|
): vol.In(["avg", "max"]),
|
|
vol.Required(
|
|
CONF_SHOW_ON_MAP, default=orig_options.get(CONF_SHOW_ON_MAP, False),
|
|
): bool,
|
|
}
|
|
)
|
|
|
|
return self.async_show_form(step_id="public_weather", data_schema=data_schema)
|
|
|
|
def _update_options(self):
|
|
"""Update config entry options."""
|
|
return self.async_create_entry(
|
|
title="Netatmo Public Weather", data=self.options
|
|
)
|
|
|
|
|
|
def fix_coordinates(user_input):
|
|
"""Fix coordinates if they don't comply with the Netatmo API."""
|
|
# Ensure coordinates have acceptable length for the Netatmo API
|
|
for coordinate in [CONF_LAT_NE, CONF_LAT_SW, CONF_LON_NE, CONF_LON_SW]:
|
|
if len(str(user_input[coordinate]).split(".")[1]) < 7:
|
|
user_input[coordinate] = user_input[coordinate] + 0.0000001
|
|
|
|
# Swap coordinates if entered in wrong order
|
|
if user_input[CONF_LAT_NE] < user_input[CONF_LAT_SW]:
|
|
user_input[CONF_LAT_NE], user_input[CONF_LAT_SW] = (
|
|
user_input[CONF_LAT_SW],
|
|
user_input[CONF_LAT_NE],
|
|
)
|
|
if user_input[CONF_LON_NE] < user_input[CONF_LON_SW]:
|
|
user_input[CONF_LON_NE], user_input[CONF_LON_SW] = (
|
|
user_input[CONF_LON_SW],
|
|
user_input[CONF_LON_NE],
|
|
)
|
|
|
|
return user_input
|