mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Apply units of measure provided in API when available in LaCrosse View (#106299)
* Apply units of measure provided in API when available to avoid mismatch of native units. Improved fix for #106148 * Fix ruff error
This commit is contained in:
parent
da684d6a7b
commit
ea7c839423
@ -2,7 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, replace
|
||||
import logging
|
||||
|
||||
from lacrosse_view import Sensor
|
||||
@ -141,6 +141,15 @@ SENSOR_DESCRIPTIONS = {
|
||||
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
|
||||
),
|
||||
}
|
||||
# map of API returned unit of measurement strings to their corresponding unit of measurement
|
||||
UNIT_OF_MEASUREMENT_MAP = {
|
||||
"degrees_celsius": UnitOfTemperature.CELSIUS,
|
||||
"degrees_fahrenheit": UnitOfTemperature.FAHRENHEIT,
|
||||
"inches": UnitOfPrecipitationDepth.INCHES,
|
||||
"millimeters": UnitOfPrecipitationDepth.MILLIMETERS,
|
||||
"kilometers_per_hour": UnitOfSpeed.KILOMETERS_PER_HOUR,
|
||||
"miles_per_hour": UnitOfSpeed.MILES_PER_HOUR,
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@ -171,6 +180,19 @@ async def async_setup_entry(
|
||||
|
||||
_LOGGER.warning(message)
|
||||
continue
|
||||
|
||||
# if the API returns a different unit of measurement from the description, update it
|
||||
if sensor.data.get(field) is not None:
|
||||
native_unit_of_measurement = UNIT_OF_MEASUREMENT_MAP.get(
|
||||
sensor.data[field].get("unit")
|
||||
)
|
||||
|
||||
if native_unit_of_measurement is not None:
|
||||
description = replace(
|
||||
description,
|
||||
native_unit_of_measurement=native_unit_of_measurement,
|
||||
)
|
||||
|
||||
sensor_list.append(
|
||||
LaCrosseViewSensor(
|
||||
coordinator=coordinator,
|
||||
|
@ -70,7 +70,7 @@ TEST_ALREADY_FLOAT_SENSOR = Sensor(
|
||||
sensor_id="2",
|
||||
sensor_field_names=["HeatIndex"],
|
||||
location=Location(id="1", name="Test"),
|
||||
data={"HeatIndex": {"values": [{"s": 2.3}], "unit": "degrees_celsius"}},
|
||||
data={"HeatIndex": {"values": [{"s": 2.3}], "unit": "degrees_fahrenheit"}},
|
||||
permissions={"read": True},
|
||||
model="Test",
|
||||
)
|
||||
@ -81,7 +81,7 @@ TEST_ALREADY_INT_SENSOR = Sensor(
|
||||
sensor_id="2",
|
||||
sensor_field_names=["WindSpeed"],
|
||||
location=Location(id="1", name="Test"),
|
||||
data={"WindSpeed": {"values": [{"s": 2}], "unit": "degrees_celsius"}},
|
||||
data={"WindSpeed": {"values": [{"s": 2}], "unit": "kilometers_per_hour"}},
|
||||
permissions={"read": True},
|
||||
model="Test",
|
||||
)
|
||||
@ -107,3 +107,14 @@ TEST_MISSING_FIELD_DATA_SENSOR = Sensor(
|
||||
permissions={"read": True},
|
||||
model="Test",
|
||||
)
|
||||
TEST_UNITS_OVERRIDE_SENSOR = Sensor(
|
||||
name="Test",
|
||||
device_id="1",
|
||||
type="Test",
|
||||
sensor_id="2",
|
||||
sensor_field_names=["Temperature"],
|
||||
location=Location(id="1", name="Test"),
|
||||
data={"Temperature": {"values": [{"s": "2.1"}], "unit": "degrees_fahrenheit"}},
|
||||
permissions={"read": True},
|
||||
model="Test",
|
||||
)
|
||||
|
@ -19,6 +19,7 @@ from . import (
|
||||
TEST_NO_PERMISSION_SENSOR,
|
||||
TEST_SENSOR,
|
||||
TEST_STRING_SENSOR,
|
||||
TEST_UNITS_OVERRIDE_SENSOR,
|
||||
TEST_UNSUPPORTED_SENSOR,
|
||||
)
|
||||
|
||||
@ -94,6 +95,7 @@ async def test_field_not_supported(
|
||||
(TEST_STRING_SENSOR, "dry", "wet_dry"),
|
||||
(TEST_ALREADY_FLOAT_SENSOR, "-16.5", "heat_index"),
|
||||
(TEST_ALREADY_INT_SENSOR, "2", "wind_speed"),
|
||||
(TEST_UNITS_OVERRIDE_SENSOR, "-16.6", "temperature"),
|
||||
],
|
||||
)
|
||||
async def test_field_types(
|
||||
|
Loading…
x
Reference in New Issue
Block a user