diff --git a/homeassistant/components/tomorrowio/__init__.py b/homeassistant/components/tomorrowio/__init__.py index 68070164312..f55ea4c16da 100644 --- a/homeassistant/components/tomorrowio/__init__.py +++ b/homeassistant/components/tomorrowio/__init__.py @@ -18,7 +18,13 @@ from pytomorrowio.exceptions import ( from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME +from homeassistant.const import ( + CONF_API_KEY, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, + CONF_NAME, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -194,8 +200,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: api = TomorrowioV4( entry.data[CONF_API_KEY], - entry.data[CONF_LATITUDE], - entry.data[CONF_LONGITUDE], + entry.data[CONF_LOCATION][CONF_LATITUDE], + entry.data[CONF_LOCATION][CONF_LONGITUDE], session=async_get_clientsession(hass), ) diff --git a/homeassistant/components/tomorrowio/config_flow.py b/homeassistant/components/tomorrowio/config_flow.py index ac8ceeaa9d5..bde49ed3fe5 100644 --- a/homeassistant/components/tomorrowio/config_flow.py +++ b/homeassistant/components/tomorrowio/config_flow.py @@ -20,13 +20,14 @@ from homeassistant.const import ( CONF_API_VERSION, CONF_FRIENDLY_NAME, CONF_LATITUDE, + CONF_LOCATION, CONF_LONGITUDE, CONF_NAME, ) from homeassistant.core import HomeAssistant, 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 homeassistant.helpers.selector import selector from .const import ( AUTO_MIGRATION_MESSAGE, @@ -63,19 +64,21 @@ def _get_config_schema( if source == config_entries.SOURCE_IMPORT: return vol.Schema(api_key_schema, extra=vol.REMOVE_EXTRA) + default_location = ( + input_dict[CONF_LOCATION] + if CONF_LOCATION in input_dict + else { + CONF_LATITUDE: hass.config.latitude, + CONF_LONGITUDE: hass.config.longitude, + } + ) return vol.Schema( { **api_key_schema, vol.Required( - CONF_LATITUDE, - "location", - default=input_dict.get(CONF_LATITUDE, hass.config.latitude), - ): cv.latitude, - vol.Required( - CONF_LONGITUDE, - "location", - default=input_dict.get(CONF_LONGITUDE, hass.config.longitude), - ): cv.longitude, + CONF_LOCATION, + default=default_location, + ): selector({"location": {"radius": False}}), }, ) @@ -84,8 +87,8 @@ 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)}" + f"_{input_dict[CONF_LOCATION][CONF_LATITUDE]}" + f"_{input_dict[CONF_LOCATION][CONF_LONGITUDE]}" ) @@ -138,14 +141,23 @@ class TomorrowioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # Grab the API key and add it to the rest of the config before continuing if self._import_config: self._import_config[CONF_API_KEY] = user_input[CONF_API_KEY] + self._import_config[CONF_LOCATION] = { + CONF_LATITUDE: self._import_config.pop( + CONF_LATITUDE, self.hass.config.latitude + ), + CONF_LONGITUDE: self._import_config.pop( + CONF_LONGITUDE, self.hass.config.longitude + ), + } user_input = self._import_config.copy() await self.async_set_unique_id( unique_id=_get_unique_id(self.hass, user_input) ) self._abort_if_unique_id_configured() - latitude = user_input.get(CONF_LATITUDE, self.hass.config.latitude) - longitude = user_input.get(CONF_LONGITUDE, self.hass.config.longitude) + location = user_input[CONF_LOCATION] + latitude = location[CONF_LATITUDE] + longitude = location[CONF_LONGITUDE] if CONF_NAME not in user_input: user_input[CONF_NAME] = DEFAULT_NAME # Append zone name if it exists and we are using the default name diff --git a/homeassistant/components/tomorrowio/strings.json b/homeassistant/components/tomorrowio/strings.json index b681dc4d043..30b729e6ef1 100644 --- a/homeassistant/components/tomorrowio/strings.json +++ b/homeassistant/components/tomorrowio/strings.json @@ -6,8 +6,7 @@ "data": { "name": "[%key:common::config_flow::data::name%]", "api_key": "[%key:common::config_flow::data::api_key%]", - "latitude": "[%key:common::config_flow::data::latitude%]", - "longitude": "[%key:common::config_flow::data::longitude%]" + "location": "[%key:common::config_flow::data::location%]" } } }, diff --git a/tests/components/climacell/const.py b/tests/components/climacell/const.py index 88a9cbd54cd..d6487bfa6ce 100644 --- a/tests/components/climacell/const.py +++ b/tests/components/climacell/const.py @@ -13,7 +13,7 @@ API_KEY = "aa" API_V3_ENTRY_DATA = { CONF_NAME: "ClimaCell", CONF_API_KEY: API_KEY, - CONF_LATITUDE: 80, - CONF_LONGITUDE: 80, + CONF_LATITUDE: 80.0, + CONF_LONGITUDE: 80.0, CONF_API_VERSION: 3, } diff --git a/tests/components/tomorrowio/const.py b/tests/components/tomorrowio/const.py index 8cb00c6707e..cc9fe803f4e 100644 --- a/tests/components/tomorrowio/const.py +++ b/tests/components/tomorrowio/const.py @@ -1,6 +1,10 @@ """Constants for tomorrowio tests.""" - -from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.const import ( + CONF_API_KEY, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, +) API_KEY = "aa" @@ -8,14 +12,10 @@ MIN_CONFIG = { CONF_API_KEY: API_KEY, } -V1_ENTRY_DATA = { - CONF_API_KEY: API_KEY, - CONF_LATITUDE: 80, - CONF_LONGITUDE: 80, -} - API_V4_ENTRY_DATA = { CONF_API_KEY: API_KEY, - CONF_LATITUDE: 80, - CONF_LONGITUDE: 80, + CONF_LOCATION: { + CONF_LATITUDE: 80.0, + CONF_LONGITUDE: 80.0, + }, } diff --git a/tests/components/tomorrowio/test_config_flow.py b/tests/components/tomorrowio/test_config_flow.py index 7e04e213bbb..ca888210a46 100644 --- a/tests/components/tomorrowio/test_config_flow.py +++ b/tests/components/tomorrowio/test_config_flow.py @@ -25,6 +25,7 @@ from homeassistant.const import ( CONF_API_KEY, CONF_API_VERSION, CONF_LATITUDE, + CONF_LOCATION, CONF_LONGITUDE, CONF_NAME, CONF_RADIUS, @@ -55,8 +56,8 @@ async def test_user_flow_minimum_fields(hass: HomeAssistant) -> None: assert result["title"] == DEFAULT_NAME assert result["data"][CONF_NAME] == DEFAULT_NAME assert result["data"][CONF_API_KEY] == API_KEY - assert result["data"][CONF_LATITUDE] == hass.config.latitude - assert result["data"][CONF_LONGITUDE] == hass.config.longitude + assert result["data"][CONF_LOCATION][CONF_LATITUDE] == hass.config.latitude + assert result["data"][CONF_LOCATION][CONF_LONGITUDE] == hass.config.longitude async def test_user_flow_minimum_fields_in_zone(hass: HomeAssistant) -> None: @@ -88,8 +89,8 @@ async def test_user_flow_minimum_fields_in_zone(hass: HomeAssistant) -> None: assert result["title"] == f"{DEFAULT_NAME} - Home" assert result["data"][CONF_NAME] == f"{DEFAULT_NAME} - Home" assert result["data"][CONF_API_KEY] == API_KEY - assert result["data"][CONF_LATITUDE] == hass.config.latitude - assert result["data"][CONF_LONGITUDE] == hass.config.longitude + assert result["data"][CONF_LOCATION][CONF_LATITUDE] == hass.config.latitude + assert result["data"][CONF_LOCATION][CONF_LONGITUDE] == hass.config.longitude async def test_user_flow_same_unique_ids(hass: HomeAssistant) -> None: @@ -219,7 +220,7 @@ async def test_import_flow_v4(hass: HomeAssistant) -> None: domain=CC_DOMAIN, data=user_config, source=SOURCE_USER, - unique_id=_get_unique_id(hass, user_config), + unique_id="test", version=1, ) old_entry.add_to_hass(hass) @@ -243,7 +244,7 @@ async def test_import_flow_v3( domain=CC_DOMAIN, data=user_config, source=SOURCE_USER, - unique_id=_get_unique_id(hass, user_config), + unique_id="test", version=1, ) old_entry.add_to_hass(hass) @@ -264,8 +265,10 @@ async def test_import_flow_v3( assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == { CONF_API_KEY: "this is a test", - CONF_LATITUDE: 80, - CONF_LONGITUDE: 80, + CONF_LOCATION: { + CONF_LATITUDE: 80.0, + CONF_LONGITUDE: 80.0, + }, CONF_NAME: "ClimaCell", "old_config_entry_id": old_entry.entry_id, } diff --git a/tests/components/tomorrowio/test_init.py b/tests/components/tomorrowio/test_init.py index 35dc7fd2d0a..c9914bf95be 100644 --- a/tests/components/tomorrowio/test_init.py +++ b/tests/components/tomorrowio/test_init.py @@ -7,7 +7,14 @@ from homeassistant.components.tomorrowio.config_flow import ( from homeassistant.components.tomorrowio.const import DOMAIN from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER -from homeassistant.const import CONF_API_KEY, CONF_API_VERSION, CONF_NAME +from homeassistant.const import ( + CONF_API_KEY, + CONF_API_VERSION, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, + CONF_NAME, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -49,7 +56,7 @@ async def test_climacell_migration_logic( old_config_entry = MockConfigEntry( domain=CC_DOMAIN, data=old_data, - unique_id=_get_unique_id(hass, old_data), + unique_id="v3apikey_80.0_80.0", version=1, ) old_config_entry.add_to_hass(hass) @@ -70,7 +77,7 @@ async def test_climacell_migration_logic( old_entity_daily = ent_reg.async_get_or_create( "weather", CC_DOMAIN, - f"{_get_unique_id(hass, old_data)}_daily", + "v3apikey_80.0_80.0_daily", config_entry=old_config_entry, original_name="ClimaCell - Daily", suggested_object_id="climacell_daily", @@ -79,7 +86,7 @@ async def test_climacell_migration_logic( old_entity_hourly = ent_reg.async_get_or_create( "weather", CC_DOMAIN, - f"{_get_unique_id(hass, old_data)}_hourly", + "v3apikey_80.0_80.0_hourly", config_entry=old_config_entry, original_name="ClimaCell - Hourly", suggested_object_id="climacell_hourly", @@ -89,7 +96,7 @@ async def test_climacell_migration_logic( old_entity_nowcast = ent_reg.async_get_or_create( "weather", CC_DOMAIN, - f"{_get_unique_id(hass, old_data)}_nowcast", + "v3apikey_80.0_80.0_nowcast", config_entry=old_config_entry, original_name="ClimaCell - Nowcast", suggested_object_id="climacell_nowcast", @@ -101,6 +108,10 @@ async def test_climacell_migration_logic( # climacell import and see what happens - we are also changing the API key to ensure # that things work as expected new_data = API_V3_ENTRY_DATA.copy() + new_data[CONF_LOCATION] = { + CONF_LATITUDE: float(new_data.pop(CONF_LATITUDE)), + CONF_LONGITUDE: float(new_data.pop(CONF_LONGITUDE)), + } new_data[CONF_API_VERSION] = 4 new_data["old_config_entry_id"] = old_config_entry.entry_id config_entry = MockConfigEntry(