Use UnitOfTemperature in integrations (e-h) (#84305)

pull/84313/head
epenet 2022-12-20 18:29:16 +01:00 committed by GitHub
parent d89ba40010
commit 9580c4f1ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 113 additions and 87 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import TEMP_CELSIUS
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -31,7 +31,7 @@ class EcoalTempSensor(SensorEntity):
"""Representation of a temperature sensor using ecoal status data."""
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
def __init__(self, ecoal_contr, name, status_attr):
"""Initialize the sensor."""

View File

@ -16,7 +16,7 @@ from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
TEMP_FAHRENHEIT,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
@ -43,7 +43,7 @@ SENSOR_TYPES: tuple[EcobeeSensorEntityDescription, ...] = (
EcobeeSensorEntityDescription(
key="temperature",
name="Temperature",
native_unit_of_measurement=TEMP_FAHRENHEIT,
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
runtime_key=None,

View File

@ -13,7 +13,7 @@ from pyeconet.errors import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, TEMP_FAHRENHEIT, Platform
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
@ -156,4 +156,4 @@ class EcoNetEntity(Entity):
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_FAHRENHEIT
return UnitOfTemperature.FAHRENHEIT

View File

@ -22,7 +22,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
STATE_UNKNOWN,
TEMP_CELSIUS,
UnitOfTemperature,
)
from homeassistant.core import Event, HomeAssistant
import homeassistant.helpers.config_validation as cv
@ -114,7 +114,7 @@ class EddystoneTemp(SensorEntity):
"""Representation of a temperature sensor."""
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
_attr_should_poll = False
def __init__(self, name: str, namespace: str, instance: str) -> None:

View File

@ -21,8 +21,8 @@ from homeassistant.const import (
PERCENTAGE,
STATE_CLOSED,
STATE_OPEN,
TEMP_CELSIUS,
UnitOfPower,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
@ -62,7 +62,7 @@ class EnOceanSensorEntityDescription(
SENSOR_DESC_TEMPERATURE = EnOceanSensorEntityDescription(
key=SENSOR_TYPE_TEMPERATURE,
name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
icon="mdi:thermometer",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,

View File

@ -9,8 +9,8 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
TEMP_FAHRENHEIT,
UnitOfPressure,
UnitOfTemperature,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
@ -125,7 +125,7 @@ class FloTemperatureSensor(FloEntity, SensorEntity):
"""Monitors the temperature."""
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = TEMP_FAHRENHEIT
_attr_native_unit_of_measurement = UnitOfTemperature.FAHRENHEIT
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
def __init__(self, name, device):

View File

@ -23,7 +23,7 @@ from homeassistant.const import (
CONF_TOKEN,
CONF_USERNAME,
PERCENTAGE,
TEMP_CELSIUS,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady
@ -52,7 +52,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="tmp",
name=ATTR_TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
),
SensorEntityDescription(

View File

@ -7,7 +7,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS
from homeassistant.const import LIGHT_LUX, PERCENTAGE, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -27,7 +27,7 @@ STATE_CLASS_MAP = {
"lightSensor": None,
}
UNIT_MAP = {
"temperatureSensor": TEMP_CELSIUS,
"temperatureSensor": UnitOfTemperature.CELSIUS,
"humiditySensor": PERCENTAGE,
"lightSensor": LIGHT_LUX,
}

View File

@ -14,7 +14,7 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
TEMP_CELSIUS,
UnitOfTemperature,
)
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
@ -62,7 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
def _update_unique_id(entry: RegistryEntry) -> dict[str, str] | None:
"""Update unique ID of entity entry."""
if (
entry.unit_of_measurement == TEMP_CELSIUS
entry.unit_of_measurement == UnitOfTemperature.CELSIUS
and "_temperature" not in entry.unique_id
):
new_unique_id = f"{entry.unique_id}_temperature"

View File

@ -18,11 +18,11 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
TEMP_CELSIUS,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfPower,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
@ -103,7 +103,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
FritzSensorEntityDescription(
key="temperature",
name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
@ -168,7 +168,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
FritzSensorEntityDescription(
key="comfort_temperature",
name="Comfort Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
suitable=suitable_comfort_temperature,
native_value=lambda device: device.comfort_temperature, # type: ignore[no-any-return]
@ -176,7 +176,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
FritzSensorEntityDescription(
key="eco_temperature",
name="Eco Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
suitable=suitable_eco_temperature,
native_value=lambda device: device.eco_temperature, # type: ignore[no-any-return]
@ -184,7 +184,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
FritzSensorEntityDescription(
key="nextchange_temperature",
name="Next Scheduled Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
suitable=suitable_nextchange_temperature,
native_value=lambda device: device.nextchange_temperature, # type: ignore[no-any-return]

View File

@ -17,8 +17,8 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_TOKEN,
CONF_USERNAME,
TEMP_CELSIUS,
Platform,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv, entity_registry as er
@ -356,7 +356,7 @@ class GeniusHeatingZone(GeniusZone):
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return TEMP_CELSIUS
return UnitOfTemperature.CELSIUS
async def async_set_temperature(self, **kwargs) -> None:
"""Set a new target temperature for this zone."""

View File

@ -16,9 +16,9 @@ from homeassistant.const import (
PERCENTAGE,
REVOLUTIONS_PER_MINUTE,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
Platform,
UnitOfInformation,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry
@ -171,7 +171,7 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="temperature_core",
type="sensors",
name_suffix="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
@ -179,7 +179,7 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="temperature_hdd",
type="sensors",
name_suffix="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),

View File

@ -13,11 +13,11 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS,
TEMP_CELSIUS,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfPower,
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
@ -97,7 +97,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
key="temperature",
name="Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(

View File

@ -11,7 +11,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
from homeassistant.const import PERCENTAGE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -96,7 +96,7 @@ class DoorSensorTemperature(DoorSensorEntity):
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
def __init__(
self,

View File

@ -63,8 +63,7 @@ from homeassistant.const import (
STATE_STANDBY,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature,
)
from homeassistant.core import DOMAIN as HA_DOMAIN
from homeassistant.helpers.network import get_url
@ -175,7 +174,7 @@ def register_trait(trait):
def _google_temp_unit(units):
"""Return Google temperature unit."""
if units == TEMP_FAHRENHEIT:
if units == UnitOfTemperature.FAHRENHEIT:
return "F"
return "C"
@ -845,7 +844,10 @@ class TemperatureControlTrait(_Trait):
current_temp = self.state.state
if current_temp not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
temp = round(
TemperatureConverter.convert(float(current_temp), unit, TEMP_CELSIUS), 1
TemperatureConverter.convert(
float(current_temp), unit, UnitOfTemperature.CELSIUS
),
1,
)
response["temperatureSetpointCelsius"] = temp
response["temperatureAmbientCelsius"] = temp
@ -951,7 +953,10 @@ class TemperatureSettingTrait(_Trait):
current_temp = attrs.get(climate.ATTR_CURRENT_TEMPERATURE)
if current_temp is not None:
response["thermostatTemperatureAmbient"] = round(
TemperatureConverter.convert(current_temp, unit, TEMP_CELSIUS), 1
TemperatureConverter.convert(
current_temp, unit, UnitOfTemperature.CELSIUS
),
1,
)
current_humidity = attrs.get(climate.ATTR_CURRENT_HUMIDITY)
@ -962,27 +967,37 @@ class TemperatureSettingTrait(_Trait):
if supported & climate.SUPPORT_TARGET_TEMPERATURE_RANGE:
response["thermostatTemperatureSetpointHigh"] = round(
TemperatureConverter.convert(
attrs[climate.ATTR_TARGET_TEMP_HIGH], unit, TEMP_CELSIUS
attrs[climate.ATTR_TARGET_TEMP_HIGH],
unit,
UnitOfTemperature.CELSIUS,
),
1,
)
response["thermostatTemperatureSetpointLow"] = round(
TemperatureConverter.convert(
attrs[climate.ATTR_TARGET_TEMP_LOW], unit, TEMP_CELSIUS
attrs[climate.ATTR_TARGET_TEMP_LOW],
unit,
UnitOfTemperature.CELSIUS,
),
1,
)
else:
if (target_temp := attrs.get(ATTR_TEMPERATURE)) is not None:
target_temp = round(
TemperatureConverter.convert(target_temp, unit, TEMP_CELSIUS), 1
TemperatureConverter.convert(
target_temp, unit, UnitOfTemperature.CELSIUS
),
1,
)
response["thermostatTemperatureSetpointHigh"] = target_temp
response["thermostatTemperatureSetpointLow"] = target_temp
else:
if (target_temp := attrs.get(ATTR_TEMPERATURE)) is not None:
response["thermostatTemperatureSetpoint"] = round(
TemperatureConverter.convert(target_temp, unit, TEMP_CELSIUS), 1
TemperatureConverter.convert(
target_temp, unit, UnitOfTemperature.CELSIUS
),
1,
)
return response
@ -996,9 +1011,9 @@ class TemperatureSettingTrait(_Trait):
if command == COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT:
temp = TemperatureConverter.convert(
params["thermostatTemperatureSetpoint"], TEMP_CELSIUS, unit
params["thermostatTemperatureSetpoint"], UnitOfTemperature.CELSIUS, unit
)
if unit == TEMP_FAHRENHEIT:
if unit == UnitOfTemperature.FAHRENHEIT:
temp = round(temp)
if temp < min_temp or temp > max_temp:
@ -1017,9 +1032,11 @@ class TemperatureSettingTrait(_Trait):
elif command == COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE:
temp_high = TemperatureConverter.convert(
params["thermostatTemperatureSetpointHigh"], TEMP_CELSIUS, unit
params["thermostatTemperatureSetpointHigh"],
UnitOfTemperature.CELSIUS,
unit,
)
if unit == TEMP_FAHRENHEIT:
if unit == UnitOfTemperature.FAHRENHEIT:
temp_high = round(temp_high)
if temp_high < min_temp or temp_high > max_temp:
@ -1032,9 +1049,11 @@ class TemperatureSettingTrait(_Trait):
)
temp_low = TemperatureConverter.convert(
params["thermostatTemperatureSetpointLow"], TEMP_CELSIUS, unit
params["thermostatTemperatureSetpointLow"],
UnitOfTemperature.CELSIUS,
unit,
)
if unit == TEMP_FAHRENHEIT:
if unit == UnitOfTemperature.FAHRENHEIT:
temp_low = round(temp_low)
if temp_low < min_temp or temp_low > max_temp:

View File

@ -22,7 +22,7 @@ from homeassistant.components.sensor import (
from homeassistant.const import (
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
TEMP_CELSIUS,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -34,7 +34,7 @@ SENSOR_DESCRIPTIONS = {
(DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
key=f"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
(DeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(

View File

@ -3,12 +3,12 @@ from __future__ import annotations
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import (
TEMP_CELSIUS,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfTemperature,
)
from .sensor_entity_description import GrowattSensorEntityDescription
@ -155,7 +155,7 @@ INVERTER_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="inverter_ipm_temperature",
name="Intelligent Power Management temperature",
api_key="ipmTemperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),
@ -163,7 +163,7 @@ INVERTER_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="inverter_temperature",
name="Temperature",
api_key="temperature",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),

View File

@ -8,12 +8,12 @@ from __future__ import annotations
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import (
PERCENTAGE,
TEMP_CELSIUS,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfTemperature,
)
from .sensor_entity_description import GrowattSensorEntityDescription
@ -255,7 +255,7 @@ TLX_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="tlx_temperature_1",
name="Temperature 1",
api_key="temp1",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),
@ -263,7 +263,7 @@ TLX_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="tlx_temperature_2",
name="Temperature 2",
api_key="temp2",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),
@ -271,7 +271,7 @@ TLX_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="tlx_temperature_3",
name="Temperature 3",
api_key="temp3",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),
@ -279,7 +279,7 @@ TLX_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="tlx_temperature_4",
name="Temperature 4",
api_key="temp4",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),
@ -287,7 +287,7 @@ TLX_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="tlx_temperature_5",
name="Temperature 5",
api_key="temp5",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
precision=1,
),

View File

@ -10,7 +10,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_FAHRENHEIT, UnitOfElectricPotential, UnitOfTime
from homeassistant.const import UnitOfElectricPotential, UnitOfTemperature, UnitOfTime
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
@ -54,7 +54,7 @@ PAIRED_SENSOR_DESCRIPTIONS = (
key=SENSOR_KIND_TEMPERATURE,
name="Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_FAHRENHEIT,
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
state_class=SensorStateClass.MEASUREMENT,
),
)
@ -63,7 +63,7 @@ VALVE_CONTROLLER_DESCRIPTIONS = (
key=SENSOR_KIND_TEMPERATURE,
name="Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_FAHRENHEIT,
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
state_class=SensorStateClass.MEASUREMENT,
api_category=API_SYSTEM_ONBOARD_SENSOR_STATUS,
),

View File

@ -18,8 +18,7 @@ from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PORT,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
@ -99,9 +98,9 @@ class HddTempSensor(SensorEntity):
self._details = self.hddtemp.data[self.disk].split("|")
self._attr_native_value = self._details[2]
if self._details is not None and self._details[3] == "F":
self._attr_native_unit_of_measurement = TEMP_FAHRENHEIT
self._attr_native_unit_of_measurement = UnitOfTemperature.FAHRENHEIT
else:
self._attr_native_unit_of_measurement = TEMP_CELSIUS
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
else:
self._attr_native_value = None

View File

@ -35,8 +35,7 @@ from homeassistant.const import (
PERCENTAGE,
STATE_ON,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature,
__version__,
)
from homeassistant.core import (
@ -184,8 +183,8 @@ def get_accessory( # noqa: C901
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
if device_class == SensorDeviceClass.TEMPERATURE or unit in (
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature.CELSIUS,
UnitOfTemperature.FAHRENHEIT,
):
a_type = "TemperatureSensor"
elif device_class == SensorDeviceClass.HUMIDITY and unit == PERCENTAGE:

View File

@ -13,7 +13,7 @@ from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
STATE_HOME,
STATE_ON,
TEMP_CELSIUS,
UnitOfTemperature,
)
from homeassistant.core import callback
@ -124,7 +124,9 @@ class TemperatureSensor(HomeAccessory):
@callback
def async_update_state(self, new_state):
"""Update temperature after state changed."""
unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS)
unit = new_state.attributes.get(
ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature.CELSIUS
)
if (temperature := convert_to_float(new_state.state)) is not None:
temperature = temperature_to_homekit(temperature, unit)
self.char_temp.set_value(temperature)

View File

@ -53,8 +53,7 @@ from homeassistant.const import (
ATTR_SUPPORTED_FEATURES,
ATTR_TEMPERATURE,
PERCENTAGE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature,
)
from homeassistant.core import State, callback
from homeassistant.util.percentage import (
@ -98,7 +97,7 @@ DEFAULT_HVAC_MODES = [
]
HC_HOMEKIT_VALID_MODES_WATER_HEATER = {"Heat": 1}
UNIT_HASS_TO_HOMEKIT = {TEMP_CELSIUS: 0, TEMP_FAHRENHEIT: 1}
UNIT_HASS_TO_HOMEKIT = {UnitOfTemperature.CELSIUS: 0, UnitOfTemperature.FAHRENHEIT: 1}
HC_HEAT_COOL_OFF = 0
HC_HEAT_COOL_HEAT = 1

View File

@ -35,7 +35,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_PORT,
CONF_TYPE,
TEMP_CELSIUS,
UnitOfTemperature,
)
from homeassistant.core import Event, HomeAssistant, State, callback, split_entity_id
import homeassistant.helpers.config_validation as cv
@ -391,12 +391,20 @@ def cleanup_name_for_homekit(name: str | None) -> str:
def temperature_to_homekit(temperature: float | int, unit: str) -> float:
"""Convert temperature to Celsius for HomeKit."""
return round(TemperatureConverter.convert(temperature, unit, TEMP_CELSIUS), 1)
return round(
TemperatureConverter.convert(temperature, unit, UnitOfTemperature.CELSIUS), 1
)
def temperature_to_states(temperature: float | int, unit: str) -> float:
"""Convert temperature back from Celsius to Home Assistant unit."""
return round(TemperatureConverter.convert(temperature, TEMP_CELSIUS, unit) * 2) / 2
return (
round(
TemperatureConverter.convert(temperature, UnitOfTemperature.CELSIUS, unit)
* 2
)
/ 2
)
def density_to_air_quality(density: float) -> int:

View File

@ -26,7 +26,6 @@ from homeassistant.const import (
LIGHT_LUX,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
TEMP_CELSIUS,
Platform,
UnitOfElectricCurrent,
UnitOfElectricPotential,
@ -34,6 +33,7 @@ from homeassistant.const import (
UnitOfPower,
UnitOfPressure,
UnitOfSoundPressure,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
@ -230,7 +230,7 @@ SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
name="Current Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
# This sensor is only for temperature characteristics that are not part
# of a temperature sensor service.
probe=(lambda char: char.service.type != ServicesTypes.TEMPERATURE_SENSOR),
@ -380,7 +380,7 @@ class HomeKitTemperatureSensor(HomeKitSensor):
"""Representation of a Homekit temperature sensor."""
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
def get_characteristic_types(self) -> list[str]:
"""Define the homekit characteristics the entity is tracking."""

View File

@ -17,7 +17,6 @@ from homeassistant.const import (
DEGREE,
LIGHT_LUX,
PERCENTAGE,
TEMP_CELSIUS,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
@ -26,6 +25,7 @@ from homeassistant.const import (
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
@ -63,13 +63,13 @@ SENSOR_DESCRIPTIONS: dict[str, SensorEntityDescription] = {
),
"ACTUAL_TEMPERATURE": SensorEntityDescription(
key="ACTUAL_TEMPERATURE",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
"TEMPERATURE": SensorEntityDescription(
key="TEMPERATURE",
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),

View File

@ -11,7 +11,7 @@ from homeassistant.components.sensor import (
SensorEntity,
SensorStateClass,
)
from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS
from homeassistant.const import LIGHT_LUX, PERCENTAGE, UnitOfTemperature
from homeassistant.helpers.entity import EntityCategory
from ..const import DOMAIN as HUE_DOMAIN
@ -76,7 +76,7 @@ class HueTemperature(GenericHueGaugeSensorEntity):
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
@property
def native_value(self):

View File

@ -23,7 +23,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS
from homeassistant.const import LIGHT_LUX, PERCENTAGE, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -96,7 +96,7 @@ class HueSensorBase(HueBaseEntity, SensorEntity):
class HueTemperatureSensor(HueSensorBase):
"""Representation of a Hue Temperature sensor."""
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
_attr_device_class = SensorDeviceClass.TEMPERATURE
@property