core/homeassistant/components/huum/config_flow.py

64 lines
2.0 KiB
Python
Raw Normal View History

Add Huum integration (#106420) * Add Huum integration * Use DeviceInfo instead of name property for huum climate * Simplify entry setup for huum climate entry * Don’t take status as attribute for huum climate init * Remove unused import * Set unique id as entity id in huum init * Remove unused import for huum climate * Use entry ID as unique ID for device entity * Remove extra newline in huum climate * Upgrade pyhuum to 0.7.4 This version no longer users Pydantic * Parameterize error huum tests * Update all requirements after pyhuum upgrade * Use Huum specific naming for ConfigFlow * Use constants for username and password in huum config flow * Use constants for temperature units * Fix typing and pylint issues * Update pyhuum to 0.7.5 * Use correct enums for data entry flow in Huum tests * Remove test for non-thrown CannotConnect in huum flow tests * Refactor failure config test to also test a successful flow after failure * Fix ruff-format issues * Move _status outside of __init__ and type it * Type temperature argument for _turn_on in huum climate * Use constants for auth in huum config flow test * Refactor validate_into into a inline call in huum config flow * Refactor current and target temperature to be able to return None values * Remove unused huum exceptions * Flip if-statment in async_step_user flow setup to simplify code * Change current and target temperature to be more future proof * Log exception instead of error * Use custom pyhuum exceptions * Add checks for duplicate entries * Use min temp if no target temp has been fetched yet when heating huum * Fix tests so that mock config entry also include username and password * Fix ruff styling issues I don’t know why it keeps doing this. I run `ruff` locally, and then it does not complain, but CI must be doing something else here. * Remove unneded setting of unique id * Update requirements * Refactor temperature setting to support settings target temparature properly
2024-01-25 11:55:55 +00:00
"""Config flow for huum integration."""
from __future__ import annotations
import logging
from typing import Any
from huum.exceptions import Forbidden, NotAuthenticated
from huum.huum import Huum
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
)
class HuumConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for huum."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors = {}
if user_input is not None:
try:
huum_handler = Huum(
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
session=async_get_clientsession(self.hass),
)
await huum_handler.status()
except (Forbidden, NotAuthenticated):
# Most likely Forbidden as that is what is returned from `.status()` with bad creds
_LOGGER.error("Could not log in to Huum with given credentials")
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unknown error")
errors["base"] = "unknown"
else:
self._async_abort_entries_match(
{CONF_USERNAME: user_input[CONF_USERNAME]}
)
return self.async_create_entry(
title=user_input[CONF_USERNAME], data=user_input
)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)