forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config flow to OpenSky (home-assistant#96912)
Co-authored-by: Sander <[email protected]>
- Loading branch information
Showing
15 changed files
with
443 additions
and
19 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
"""The opensky component.""" | ||
from __future__ import annotations | ||
|
||
from python_opensky import OpenSky | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
|
||
from .const import CLIENT, DOMAIN, PLATFORMS | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up opensky from a config entry.""" | ||
|
||
client = OpenSky(session=async_get_clientsession(hass)) | ||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {CLIENT: client} | ||
|
||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload opensky config entry.""" | ||
|
||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"""Config flow for OpenSky integration.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigFlow | ||
from homeassistant.const import ( | ||
CONF_LATITUDE, | ||
CONF_LONGITUDE, | ||
CONF_NAME, | ||
CONF_RADIUS, | ||
) | ||
from homeassistant.data_entry_flow import FlowResult | ||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.helpers.typing import ConfigType | ||
|
||
from .const import DEFAULT_NAME, DOMAIN | ||
from .sensor import CONF_ALTITUDE, DEFAULT_ALTITUDE | ||
|
||
|
||
class OpenSkyConfigFlowHandler(ConfigFlow, domain=DOMAIN): | ||
"""Config flow handler for OpenSky.""" | ||
|
||
async def async_step_user( | ||
self, user_input: dict[str, Any] | None = None | ||
) -> FlowResult: | ||
"""Initialize user input.""" | ||
if user_input is not None: | ||
return self.async_create_entry( | ||
title=DEFAULT_NAME, | ||
data={ | ||
CONF_LATITUDE: user_input[CONF_LATITUDE], | ||
CONF_LONGITUDE: user_input[CONF_LONGITUDE], | ||
}, | ||
options={ | ||
CONF_RADIUS: user_input[CONF_RADIUS], | ||
CONF_ALTITUDE: user_input[CONF_ALTITUDE], | ||
}, | ||
) | ||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=self.add_suggested_values_to_schema( | ||
vol.Schema( | ||
{ | ||
vol.Required(CONF_RADIUS): vol.Coerce(float), | ||
vol.Required(CONF_LATITUDE): cv.latitude, | ||
vol.Required(CONF_LONGITUDE): cv.longitude, | ||
vol.Optional(CONF_ALTITUDE): vol.Coerce(float), | ||
} | ||
), | ||
{ | ||
CONF_LATITUDE: self.hass.config.latitude, | ||
CONF_LONGITUDE: self.hass.config.longitude, | ||
CONF_ALTITUDE: DEFAULT_ALTITUDE, | ||
}, | ||
), | ||
) | ||
|
||
async def async_step_import(self, import_config: ConfigType) -> FlowResult: | ||
"""Import config from yaml.""" | ||
entry_data = { | ||
CONF_LATITUDE: import_config.get(CONF_LATITUDE, self.hass.config.latitude), | ||
CONF_LONGITUDE: import_config.get( | ||
CONF_LONGITUDE, self.hass.config.longitude | ||
), | ||
} | ||
self._async_abort_entries_match(entry_data) | ||
return self.async_create_entry( | ||
title=import_config.get(CONF_NAME, DEFAULT_NAME), | ||
data=entry_data, | ||
options={ | ||
CONF_RADIUS: import_config[CONF_RADIUS] * 1000, | ||
CONF_ALTITUDE: import_config.get(CONF_ALTITUDE, DEFAULT_ALTITUDE), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"description": "Fill in the location to track.", | ||
"data": { | ||
"name": "[%key:common::config_flow::data::api_key%]", | ||
"radius": "Radius", | ||
"latitude": "[%key:common::config_flow::data::latitude%]", | ||
"longitude": "[%key:common::config_flow::data::longitude%]", | ||
"altitude": "Altitude" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Opensky tests.""" | ||
from unittest.mock import patch | ||
|
||
|
||
def patch_setup_entry() -> bool: | ||
"""Patch interface.""" | ||
return patch( | ||
"homeassistant.components.opensky.async_setup_entry", return_value=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Configure tests for the OpenSky integration.""" | ||
from collections.abc import Awaitable, Callable | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from python_opensky import StatesResponse | ||
|
||
from homeassistant.components.opensky.const import CONF_ALTITUDE, DOMAIN | ||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.setup import async_setup_component | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
ComponentSetup = Callable[[MockConfigEntry], Awaitable[None]] | ||
|
||
|
||
@pytest.fixture(name="config_entry") | ||
def mock_config_entry() -> MockConfigEntry: | ||
"""Create OpenSky entry in Home Assistant.""" | ||
return MockConfigEntry( | ||
domain=DOMAIN, | ||
title="OpenSky", | ||
data={ | ||
CONF_LATITUDE: 0.0, | ||
CONF_LONGITUDE: 0.0, | ||
}, | ||
options={ | ||
CONF_RADIUS: 10.0, | ||
CONF_ALTITUDE: 0.0, | ||
}, | ||
) | ||
|
||
|
||
@pytest.fixture(name="setup_integration") | ||
async def mock_setup_integration( | ||
hass: HomeAssistant, | ||
) -> Callable[[MockConfigEntry], Awaitable[None]]: | ||
"""Fixture for setting up the component.""" | ||
|
||
async def func(mock_config_entry: MockConfigEntry) -> None: | ||
mock_config_entry.add_to_hass(hass) | ||
with patch( | ||
"python_opensky.OpenSky.get_states", | ||
return_value=StatesResponse(states=[], time=0), | ||
): | ||
assert await async_setup_component(hass, DOMAIN, {}) | ||
await hass.async_block_till_done() | ||
|
||
return func |
Oops, something went wrong.