Add doorsensor + coordinator to nuki (#40933)

* implemented coordinator + doorsensor

* added async_unload_entry

* small fixes + reauth_flow

* update function

* black

* define _data inside __init__

* removed unused property

* await on update & coverage for binary_sensor

* keep reauth seperate from validate

* setting entities unavailable when connection goes down

* add unknown error when entity is not present

* override extra_state_attributes()

* removed unnecessary else

* moved to locks & openers variables

* removed doorsensorState attribute

* changed config entry reload to a task

* wait for reload
This commit is contained in:
Pascal Reeb 2021-04-06 21:20:57 +02:00 committed by GitHub
parent 9f5db2ce3f
commit fb1444c414
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 411 additions and 75 deletions

View file

@ -22,6 +22,8 @@ USER_SCHEMA = vol.Schema(
}
)
REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_TOKEN): str})
async def validate_input(hass, data):
"""Validate the user input allows us to connect.
@ -54,6 +56,7 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
def __init__(self):
"""Initialize the Nuki config flow."""
self.discovery_schema = {}
self._data = {}
async def async_step_import(self, user_input=None):
"""Handle a flow initiated by import."""
@ -79,6 +82,50 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return await self.async_step_validate()
async def async_step_reauth(self, data):
"""Perform reauth upon an API authentication error."""
self._data = data
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(self, user_input=None):
"""Dialog that inform the user that reauth is required."""
errors = {}
if user_input is None:
return self.async_show_form(
step_id="reauth_confirm", data_schema=REAUTH_SCHEMA
)
conf = {
CONF_HOST: self._data[CONF_HOST],
CONF_PORT: self._data[CONF_PORT],
CONF_TOKEN: user_input[CONF_TOKEN],
}
try:
info = await validate_input(self.hass, conf)
except CannotConnect:
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
if not errors:
existing_entry = await self.async_set_unique_id(info["ids"]["hardwareId"])
if existing_entry:
self.hass.config_entries.async_update_entry(existing_entry, data=conf)
self.hass.async_create_task(
self.hass.config_entries.async_reload(existing_entry.entry_id)
)
return self.async_abort(reason="reauth_successful")
errors["base"] = "unknown"
return self.async_show_form(
step_id="reauth_confirm", data_schema=REAUTH_SCHEMA, errors=errors
)
async def async_step_validate(self, user_input=None):
"""Handle init step of a flow."""
@ -102,7 +149,6 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
)
data_schema = self.discovery_schema or USER_SCHEMA
return self.async_show_form(
step_id="user", data_schema=data_schema, errors=errors
)