diff --git a/custom_components/tesla_custom/climate.py b/custom_components/tesla_custom/climate.py index 0f719102..fbc462f5 100644 --- a/custom_components/tesla_custom/climate.py +++ b/custom_components/tesla_custom/climate.py @@ -7,7 +7,7 @@ HVACMode, ) from homeassistant.components.climate.const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP -from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS +from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from .base import TeslaCarEntity @@ -62,7 +62,7 @@ def temperature_unit(self): Tesla API always returns in Celsius. """ - return TEMP_CELSIUS + return UnitOfTemperature.CELSIUS @property def current_temperature(self): diff --git a/custom_components/tesla_custom/device_tracker.py b/custom_components/tesla_custom/device_tracker.py index c355416f..8e677bb3 100644 --- a/custom_components/tesla_custom/device_tracker.py +++ b/custom_components/tesla_custom/device_tracker.py @@ -1,7 +1,7 @@ """Support for Tesla device tracker.""" import logging -from homeassistant.components.device_tracker import SOURCE_TYPE_GPS +from homeassistant.components.device_tracker import SourceType from homeassistant.components.device_tracker.config_entry import TrackerEntity from homeassistant.core import HomeAssistant @@ -34,7 +34,7 @@ class TeslaCarLocation(TeslaCarEntity, TrackerEntity): @property def source_type(self): """Return device tracker source type.""" - return SOURCE_TYPE_GPS + return SourceType.GPS @property def longitude(self): @@ -68,7 +68,7 @@ class TeslaCarDestinationLocation(TeslaCarEntity, TrackerEntity): @property def source_type(self): """Return device tracker source type.""" - return SOURCE_TYPE_GPS + return SourceType.GPS @property def longitude(self): diff --git a/custom_components/tesla_custom/number.py b/custom_components/tesla_custom/number.py index 369255d1..73a93eb4 100644 --- a/custom_components/tesla_custom/number.py +++ b/custom_components/tesla_custom/number.py @@ -1,6 +1,6 @@ """Support for Tesla numbers.""" from homeassistant.components.number import NumberEntity, NumberMode -from homeassistant.const import ELECTRIC_CURRENT_AMPERE, PERCENTAGE +from homeassistant.const import PERCENTAGE, UnitOfElectricCurrent from homeassistant.core import HomeAssistant from homeassistant.helpers.icon import icon_for_battery_level from teslajsonpy.const import ( @@ -108,7 +108,7 @@ def native_max_value(self) -> int: @property def native_unit_of_measurement(self) -> str: """Return percentage.""" - return ELECTRIC_CURRENT_AMPERE + return UnitOfElectricCurrent.AMPERE class TeslaEnergyBackupReserve(TeslaEnergyEntity, NumberEntity): diff --git a/custom_components/tesla_custom/sensor.py b/custom_components/tesla_custom/sensor.py index 59f81c8a..0e08cf27 100644 --- a/custom_components/tesla_custom/sensor.py +++ b/custom_components/tesla_custom/sensor.py @@ -9,17 +9,13 @@ SensorStateClass, ) from homeassistant.const import ( - ENERGY_KILO_WATT_HOUR, - ENERGY_WATT_HOUR, - LENGTH_KILOMETERS, - LENGTH_MILES, PERCENTAGE, - POWER_KILO_WATT, - POWER_WATT, - PRESSURE_BAR, - PRESSURE_PSI, - SPEED_MILES_PER_HOUR, - TEMP_CELSIUS, + UnitOfEnergy, + UnitOfLength, + UnitOfPower, + UnitOfPressure, + UnitOfSpeed, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory @@ -148,7 +144,7 @@ class TeslaCarChargerEnergy(TeslaCarEntity, SensorEntity): type = "energy added" _attr_device_class = SensorDeviceClass.ENERGY _attr_state_class = SensorStateClass.TOTAL_INCREASING - _attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR + _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR _attr_icon = "mdi:lightning-bolt" @property @@ -173,7 +169,7 @@ def extra_state_attributes(self): if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR: added_range = DistanceConverter.convert( - added_range, LENGTH_MILES, LENGTH_KILOMETERS + added_range, UnitOfLength.MILES, UnitOfLength.KILOMETERS ) return { @@ -187,7 +183,7 @@ class TeslaCarChargerPower(TeslaCarEntity, SensorEntity): type = "charger power" _attr_device_class = SensorDeviceClass.POWER _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = POWER_KILO_WATT + _attr_native_unit_of_measurement = UnitOfPower.KILO_WATT @property def native_value(self) -> int: @@ -212,7 +208,7 @@ class TeslaCarChargerRate(TeslaCarEntity, SensorEntity): type = "charging rate" _attr_device_class = SensorDeviceClass.SPEED _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = SPEED_MILES_PER_HOUR + _attr_native_unit_of_measurement = UnitOfSpeed.MILES_PER_HOUR _attr_icon = "mdi:speedometer" @property @@ -239,7 +235,7 @@ class TeslaCarOdometer(TeslaCarEntity, SensorEntity): type = "odometer" _attr_device_class = SensorDeviceClass.DISTANCE _attr_state_class = SensorStateClass.TOTAL_INCREASING - _attr_native_unit_of_measurement = LENGTH_MILES + _attr_native_unit_of_measurement = UnitOfLength.MILES _attr_icon = "mdi:counter" @property @@ -293,7 +289,7 @@ class TeslaCarRange(TeslaCarEntity, SensorEntity): type = "range" _attr_device_class = SensorDeviceClass.DISTANCE _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = LENGTH_MILES + _attr_native_unit_of_measurement = UnitOfLength.MILES _attr_icon = "mdi:gauge" @property @@ -319,7 +315,7 @@ def extra_state_attributes(self): ) if est_battery_range is not None: est_battery_range_km = DistanceConverter.convert( - est_battery_range, LENGTH_MILES, LENGTH_KILOMETERS + est_battery_range, UnitOfLength.MILES, UnitOfLength.KILOMETERS ) else: est_battery_range_km = None @@ -336,7 +332,7 @@ class TeslaCarTemp(TeslaCarEntity, SensorEntity): type = "temperature" _attr_device_class = SensorDeviceClass.TEMPERATURE _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = TEMP_CELSIUS + _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS _attr_icon = "mdi:thermometer" def __init__( @@ -367,7 +363,7 @@ class TeslaEnergyPowerSensor(TeslaEnergyEntity, SensorEntity): _attr_device_class = SensorDeviceClass.POWER _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = POWER_WATT + _attr_native_unit_of_measurement = UnitOfPower.WATT def __init__( self, @@ -435,7 +431,7 @@ class TeslaEnergyBatteryRemaining(TeslaEnergyEntity, SensorEntity): type = "battery remaining" _attr_device_class = SensorDeviceClass.ENERGY_STORAGE _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = ENERGY_WATT_HOUR + _attr_native_unit_of_measurement = UnitOfEnergy.WATT_HOUR @property def native_value(self) -> int: @@ -526,8 +522,8 @@ class TeslaCarTpmsPressureSensor(TeslaCarEntity, SensorEntity): _attr_device_class = SensorDeviceClass.PRESSURE _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = PRESSURE_BAR - _attr_suggested_unit_of_measurement = PRESSURE_PSI + _attr_native_unit_of_measurement = UnitOfPressure.BAR + _attr_suggested_unit_of_measurement = UnitOfPressure.PSI _attr_icon = "mdi:gauge-full" def __init__( @@ -619,7 +615,7 @@ class TeslaCarDistanceToArrival(TeslaCarEntity, SensorEntity): type = "distance to arrival" _attr_device_class = SensorDeviceClass.DISTANCE _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = LENGTH_MILES + _attr_native_unit_of_measurement = UnitOfLength.MILES _attr_icon = "mdi:map-marker-distance" @property diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 9405dc6f..adbfe635 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -10,18 +10,14 @@ from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_UNIT_OF_MEASUREMENT, - ENERGY_KILO_WATT_HOUR, - ENERGY_WATT_HOUR, - LENGTH_KILOMETERS, - LENGTH_MILES, PERCENTAGE, - POWER_WATT, - PRESSURE_BAR, - PRESSURE_PSI, - SPEED_KILOMETERS_PER_HOUR, - SPEED_MILES_PER_HOUR, STATE_UNKNOWN, - TEMP_CELSIUS, + UnitOfEnergy, + UnitOfLength, + UnitOfPower, + UnitOfPressure, + UnitOfSpeed, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -118,7 +114,7 @@ async def test_battery_power_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT async def test_battery_remaining(hass: HomeAssistant) -> None: @@ -132,7 +128,7 @@ async def test_battery_remaining(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY_STORAGE assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.WATT_HOUR async def test_backup_reserve(hass: HomeAssistant) -> None: @@ -180,7 +176,7 @@ async def test_charger_energy_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR async def test_charger_power_value(hass: HomeAssistant) -> None: @@ -219,17 +215,22 @@ async def test_charger_rate_value(hass: HomeAssistant) -> None: state = hass.states.get("sensor.my_model_s_charging_rate") - if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == SPEED_KILOMETERS_PER_HOUR: + if ( + state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + == UnitOfSpeed.KILOMETERS_PER_HOUR + ): assert float(state.state) == round( SpeedConverter.convert( car_mock_data.VEHICLE_DATA["charge_state"]["charge_rate"], - SPEED_MILES_PER_HOUR, - SPEED_KILOMETERS_PER_HOUR, + UnitOfSpeed.MILES_PER_HOUR, + UnitOfSpeed.KILOMETERS_PER_HOUR, ), 1, ) else: - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == SPEED_MILES_PER_HOUR + assert ( + state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfSpeed.MILES_PER_HOUR + ) assert ( float(state.state) == car_mock_data.VEHICLE_DATA["charge_state"]["charge_rate"] @@ -307,7 +308,7 @@ async def test_grid_power_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT async def test_inside_temp_value(hass: HomeAssistant) -> None: @@ -321,7 +322,7 @@ async def test_inside_temp_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS async def test_load_power_value(hass: HomeAssistant) -> None: @@ -333,7 +334,7 @@ async def test_load_power_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT async def test_odometer_value(hass: HomeAssistant) -> None: @@ -342,18 +343,18 @@ async def test_odometer_value(hass: HomeAssistant) -> None: state = hass.states.get("sensor.my_model_s_odometer") - if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_KILOMETERS: + if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfLength.KILOMETERS: assert float(state.state) == round( DistanceConverter.convert( car_mock_data.VEHICLE_DATA["vehicle_state"]["odometer"], - LENGTH_MILES, - LENGTH_KILOMETERS, + UnitOfLength.MILES, + UnitOfLength.KILOMETERS, ), 1, ) else: - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_MILES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfLength.MILES assert float(state.state) == round( car_mock_data.VEHICLE_DATA["vehicle_state"]["odometer"], 1 ) @@ -374,7 +375,7 @@ async def test_outside_temp_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS async def test_range_value(hass: HomeAssistant) -> None: @@ -383,18 +384,18 @@ async def test_range_value(hass: HomeAssistant) -> None: state = hass.states.get("sensor.my_model_s_range") - if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_KILOMETERS: + if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfLength.KILOMETERS: assert float(state.state) == round( DistanceConverter.convert( car_mock_data.VEHICLE_DATA["charge_state"]["battery_range"], - LENGTH_MILES, - LENGTH_KILOMETERS, + UnitOfLength.MILES, + UnitOfLength.KILOMETERS, ), 2, ) else: - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_MILES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfLength.MILES assert ( float(state.state) == car_mock_data.VEHICLE_DATA["charge_state"]["battery_range"] @@ -410,8 +411,8 @@ async def test_range_value(hass: HomeAssistant) -> None: if est_range_miles is not None: est_range_km = DistanceConverter.convert( car_mock_data.VEHICLE_DATA["charge_state"]["est_battery_range"], - LENGTH_MILES, - LENGTH_KILOMETERS, + UnitOfLength.MILES, + UnitOfLength.KILOMETERS, ) else: est_range_km = None @@ -434,8 +435,8 @@ async def test_range_attributes_not_available(hass: HomeAssistant) -> None: if est_range_miles is not None: est_range_km = DistanceConverter.convert( car_mock_data.VEHICLE_DATA["charge_state"]["est_battery_range"], - LENGTH_MILES, - LENGTH_KILOMETERS, + UnitOfLength.MILES, + UnitOfLength.KILOMETERS, ) else: est_range_km = None @@ -452,7 +453,7 @@ async def test_solar_power_value(hass: HomeAssistant) -> None: assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT async def test_tpms_pressure_sensor(hass: HomeAssistant) -> None: @@ -468,15 +469,15 @@ async def test_tpms_pressure_sensor(hass: HomeAssistant) -> None: assert float(state_fl.state) == round( PressureConverter.convert( round(car_mock_data.VEHICLE_DATA["vehicle_state"]["tpms_pressure_fl"], 2), - PRESSURE_BAR, - PRESSURE_PSI, + UnitOfPressure.BAR, + UnitOfPressure.PSI, ), prec, ) assert state_fl.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state_fl.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state_fl.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI + assert state_fl.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PSI assert ( state_fl.attributes.get("tpms_last_seen_pressure_timestamp") @@ -494,15 +495,15 @@ async def test_tpms_pressure_sensor(hass: HomeAssistant) -> None: assert float(state_fr.state) == round( PressureConverter.convert( round(car_mock_data.VEHICLE_DATA["vehicle_state"]["tpms_pressure_fr"], 2), - PRESSURE_BAR, - PRESSURE_PSI, + UnitOfPressure.BAR, + UnitOfPressure.PSI, ), prec, ) assert state_fr.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state_fr.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state_fr.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI + assert state_fr.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PSI assert ( state_fr.attributes.get("tpms_last_seen_pressure_timestamp") @@ -520,15 +521,15 @@ async def test_tpms_pressure_sensor(hass: HomeAssistant) -> None: assert float(state_rl.state) == round( PressureConverter.convert( round(car_mock_data.VEHICLE_DATA["vehicle_state"]["tpms_pressure_rl"], 2), - PRESSURE_BAR, - PRESSURE_PSI, + UnitOfPressure.BAR, + UnitOfPressure.PSI, ), prec, ) assert state_rl.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state_rl.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state_rl.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI + assert state_rl.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PSI assert ( state_rl.attributes.get("tpms_last_seen_pressure_timestamp") @@ -546,15 +547,15 @@ async def test_tpms_pressure_sensor(hass: HomeAssistant) -> None: assert float(state_rr.state) == round( PressureConverter.convert( round(car_mock_data.VEHICLE_DATA["vehicle_state"]["tpms_pressure_rr"], 2), - PRESSURE_BAR, - PRESSURE_PSI, + UnitOfPressure.BAR, + UnitOfPressure.PSI, ), prec, ) assert state_rr.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state_rr.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state_rr.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI + assert state_rr.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PSI assert ( state_rr.attributes.get("tpms_last_seen_pressure_timestamp") @@ -580,7 +581,7 @@ async def test_tpms_pressure_none(hass: HomeAssistant) -> None: assert state_fl.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state_fl.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state_fl.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PSI + assert state_fl.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PSI assert state_fl.attributes.get("tpms_last_seen_pressure_timestamp") is None @@ -645,19 +646,19 @@ async def test_distance_to_arrival(hass: HomeAssistant) -> None: # TODO: Fix async test_distance_to_arrival failing in ci # This fixes an async test error. This doesn't happen when test is run individually return - if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_KILOMETERS: + if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfLength.KILOMETERS: assert float(state.state) == round( DistanceConverter.convert( car_mock_data.VEHICLE_DATA["drive_state"][ "active_route_miles_to_arrival" ], - LENGTH_MILES, - LENGTH_KILOMETERS, + UnitOfLength.MILES, + UnitOfLength.KILOMETERS, ), 2, ) else: - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_MILES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfLength.MILES assert float(state.state) == round( car_mock_data.VEHICLE_DATA["drive_state"]["active_route_miles_to_arrival"], 2,