Skip to content
Snippets Groups Projects
Unverified Commit e617bfb1 authored by Chris Xiao's avatar Chris Xiao Committed by GitHub
Browse files

Display unit of elevation in met config flow (#88283)


* display unit of elevation in met config flow

Co-authored-by: default avatarlijake8 <lijake8@users.noreply.github.com>
Signed-off-by: default avatarChris Xiao <30990835+chrisx8@users.noreply.github.com>

* use NumberSelector for met config flow

* met remove unused is_metric param

---------

Signed-off-by: default avatarChris Xiao <30990835+chrisx8@users.noreply.github.com>
Co-authored-by: default avatarlijake8 <lijake8@users.noreply.github.com>
parent 23a1a807
No related branches found
No related tags found
No related merge requests found
...@@ -18,15 +18,12 @@ from homeassistant.const import ( ...@@ -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"]): ...@@ -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"]): ...@@ -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: ...@@ -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 ...@@ -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( ...@@ -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( ...@@ -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,
)
),
} }
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment