mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
parent
128ccbaa57
commit
551d52103d
@ -7,110 +7,28 @@ from math import ceil
|
|||||||
|
|
||||||
import pytankerkoenig
|
import pytankerkoenig
|
||||||
from requests.exceptions import RequestException
|
from requests.exceptions import RequestException
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import ATTR_ID, CONF_API_KEY, CONF_SHOW_ON_MAP, Platform
|
||||||
ATTR_ID,
|
|
||||||
CONF_API_KEY,
|
|
||||||
CONF_LATITUDE,
|
|
||||||
CONF_LOCATION,
|
|
||||||
CONF_LONGITUDE,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_RADIUS,
|
|
||||||
CONF_SCAN_INTERVAL,
|
|
||||||
CONF_SHOW_ON_MAP,
|
|
||||||
Platform,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.typing import ConfigType
|
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
UpdateFailed,
|
UpdateFailed,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import CONF_FUEL_TYPES, CONF_STATIONS, DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||||
CONF_FUEL_TYPES,
|
|
||||||
CONF_STATIONS,
|
|
||||||
DEFAULT_RADIUS,
|
|
||||||
DEFAULT_SCAN_INTERVAL,
|
|
||||||
DOMAIN,
|
|
||||||
FUEL_TYPES,
|
|
||||||
)
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
|
||||||
vol.All(
|
|
||||||
cv.deprecated(DOMAIN),
|
|
||||||
{
|
|
||||||
DOMAIN: vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
|
||||||
vol.Optional(
|
|
||||||
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
|
|
||||||
): cv.time_period,
|
|
||||||
vol.Optional(CONF_FUEL_TYPES, default=FUEL_TYPES): vol.All(
|
|
||||||
cv.ensure_list, [vol.In(FUEL_TYPES)]
|
|
||||||
),
|
|
||||||
vol.Inclusive(
|
|
||||||
CONF_LATITUDE,
|
|
||||||
"coordinates",
|
|
||||||
"Latitude and longitude must exist together",
|
|
||||||
): cv.latitude,
|
|
||||||
vol.Inclusive(
|
|
||||||
CONF_LONGITUDE,
|
|
||||||
"coordinates",
|
|
||||||
"Latitude and longitude must exist together",
|
|
||||||
): cv.longitude,
|
|
||||||
vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): vol.All(
|
|
||||||
cv.positive_int, vol.Range(min=1)
|
|
||||||
),
|
|
||||||
vol.Optional(CONF_STATIONS, default=[]): vol.All(
|
|
||||||
cv.ensure_list, [cv.string]
|
|
||||||
),
|
|
||||||
vol.Optional(CONF_SHOW_ON_MAP, default=True): cv.boolean,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
extra=vol.ALLOW_EXTRA,
|
|
||||||
)
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
||||||
"""Set the tankerkoenig component up."""
|
|
||||||
if DOMAIN not in config:
|
|
||||||
return True
|
|
||||||
|
|
||||||
conf = config[DOMAIN]
|
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": SOURCE_IMPORT},
|
|
||||||
data={
|
|
||||||
CONF_NAME: "Home",
|
|
||||||
CONF_API_KEY: conf[CONF_API_KEY],
|
|
||||||
CONF_FUEL_TYPES: conf[CONF_FUEL_TYPES],
|
|
||||||
CONF_LOCATION: {
|
|
||||||
"latitude": conf.get(CONF_LATITUDE, hass.config.latitude),
|
|
||||||
"longitude": conf.get(CONF_LONGITUDE, hass.config.longitude),
|
|
||||||
},
|
|
||||||
CONF_RADIUS: conf[CONF_RADIUS],
|
|
||||||
CONF_STATIONS: conf[CONF_STATIONS],
|
|
||||||
CONF_SHOW_ON_MAP: conf[CONF_SHOW_ON_MAP],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
@ -67,37 +67,6 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return OptionsFlowHandler(config_entry)
|
return OptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
|
|
||||||
"""Import YAML configuration."""
|
|
||||||
await self.async_set_unique_id(
|
|
||||||
f"{config[CONF_LOCATION][CONF_LATITUDE]}_{config[CONF_LOCATION][CONF_LONGITUDE]}"
|
|
||||||
)
|
|
||||||
self._abort_if_unique_id_configured()
|
|
||||||
|
|
||||||
selected_station_ids: list[str] = []
|
|
||||||
# add all nearby stations
|
|
||||||
nearby_stations = await async_get_nearby_stations(self.hass, config)
|
|
||||||
for station in nearby_stations.get("stations", []):
|
|
||||||
selected_station_ids.append(station["id"])
|
|
||||||
|
|
||||||
# add all manual added stations
|
|
||||||
for station_id in config[CONF_STATIONS]:
|
|
||||||
selected_station_ids.append(station_id)
|
|
||||||
|
|
||||||
return self._create_entry(
|
|
||||||
data={
|
|
||||||
CONF_NAME: "Home",
|
|
||||||
CONF_API_KEY: config[CONF_API_KEY],
|
|
||||||
CONF_FUEL_TYPES: config[CONF_FUEL_TYPES],
|
|
||||||
CONF_LOCATION: config[CONF_LOCATION],
|
|
||||||
CONF_RADIUS: config[CONF_RADIUS],
|
|
||||||
CONF_STATIONS: selected_station_ids,
|
|
||||||
},
|
|
||||||
options={
|
|
||||||
CONF_SHOW_ON_MAP: config[CONF_SHOW_ON_MAP],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
|
@ -8,7 +8,7 @@ from homeassistant.components.tankerkoenig.const import (
|
|||||||
CONF_STATIONS,
|
CONF_STATIONS,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
@ -47,18 +47,6 @@ MOCK_OPTIONS_DATA = {
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
MOCK_IMPORT_DATA = {
|
|
||||||
CONF_API_KEY: "269534f6-xxxx-xxxx-xxxx-yyyyzzzzxxxx",
|
|
||||||
CONF_FUEL_TYPES: ["e5"],
|
|
||||||
CONF_LOCATION: {CONF_LATITUDE: 51.0, CONF_LONGITUDE: 13.0},
|
|
||||||
CONF_RADIUS: 2.0,
|
|
||||||
CONF_STATIONS: [
|
|
||||||
"3bcd61da-yyyy-yyyy-yyyy-19d5523a7ae8",
|
|
||||||
"36b4b812-yyyy-yyyy-yyyy-c51735325858",
|
|
||||||
],
|
|
||||||
CONF_SHOW_ON_MAP: True,
|
|
||||||
}
|
|
||||||
|
|
||||||
MOCK_NEARVY_STATIONS_OK = {
|
MOCK_NEARVY_STATIONS_OK = {
|
||||||
"ok": True,
|
"ok": True,
|
||||||
"stations": [
|
"stations": [
|
||||||
@ -187,37 +175,6 @@ async def test_user_no_stations(hass: HomeAssistant):
|
|||||||
assert result["errors"][CONF_RADIUS] == "no_stations"
|
assert result["errors"][CONF_RADIUS] == "no_stations"
|
||||||
|
|
||||||
|
|
||||||
async def test_import(hass: HomeAssistant):
|
|
||||||
"""Test starting a flow by import."""
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tankerkoenig.async_setup_entry", return_value=True
|
|
||||||
) as mock_setup_entry, patch(
|
|
||||||
"homeassistant.components.tankerkoenig.config_flow.getNearbyStations",
|
|
||||||
return_value=MOCK_NEARVY_STATIONS_OK,
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=MOCK_IMPORT_DATA
|
|
||||||
)
|
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["data"][CONF_NAME] == "Home"
|
|
||||||
assert result["data"][CONF_API_KEY] == "269534f6-xxxx-xxxx-xxxx-yyyyzzzzxxxx"
|
|
||||||
assert result["data"][CONF_FUEL_TYPES] == ["e5"]
|
|
||||||
assert result["data"][CONF_LOCATION] == {"latitude": 51.0, "longitude": 13.0}
|
|
||||||
assert result["data"][CONF_RADIUS] == 2.0
|
|
||||||
assert result["data"][CONF_STATIONS] == [
|
|
||||||
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
|
|
||||||
"36b4b812-xxxx-xxxx-xxxx-c51735325858",
|
|
||||||
"3bcd61da-yyyy-yyyy-yyyy-19d5523a7ae8",
|
|
||||||
"36b4b812-yyyy-yyyy-yyyy-c51735325858",
|
|
||||||
]
|
|
||||||
assert result["options"][CONF_SHOW_ON_MAP]
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert mock_setup_entry.called
|
|
||||||
|
|
||||||
|
|
||||||
async def test_reauth(hass: HomeAssistant):
|
async def test_reauth(hass: HomeAssistant):
|
||||||
"""Test starting a flow by user to re-auth."""
|
"""Test starting a flow by user to re-auth."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user