From 1445e08090c5a0ae92be1099308a4bc07c31b7b0 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 14 Oct 2022 12:22:11 +0200 Subject: [PATCH] Replace `is_metric` with `is METRIC_SYSTEM` (#80262) --- homeassistant/components/accuweather/__init__.py | 3 ++- homeassistant/components/accuweather/sensor.py | 5 +++-- homeassistant/components/accuweather/weather.py | 3 ++- homeassistant/components/airthings_ble/__init__.py | 3 ++- homeassistant/components/airthings_ble/sensor.py | 3 ++- homeassistant/components/bloomsky/__init__.py | 3 ++- homeassistant/components/darksky/sensor.py | 3 ++- homeassistant/components/fitbit/sensor.py | 7 ++++--- homeassistant/components/life360/coordinator.py | 3 ++- homeassistant/components/magicseaweed/sensor.py | 3 ++- homeassistant/components/met/__init__.py | 3 ++- homeassistant/components/met/weather.py | 8 ++++++-- homeassistant/components/mold_indicator/sensor.py | 3 ++- homeassistant/components/mysensors/climate.py | 5 ++++- homeassistant/components/mysensors/gateway.py | 3 ++- homeassistant/components/mysensors/sensor.py | 3 ++- homeassistant/components/noaa_tides/sensor.py | 3 ++- homeassistant/components/roomba/irobot_base.py | 3 ++- homeassistant/components/shelly/__init__.py | 3 ++- 19 files changed, 47 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/accuweather/__init__.py b/homeassistant/components/accuweather/__init__.py index 0484dd0c8e7..89af284f873 100644 --- a/homeassistant/components/accuweather/__init__.py +++ b/homeassistant/components/accuweather/__init__.py @@ -17,6 +17,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.util.unit_system import METRIC_SYSTEM from .const import ATTR_FORECAST, CONF_FORECAST, DOMAIN, MANUFACTURER @@ -116,7 +117,7 @@ class AccuWeatherDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): current = await self.accuweather.async_get_current_conditions() forecast = ( await self.accuweather.async_get_forecast( - metric=self.hass.config.units.is_metric + metric=self.hass.config.units is METRIC_SYSTEM ) if self.forecast else {} diff --git a/homeassistant/components/accuweather/sensor.py b/homeassistant/components/accuweather/sensor.py index 4347bca5863..0238026c1a0 100644 --- a/homeassistant/components/accuweather/sensor.py +++ b/homeassistant/components/accuweather/sensor.py @@ -30,6 +30,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.util.unit_system import METRIC_SYSTEM from . import AccuWeatherDataUpdateCoordinator from .const import ( @@ -412,12 +413,12 @@ class AccuWeatherSensor( self._attr_unique_id = ( f"{coordinator.location_key}-{description.key}".lower() ) - if self.coordinator.hass.config.units.is_metric: + if self.coordinator.hass.config.units is METRIC_SYSTEM: self._unit_system = API_METRIC else: self._unit_system = API_IMPERIAL self._attr_native_unit_of_measurement = self.entity_description.unit_fn( - self.coordinator.hass.config.units.is_metric + self.coordinator.hass.config.units is METRIC_SYSTEM ) self._attr_device_info = coordinator.device_info if forecast_day is not None: diff --git a/homeassistant/components/accuweather/weather.py b/homeassistant/components/accuweather/weather.py index 2bbbe7b9160..82db25288b8 100644 --- a/homeassistant/components/accuweather/weather.py +++ b/homeassistant/components/accuweather/weather.py @@ -33,6 +33,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util.dt import utc_from_timestamp +from homeassistant.util.unit_system import METRIC_SYSTEM from . import AccuWeatherDataUpdateCoordinator from .const import ( @@ -70,7 +71,7 @@ class AccuWeatherEntity( # Coordinator data is used also for sensors which don't have units automatically # converted, hence the weather entity's native units follow the configured unit # system - if coordinator.hass.config.units.is_metric: + if coordinator.hass.config.units is METRIC_SYSTEM: self._attr_native_precipitation_unit = LENGTH_MILLIMETERS self._attr_native_pressure_unit = PRESSURE_HPA self._attr_native_temperature_unit = TEMP_CELSIUS diff --git a/homeassistant/components/airthings_ble/__init__.py b/homeassistant/components/airthings_ble/__init__.py index 4e066ea8447..d7e6bddbcd4 100644 --- a/homeassistant/components/airthings_ble/__init__.py +++ b/homeassistant/components/airthings_ble/__init__.py @@ -12,6 +12,7 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.util.unit_system import METRIC_SYSTEM from .const import DEFAULT_SCAN_INTERVAL, DOMAIN @@ -26,7 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: address = entry.unique_id elevation = hass.config.elevation - is_metric = hass.config.units.is_metric + is_metric = hass.config.units is METRIC_SYSTEM assert address is not None ble_device = bluetooth.async_ble_device_from_address(hass, address) diff --git a/homeassistant/components/airthings_ble/sensor.py b/homeassistant/components/airthings_ble/sensor.py index 0f0ca2e4af5..37b5ce6160e 100644 --- a/homeassistant/components/airthings_ble/sensor.py +++ b/homeassistant/components/airthings_ble/sensor.py @@ -29,6 +29,7 @@ from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, ) +from homeassistant.util.unit_system import METRIC_SYSTEM from .const import DOMAIN, VOLUME_BECQUEREL, VOLUME_PICOCURIE @@ -112,7 +113,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Airthings BLE sensors.""" - is_metric = hass.config.units.is_metric + is_metric = hass.config.units is METRIC_SYSTEM coordinator: DataUpdateCoordinator[AirthingsDevice] = hass.data[DOMAIN][ entry.entry_id diff --git a/homeassistant/components/bloomsky/__init__.py b/homeassistant/components/bloomsky/__init__.py index ed2ce1ebc70..5b069cacdb3 100644 --- a/homeassistant/components/bloomsky/__init__.py +++ b/homeassistant/components/bloomsky/__init__.py @@ -12,6 +12,7 @@ from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType from homeassistant.util import Throttle +from homeassistant.util.unit_system import METRIC_SYSTEM _LOGGER = logging.getLogger(__name__) @@ -33,7 +34,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: api_key = config[DOMAIN][CONF_API_KEY] try: - bloomsky = BloomSky(api_key, hass.config.units.is_metric) + bloomsky = BloomSky(api_key, hass.config.units is METRIC_SYSTEM) except RuntimeError: return False diff --git a/homeassistant/components/darksky/sensor.py b/homeassistant/components/darksky/sensor.py index ccd4516e39c..663b92a7ccf 100644 --- a/homeassistant/components/darksky/sensor.py +++ b/homeassistant/components/darksky/sensor.py @@ -45,6 +45,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import Throttle +from homeassistant.util.unit_system import METRIC_SYSTEM _LOGGER = logging.getLogger(__name__) @@ -584,7 +585,7 @@ def setup_platform( if CONF_UNITS in config: units = config[CONF_UNITS] - elif hass.config.units.is_metric: + elif hass.config.units is METRIC_SYSTEM: units = "si" else: units = "us" diff --git a/homeassistant/components/fitbit/sensor.py b/homeassistant/components/fitbit/sensor.py index f9dc74fc328..c4e9970691d 100644 --- a/homeassistant/components/fitbit/sensor.py +++ b/homeassistant/components/fitbit/sensor.py @@ -32,6 +32,7 @@ from homeassistant.helpers.icon import icon_for_battery_level from homeassistant.helpers.network import NoURLAvailableError, get_url from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util.json import load_json, save_json +from homeassistant.util.unit_system import METRIC_SYSTEM from .const import ( ATTR_ACCESS_TOKEN, @@ -199,7 +200,7 @@ def setup_platform( if (unit_system := config[CONF_UNIT_SYSTEM]) == "default": authd_client.system = user_profile["locale"] if authd_client.system != "en_GB": - if hass.config.units.is_metric: + if hass.config.units is METRIC_SYSTEM: authd_client.system = "metric" else: authd_client.system = "en_US" @@ -215,7 +216,7 @@ def setup_platform( user_profile, config_path, description, - hass.config.units.is_metric, + hass.config.units is METRIC_SYSTEM, clock_format, ) for description in FITBIT_RESOURCES_LIST @@ -229,7 +230,7 @@ def setup_platform( user_profile, config_path, FITBIT_RESOURCE_BATTERY, - hass.config.units.is_metric, + hass.config.units is METRIC_SYSTEM, clock_format, dev_extra, ) diff --git a/homeassistant/components/life360/coordinator.py b/homeassistant/components/life360/coordinator.py index edb86e9727a..ba3e7672cdb 100644 --- a/homeassistant/components/life360/coordinator.py +++ b/homeassistant/components/life360/coordinator.py @@ -23,6 +23,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed 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 ( COMM_TIMEOUT, @@ -206,7 +207,7 @@ class Life360DataUpdateCoordinator(DataUpdateCoordinator[Life360Data]): address = address1 or address2 speed = max(0, float(loc["speed"]) * SPEED_FACTOR_MPH) - if self._hass.config.units.is_metric: + if self._hass.config.units is METRIC_SYSTEM: speed = DistanceConverter.convert( speed, LENGTH_MILES, LENGTH_KILOMETERS ) diff --git a/homeassistant/components/magicseaweed/sensor.py b/homeassistant/components/magicseaweed/sensor.py index b0b2e92ada5..13bc8b9be2e 100644 --- a/homeassistant/components/magicseaweed/sensor.py +++ b/homeassistant/components/magicseaweed/sensor.py @@ -24,6 +24,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import Throttle import homeassistant.util.dt as dt_util +from homeassistant.util.unit_system import METRIC_SYSTEM _LOGGER = logging.getLogger(__name__) @@ -92,7 +93,7 @@ def setup_platform( if CONF_UNITS in config: units = config.get(CONF_UNITS) - elif hass.config.units.is_metric: + elif hass.config.units is METRIC_SYSTEM: units = UNITS[0] else: units = UNITS[2] diff --git a/homeassistant/components/met/__init__.py b/homeassistant/components/met/__init__.py index 2857c057482..fc79ac4c60f 100644 --- a/homeassistant/components/met/__init__.py +++ b/homeassistant/components/met/__init__.py @@ -26,6 +26,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed 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 ( CONF_TRACK_HOME, @@ -95,7 +96,7 @@ class MetDataUpdateCoordinator(DataUpdateCoordinator["MetWeatherData"]): """Initialize global Met data updater.""" self._unsub_track_home: Callable[[], None] | None = None self.weather = MetWeatherData( - hass, config_entry.data, hass.config.units.is_metric + hass, config_entry.data, hass.config.units is METRIC_SYSTEM ) self.weather.set_coordinates() diff --git a/homeassistant/components/met/weather.py b/homeassistant/components/met/weather.py index c843be73fe7..2aa70929795 100644 --- a/homeassistant/components/met/weather.py +++ b/homeassistant/components/met/weather.py @@ -30,6 +30,7 @@ from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.util.unit_system import METRIC_SYSTEM from . import MetDataUpdateCoordinator from .const import ATTR_MAP, CONDITIONS_MAP, CONF_TRACK_HOME, DOMAIN, FORECAST_MAP @@ -51,10 +52,13 @@ async def async_setup_entry( async_add_entities( [ MetWeather( - coordinator, config_entry.data, hass.config.units.is_metric, False + coordinator, + config_entry.data, + hass.config.units is METRIC_SYSTEM, + False, ), MetWeather( - coordinator, config_entry.data, hass.config.units.is_metric, True + coordinator, config_entry.data, hass.config.units is METRIC_SYSTEM, True ), ] ) diff --git a/homeassistant/components/mold_indicator/sensor.py b/homeassistant/components/mold_indicator/sensor.py index 5685e76fac0..f10ddbe1ab0 100644 --- a/homeassistant/components/mold_indicator/sensor.py +++ b/homeassistant/components/mold_indicator/sensor.py @@ -23,6 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util.unit_conversion import TemperatureConverter +from homeassistant.util.unit_system import METRIC_SYSTEM _LOGGER = logging.getLogger(__name__) @@ -67,7 +68,7 @@ async def async_setup_platform( [ MoldIndicator( name, - hass.config.units.is_metric, + hass.config.units is METRIC_SYSTEM, indoor_temp_sensor, outdoor_temp_sensor, indoor_humidity_sensor, diff --git a/homeassistant/components/mysensors/climate.py b/homeassistant/components/mysensors/climate.py index 435bf2ffddb..44468d8db4c 100644 --- a/homeassistant/components/mysensors/climate.py +++ b/homeassistant/components/mysensors/climate.py @@ -20,6 +20,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.util.unit_system import METRIC_SYSTEM from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo @@ -94,7 +95,9 @@ class MySensorsHVAC(mysensors.device.MySensorsEntity, ClimateEntity): @property def temperature_unit(self) -> str: """Return the unit of measurement.""" - return TEMP_CELSIUS if self.hass.config.units.is_metric else TEMP_FAHRENHEIT + return ( + TEMP_CELSIUS if self.hass.config.units is METRIC_SYSTEM else TEMP_FAHRENHEIT + ) @property def current_temperature(self) -> float | None: diff --git a/homeassistant/components/mysensors/gateway.py b/homeassistant/components/mysensors/gateway.py index c93e0380757..eace7b355d7 100644 --- a/homeassistant/components/mysensors/gateway.py +++ b/homeassistant/components/mysensors/gateway.py @@ -22,6 +22,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import Event, HomeAssistant, callback import homeassistant.helpers.config_validation as cv +from homeassistant.util.unit_system import METRIC_SYSTEM from .const import ( CONF_BAUD_RATE, @@ -220,7 +221,7 @@ async def _get_gateway( protocol_version=version, ) gateway.event_callback = event_callback - gateway.metric = hass.config.units.is_metric + gateway.metric = hass.config.units is METRIC_SYSTEM if persistence: await gateway.start_persistence() diff --git a/homeassistant/components/mysensors/sensor.py b/homeassistant/components/mysensors/sensor.py index f21d343f9c3..5bafed04353 100644 --- a/homeassistant/components/mysensors/sensor.py +++ b/homeassistant/components/mysensors/sensor.py @@ -35,6 +35,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.util.unit_system import METRIC_SYSTEM from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo @@ -242,7 +243,7 @@ class MySensorsSensor(mysensors.device.MySensorsEntity, SensorEntity): return custom_unit if set_req(self.value_type) == set_req.V_TEMP: - if self.hass.config.units.is_metric: + if self.hass.config.units is METRIC_SYSTEM: return TEMP_CELSIUS return TEMP_FAHRENHEIT diff --git a/homeassistant/components/noaa_tides/sensor.py b/homeassistant/components/noaa_tides/sensor.py index 49635973cf8..8de16055714 100644 --- a/homeassistant/components/noaa_tides/sensor.py +++ b/homeassistant/components/noaa_tides/sensor.py @@ -20,6 +20,7 @@ from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.util.unit_system import METRIC_SYSTEM _LOGGER = logging.getLogger(__name__) @@ -57,7 +58,7 @@ def setup_platform( if CONF_UNIT_SYSTEM in config: unit_system = config[CONF_UNIT_SYSTEM] - elif hass.config.units.is_metric: + elif hass.config.units is METRIC_SYSTEM: unit_system = UNIT_SYSTEMS[1] else: unit_system = UNIT_SYSTEMS[0] diff --git a/homeassistant/components/roomba/irobot_base.py b/homeassistant/components/roomba/irobot_base.py index f443f72279f..8ec91acf965 100644 --- a/homeassistant/components/roomba/irobot_base.py +++ b/homeassistant/components/roomba/irobot_base.py @@ -17,6 +17,7 @@ from homeassistant.const import STATE_IDLE, STATE_PAUSED import homeassistant.helpers.device_registry as dr from homeassistant.helpers.entity import DeviceInfo, Entity import homeassistant.util.dt as dt_util +from homeassistant.util.unit_system import METRIC_SYSTEM from . import roomba_reported_state from .const import DOMAIN @@ -221,7 +222,7 @@ class IRobotVacuum(IRobotEntity, StateVacuumEntity): if cleaned_area := mission_state.get("sqft", 0): # Imperial # Convert to m2 if the unit_system is set to metric - if self.hass.config.units.is_metric: + if self.hass.config.units is METRIC_SYSTEM: cleaned_area = round(cleaned_area * 0.0929) return (cleaning_time, cleaned_area) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index b2b927e48b5..bc7a3f771f7 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -20,6 +20,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import aiohttp_client, device_registry import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType +from homeassistant.util.unit_system import METRIC_SYSTEM from .const import ( AIOSHELLY_DEVICE_TIMEOUT_SEC, @@ -108,7 +109,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def _async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Shelly block based device from a config entry.""" - temperature_unit = "C" if hass.config.units.is_metric else "F" + temperature_unit = "C" if hass.config.units is METRIC_SYSTEM else "F" options = aioshelly.common.ConnectionOptions( entry.data[CONF_HOST],