1
0
mirror of https://github.com/home-assistant/core.git synced 2025-08-13 07:20:01 +00:00
Files
.devcontainer
.github
.vscode
homeassistant
auth
backports
brands
components
generated
helpers
service_info
__init__.py
aiohttp_client.py
area_registry.py
backup.py
category_registry.py
chat_session.py
check_config.py
collection.py
condition.py
config_entry_flow.py
config_entry_oauth2_flow.py
config_validation.py
data_entry_flow.py
debounce.py
deprecation.py
device.py
device_registry.py
discovery.py
discovery_flow.py
dispatcher.py
entity.py
entity_component.py
entity_platform.py
entity_registry.py
entity_values.py
entityfilter.py
event.py
floor_registry.py
frame.py
group.py
hassio.py
http.py
httpx_client.py
icon.py
importlib.py
instance_id.py
integration_platform.py
intent.py
issue_registry.py
json.py
label_registry.py
llm.py
location.py
network.py
normalized_name_base_registry.py
ratelimit.py
recorder.py
redact.py
registry.py
reload.py
restore_state.py
schema_config_entry_flow.py
script.py
script_variables.py
selector.py
sensor.py
service.py
signal.py
significant_change.py
singleton.py
start.py
state.py
storage.py
sun.py
system_info.py
temperature.py
template.py
trace.py
translation.py
trigger.py
trigger_template_entity.py
typing.py
update_coordinator.py
scripts
util
__init__.py
__main__.py
backup_restore.py
block_async_io.py
bootstrap.py
config.py
config_entries.py
const.py
core.py
core_config.py
data_entry_flow.py
exceptions.py
loader.py
package_constraints.txt
py.typed
requirements.py
runner.py
setup.py
strings.json
machine
pylint
rootfs
script
tests
.core_files.yaml
.dockerignore
.git-blame-ignore-revs
.gitattributes
.gitignore
.hadolint.yaml
.pre-commit-config.yaml
.prettierignore
.strict-typing
.yamllint
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
Dockerfile.dev
LICENSE.md
MANIFEST.in
README.rst
build.yaml
codecov.yml
mypy.ini
pyproject.toml
requirements.txt
requirements_all.txt
requirements_test.txt
requirements_test_all.txt
requirements_test_pre_commit.txt
core/homeassistant/helpers/temperature.py

39 lines
1.3 KiB
Python

"""Temperature helpers for Home Assistant."""
from __future__ import annotations
from numbers import Number
from homeassistant.const import PRECISION_HALVES, PRECISION_TENTHS
from homeassistant.core import HomeAssistant
from homeassistant.util.unit_conversion import TemperatureConverter
def display_temp(
hass: HomeAssistant, temperature: float | None, unit: str, precision: float
) -> float | None:
"""Convert temperature into preferred units/precision for display."""
temperature_unit = unit
ha_unit = hass.config.units.temperature_unit
if temperature is None:
return temperature
# If the temperature is not a number this can cause issues
# with Polymer components, so bail early there.
if not isinstance(temperature, Number):
raise TypeError(f"Temperature is not a number: {temperature}")
if temperature_unit != ha_unit:
temperature = TemperatureConverter.converter_factory(temperature_unit, ha_unit)(
temperature
)
# Round in the units appropriate
if precision == PRECISION_HALVES:
return round(temperature * 2) / 2.0
if precision == PRECISION_TENTHS:
return round(temperature, 1)
# Integer as a fall back (PRECISION_WHOLE)
return round(temperature)