Skip to content

Commit

Permalink
Bump subarulink to 0.7.11
Browse files Browse the repository at this point in the history
- API native values are now US customary
- Use latest MySubaru API g2v29
  • Loading branch information
G-Two committed Mar 3, 2024
1 parent 77f6836 commit dd97e9d
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 128 deletions.
4 changes: 2 additions & 2 deletions custom_components/subaru/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"documentation": "https://github.com/G-Two/homeassistant-subaru",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/G-Two/homeassistant-subaru/issues",
"requirements": ["subarulink==0.7.9"],
"version": "0.7.12"
"requirements": ["subarulink==0.7.11"],
"version": "0.7.13"
}
71 changes: 41 additions & 30 deletions custom_components/subaru/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util.unit_conversion import DistanceConverter, VolumeConverter
from homeassistant.util.unit_system import (
LENGTH_UNITS,
PRESSURE_UNITS,
US_CUSTOMARY_SYSTEM,
from homeassistant.util.unit_conversion import (
DistanceConverter,
PressureConverter,
VolumeConverter,
)
from homeassistant.util.unit_system import LENGTH_UNITS, METRIC_SYSTEM, PRESSURE_UNITS

from .const import (
API_GEN_2,
Expand Down Expand Up @@ -59,7 +59,7 @@
translation_key="odometer",
device_class=SensorDeviceClass.DISTANCE,
icon="mdi:road-variant",
native_unit_of_measurement=UnitOfLength.KILOMETERS,
native_unit_of_measurement=UnitOfLength.MILES,
state_class=SensorStateClass.TOTAL_INCREASING,
),
]
Expand All @@ -70,43 +70,43 @@
key=sc.AVG_FUEL_CONSUMPTION,
translation_key="average_fuel_consumption",
icon="mdi:leaf",
native_unit_of_measurement=FUEL_CONSUMPTION_LITERS_PER_HUNDRED_KILOMETERS,
native_unit_of_measurement=FUEL_CONSUMPTION_MILES_PER_GALLON,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=sc.DIST_TO_EMPTY,
translation_key="range",
device_class=SensorDeviceClass.DISTANCE,
icon="mdi:gas-station",
native_unit_of_measurement=UnitOfLength.KILOMETERS,
native_unit_of_measurement=UnitOfLength.MILES,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=sc.TIRE_PRESSURE_FL,
translation_key="tire_pressure_front_left",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=UnitOfPressure.HPA,
native_unit_of_measurement=UnitOfPressure.PSI,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=sc.TIRE_PRESSURE_FR,
translation_key="tire_pressure_front_right",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=UnitOfPressure.HPA,
native_unit_of_measurement=UnitOfPressure.PSI,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=sc.TIRE_PRESSURE_RL,
translation_key="tire_pressure_rear_left",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=UnitOfPressure.HPA,
native_unit_of_measurement=UnitOfPressure.PSI,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=sc.TIRE_PRESSURE_RR,
translation_key="tire_pressure_rear_right",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=UnitOfPressure.HPA,
native_unit_of_measurement=UnitOfPressure.PSI,
state_class=SensorStateClass.MEASUREMENT,
),
]
Expand Down Expand Up @@ -220,13 +220,12 @@ def native_value(self) -> None | int | float:
if current_value is None:
return None

if unit in LENGTH_UNITS:
if unit in LENGTH_UNITS and unit_system == METRIC_SYSTEM:
return round(unit_system.length(current_value, cast(str, unit)), 1)

if unit in PRESSURE_UNITS and unit_system == US_CUSTOMARY_SYSTEM:
if unit in PRESSURE_UNITS and unit_system == METRIC_SYSTEM:
return round(
unit_system.pressure(current_value, cast(str, unit)),
1,
PressureConverter.convert(current_value, unit, UnitOfPressure.KPA), 0
)

if (
Expand All @@ -235,7 +234,7 @@ def native_value(self) -> None | int | float:
FUEL_CONSUMPTION_LITERS_PER_HUNDRED_KILOMETERS,
FUEL_CONSUMPTION_MILES_PER_GALLON,
]
and unit_system == US_CUSTOMARY_SYSTEM
and unit_system == METRIC_SYSTEM
):
return round((100.0 * L_PER_GAL) / (KM_PER_MI * current_value), 1)

Expand All @@ -247,25 +246,27 @@ def native_unit_of_measurement(self) -> str | None:
unit = self.entity_description.native_unit_of_measurement

if unit in LENGTH_UNITS:
return self.hass.config.units.length_unit
if self.hass.config.units == METRIC_SYSTEM:
return self.hass.config.units.length_unit

if unit in PRESSURE_UNITS:
if self.hass.config.units == US_CUSTOMARY_SYSTEM:
return self.hass.config.units.pressure_unit
if self.hass.config.units == METRIC_SYSTEM:
return UnitOfPressure.KPA

if unit in [
FUEL_CONSUMPTION_LITERS_PER_HUNDRED_KILOMETERS,
FUEL_CONSUMPTION_MILES_PER_GALLON,
]:
if self.hass.config.units == US_CUSTOMARY_SYSTEM:
return FUEL_CONSUMPTION_MILES_PER_GALLON
if self.hass.config.units == METRIC_SYSTEM:
return FUEL_CONSUMPTION_LITERS_PER_HUNDRED_KILOMETERS

return unit

@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return entity specific state attributes."""
extra_attributes = None
unit_system = self.hass.config.units

# Provide recommended tire pressure
if self.device_class == SensorDeviceClass.PRESSURE:
Expand All @@ -276,17 +277,27 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
sc.TIRE_PRESSURE_FL,
sc.TIRE_PRESSURE_FR,
]:
extra_attributes = {
"Recommended pressure": info.get(
sc.HEALTH_RECOMMENDED_TIRE_PRESSURE_FRONT
value = info.get(sc.HEALTH_RECOMMENDED_TIRE_PRESSURE_FRONT)
if unit_system == METRIC_SYSTEM:
value = round(
PressureConverter.convert(
value, UnitOfPressure.PSI, UnitOfPressure.KPA
),
0,
)
}

extra_attributes = {"Recommended pressure": value}
else:
extra_attributes = {
"Recommended pressure": info.get(
sc.HEALTH_RECOMMENDED_TIRE_PRESSURE_REAR
value = info.get(sc.HEALTH_RECOMMENDED_TIRE_PRESSURE_REAR)
if unit_system == METRIC_SYSTEM:
value = round(
PressureConverter.convert(
value, UnitOfPressure.PSI, UnitOfPressure.KPA
),
0,
)
}

extra_attributes = {"Recommended pressure": value}

return extra_attributes

Expand Down
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pylint
pytest
pytest-cov
pytest-homeassistant-custom-component
subarulink==0.7.9
subarulink==0.7.11
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ forced_separate = tests
combine_as_imports = true

[mypy]
python_version = 3.9
python_version = 3.11
ignore_errors = true
follow_imports = silent
ignore_missing_imports = true
Expand Down
Loading

0 comments on commit dd97e9d

Please sign in to comment.