Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,8 @@ homeassistant/components/tmb/* @alemuro
homeassistant/components/todoist/* @boralyl
homeassistant/components/tolo/* @MatthiasLohr
tests/components/tolo/* @MatthiasLohr
homeassistant/components/tomorrowio/* @raman325
tests/components/tomorrowio/* @raman325
homeassistant/components/totalconnect/* @austinmroczek
tests/components/totalconnect/* @austinmroczek
homeassistant/components/tplink/* @rytilahti @thegardenmonkey
Expand Down
117 changes: 3 additions & 114 deletions homeassistant/components/climacell/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,15 @@
"""Config flow for ClimaCell integration."""
from __future__ import annotations

import logging
from typing import Any

from pyclimacell import ClimaCellV3
from pyclimacell.exceptions import (
CantConnectException,
InvalidAPIKeyException,
RateLimitedException,
)
from pyclimacell.pyclimacell import ClimaCellV4
import voluptuous as vol

from homeassistant import config_entries, core
from homeassistant.const import (
CONF_API_KEY,
CONF_API_VERSION,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv

from .const import (
CC_ATTR_TEMPERATURE,
CC_V3_ATTR_TEMPERATURE,
CONF_TIMESTEP,
DEFAULT_NAME,
DEFAULT_TIMESTEP,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)


def _get_config_schema(
hass: core.HomeAssistant, input_dict: dict[str, Any] = None
) -> vol.Schema:
"""
Return schema defaults for init step based on user input/config dict.

Retain info already provided for future form views by setting them as
defaults in schema.
"""
if input_dict is None:
input_dict = {}

return vol.Schema(
{
vol.Required(
CONF_NAME, default=input_dict.get(CONF_NAME, DEFAULT_NAME)
): str,
vol.Required(CONF_API_KEY, default=input_dict.get(CONF_API_KEY)): str,
vol.Required(CONF_API_VERSION, default=4): vol.In([3, 4]),
vol.Inclusive(
CONF_LATITUDE,
"location",
default=input_dict.get(CONF_LATITUDE, hass.config.latitude),
): cv.latitude,
vol.Inclusive(
CONF_LONGITUDE,
"location",
default=input_dict.get(CONF_LONGITUDE, hass.config.longitude),
): cv.longitude,
},
extra=vol.REMOVE_EXTRA,
)


def _get_unique_id(hass: HomeAssistant, input_dict: dict[str, Any]):
"""Return unique ID from config data."""
return (
f"{input_dict[CONF_API_KEY]}"
f"_{input_dict.get(CONF_LATITUDE, hass.config.latitude)}"
f"_{input_dict.get(CONF_LONGITUDE, hass.config.longitude)}"
)
from .const import CONF_TIMESTEP, DEFAULT_TIMESTEP, DOMAIN


class ClimaCellOptionsConfigFlow(config_entries.OptionsFlow):
Expand Down Expand Up @@ -117,45 +48,3 @@ def async_get_options_flow(
) -> ClimaCellOptionsConfigFlow:
"""Get the options flow for this handler."""
return ClimaCellOptionsConfigFlow(config_entry)

async def async_step_user(self, user_input: dict[str, Any] = None) -> FlowResult:
"""Handle the initial step."""
errors = {}
if user_input is not None:
await self.async_set_unique_id(
unique_id=_get_unique_id(self.hass, user_input)
)
self._abort_if_unique_id_configured()

try:
if user_input[CONF_API_VERSION] == 3:
api_class = ClimaCellV3
field = CC_V3_ATTR_TEMPERATURE
else:
api_class = ClimaCellV4
field = CC_ATTR_TEMPERATURE
await api_class(
user_input[CONF_API_KEY],
str(user_input.get(CONF_LATITUDE, self.hass.config.latitude)),
str(user_input.get(CONF_LONGITUDE, self.hass.config.longitude)),
session=async_get_clientsession(self.hass),
).realtime([field])

return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
except CantConnectException:
errors["base"] = "cannot_connect"
except InvalidAPIKeyException:
errors[CONF_API_KEY] = "invalid_api_key"
except RateLimitedException:
errors[CONF_API_KEY] = "rate_limited"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"

return self.async_show_form(
step_id="user",
data_schema=_get_config_schema(self.hass, user_input),
errors=errors,
)
2 changes: 1 addition & 1 deletion homeassistant/components/climacell/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "climacell",
"name": "ClimaCell",
"config_flow": true,
"config_flow": false,
"documentation": "https://www.home-assistant.io/integrations/climacell",
"requirements": ["pyclimacell==0.18.2"],
"codeowners": ["@raman325"],
Expand Down
22 changes: 1 addition & 21 deletions homeassistant/components/climacell/strings.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
{
"config": {
"step": {
"user": {
"description": "If [%key:common::config_flow::data::latitude%] and [%key:common::config_flow::data::longitude%] are not provided, the default values in the Home Assistant configuration will be used. An entity will be created for each forecast type but only the ones you select will be enabled by default.",
"data": {
"name": "[%key:common::config_flow::data::name%]",
"api_key": "[%key:common::config_flow::data::api_key%]",
"api_version": "API Version",
"latitude": "[%key:common::config_flow::data::latitude%]",
"longitude": "[%key:common::config_flow::data::longitude%]"
}
}
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]",
"unknown": "[%key:common::config_flow::error::unknown%]",
"rate_limited": "Currently rate limited, please try again later."
}
},
"options": {
"step": {
"init": {
"title": "Update [%key:component::climacell::title%] Options",
"title": "Update ClimaCell Options",
"description": "If you choose to enable the `nowcast` forecast entity, you can configure the number of minutes between each forecast. The number of forecasts provided depends on the number of minutes chosen between forecasts.",
"data": {
"timestep": "Min. Between NowCast Forecasts"
Expand Down
23 changes: 1 addition & 22 deletions homeassistant/components/climacell/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
{
"config": {
"error": {
"cannot_connect": "Failed to connect",
"invalid_api_key": "Invalid API key",
"rate_limited": "Currently rate limited, please try again later.",
"unknown": "Unexpected error"
},
"step": {
"user": {
"data": {
"api_key": "API Key",
"api_version": "API Version",
"latitude": "Latitude",
"longitude": "Longitude",
"name": "Name"
},
"description": "If Latitude and Longitude are not provided, the default values in the Home Assistant configuration will be used. An entity will be created for each forecast type but only the ones you select will be enabled by default."
}
}
},
"options": {
"step": {
"init": {
Expand All @@ -29,6 +9,5 @@
"title": "Update ClimaCell Options"
}
}
},
"title": "ClimaCell"
}
}
Loading