mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Display unit of elevation in met config flow (#88283)
* display unit of elevation in met config flow Co-authored-by: lijake8 <lijake8@users.noreply.github.com> Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * use NumberSelector for met config flow * met remove unused is_metric param --------- Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> Co-authored-by: lijake8 <lijake8@users.noreply.github.com>
This commit is contained in:
parent
23a1a8075c
commit
e617bfb1bb
@ -18,15 +18,12 @@ from homeassistant.const import (
|
|||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
EVENT_CORE_CONFIG_UPDATE,
|
EVENT_CORE_CONFIG_UPDATE,
|
||||||
Platform,
|
Platform,
|
||||||
UnitOfLength,
|
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant
|
from homeassistant.core import Event, HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
from homeassistant.util.unit_conversion import DistanceConverter
|
|
||||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_TRACK_HOME,
|
CONF_TRACK_HOME,
|
||||||
@ -102,9 +99,7 @@ class MetDataUpdateCoordinator(DataUpdateCoordinator["MetWeatherData"]):
|
|||||||
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||||
"""Initialize global Met data updater."""
|
"""Initialize global Met data updater."""
|
||||||
self._unsub_track_home: Callable[[], None] | None = None
|
self._unsub_track_home: Callable[[], None] | None = None
|
||||||
self.weather = MetWeatherData(
|
self.weather = MetWeatherData(hass, config_entry.data)
|
||||||
hass, config_entry.data, hass.config.units is METRIC_SYSTEM
|
|
||||||
)
|
|
||||||
self.weather.set_coordinates()
|
self.weather.set_coordinates()
|
||||||
|
|
||||||
update_interval = timedelta(minutes=randrange(55, 65))
|
update_interval = timedelta(minutes=randrange(55, 65))
|
||||||
@ -142,13 +137,10 @@ class MetDataUpdateCoordinator(DataUpdateCoordinator["MetWeatherData"]):
|
|||||||
class MetWeatherData:
|
class MetWeatherData:
|
||||||
"""Keep data for Met.no weather entities."""
|
"""Keep data for Met.no weather entities."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, hass: HomeAssistant, config: MappingProxyType[str, Any]) -> None:
|
||||||
self, hass: HomeAssistant, config: MappingProxyType[str, Any], is_metric: bool
|
|
||||||
) -> None:
|
|
||||||
"""Initialise the weather entity data."""
|
"""Initialise the weather entity data."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._config = config
|
self._config = config
|
||||||
self._is_metric = is_metric
|
|
||||||
self._weather_data: metno.MetWeatherData
|
self._weather_data: metno.MetWeatherData
|
||||||
self.current_weather_data: dict = {}
|
self.current_weather_data: dict = {}
|
||||||
self.daily_forecast: list[dict] = []
|
self.daily_forecast: list[dict] = []
|
||||||
@ -165,14 +157,6 @@ class MetWeatherData:
|
|||||||
latitude = self._config[CONF_LATITUDE]
|
latitude = self._config[CONF_LATITUDE]
|
||||||
longitude = self._config[CONF_LONGITUDE]
|
longitude = self._config[CONF_LONGITUDE]
|
||||||
elevation = self._config[CONF_ELEVATION]
|
elevation = self._config[CONF_ELEVATION]
|
||||||
if not self._is_metric:
|
|
||||||
elevation = int(
|
|
||||||
round(
|
|
||||||
DistanceConverter.convert(
|
|
||||||
elevation, UnitOfLength.FEET, UnitOfLength.METERS
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
coordinates = {
|
coordinates = {
|
||||||
"lat": str(latitude),
|
"lat": str(latitude),
|
||||||
|
@ -6,10 +6,21 @@ from typing import Any
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
from homeassistant.const import (
|
||||||
|
CONF_ELEVATION,
|
||||||
|
CONF_LATITUDE,
|
||||||
|
CONF_LONGITUDE,
|
||||||
|
CONF_NAME,
|
||||||
|
UnitOfLength,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.selector import (
|
||||||
|
NumberSelector,
|
||||||
|
NumberSelectorConfig,
|
||||||
|
NumberSelectorMode,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_TRACK_HOME,
|
CONF_TRACK_HOME,
|
||||||
@ -47,7 +58,14 @@ def _get_data_schema(
|
|||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_LONGITUDE, default=hass.config.longitude
|
CONF_LONGITUDE, default=hass.config.longitude
|
||||||
): cv.longitude,
|
): cv.longitude,
|
||||||
vol.Required(CONF_ELEVATION, default=hass.config.elevation): int,
|
vol.Required(
|
||||||
|
CONF_ELEVATION, default=hass.config.elevation
|
||||||
|
): NumberSelector(
|
||||||
|
NumberSelectorConfig(
|
||||||
|
mode=NumberSelectorMode.BOX,
|
||||||
|
unit_of_measurement=UnitOfLength.METERS,
|
||||||
|
)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
# Not tracking home, default values come from config entry
|
# Not tracking home, default values come from config entry
|
||||||
@ -62,7 +80,12 @@ def _get_data_schema(
|
|||||||
): cv.longitude,
|
): cv.longitude,
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_ELEVATION, default=config_entry.data.get(CONF_ELEVATION)
|
CONF_ELEVATION, default=config_entry.data.get(CONF_ELEVATION)
|
||||||
): int,
|
): NumberSelector(
|
||||||
|
NumberSelectorConfig(
|
||||||
|
mode=NumberSelectorMode.BOX,
|
||||||
|
unit_of_measurement=UnitOfLength.METERS,
|
||||||
|
)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user