Skip to content

Commit 271b8c9

Browse files
authored
Remove deprecated sensors const values. (#10)
* Resolves #8. Tested with HACS 2.0.3, HA Core 2025.1.0.
1 parent f9e1649 commit 271b8c9

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

custom_components/uhoo/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from pyuhoo.errors import UhooError, UnauthorizedError
1313

1414
from homeassistant.config_entries import ConfigEntry
15-
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
16-
from homeassistant.core import Config, HomeAssistant
15+
from homeassistant.const import UnitOfPressure, UnitOfTemperature, CONF_PASSWORD, CONF_USERNAME
16+
from homeassistant.core_config import Config
17+
from homeassistant.core import HomeAssistant
1718
from homeassistant.exceptions import ConfigEntryNotReady
1819
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1920
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

custom_components/uhoo/const.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
99
CONCENTRATION_PARTS_PER_BILLION,
1010
CONCENTRATION_PARTS_PER_MILLION,
11-
DEVICE_CLASS_CO,
12-
DEVICE_CLASS_CO2,
13-
DEVICE_CLASS_HUMIDITY,
14-
DEVICE_CLASS_PRESSURE,
15-
DEVICE_CLASS_TEMPERATURE,
11+
UnitOfPressure,
12+
UnitOfTemperature,
1613
PERCENTAGE,
17-
PRESSURE_HPA,
18-
TEMP_CELSIUS,
19-
TEMP_FAHRENHEIT,
14+
)
15+
from homeassistant.components.sensor import (
16+
SensorDeviceClass,
2017
)
2118

2219
# Base component constants
@@ -51,14 +48,14 @@
5148

5249
SENSOR_TYPES = {
5350
API_CO: {
54-
ATTR_DEVICE_CLASS: DEVICE_CLASS_CO,
51+
ATTR_DEVICE_CLASS: SensorDeviceClass.CO,
5552
ATTR_ICON: "mdi:molecule-co",
5653
ATTR_UNIT_OF_MEASUREMENT: CONCENTRATION_PARTS_PER_MILLION,
5754
ATTR_LABEL: "Carbon monoxide",
5855
ATTR_UNIQUE_ID: API_CO,
5956
},
6057
API_CO2: {
61-
ATTR_DEVICE_CLASS: DEVICE_CLASS_CO2,
58+
ATTR_DEVICE_CLASS: SensorDeviceClass.CO2,
6259
ATTR_ICON: "mdi:molecule-co2",
6360
ATTR_UNIT_OF_MEASUREMENT: CONCENTRATION_PARTS_PER_MILLION,
6461
ATTR_LABEL: "Carbon dioxide",
@@ -72,7 +69,7 @@
7269
ATTR_UNIQUE_ID: API_DUST,
7370
},
7471
API_HUMIDITY: {
75-
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
72+
ATTR_DEVICE_CLASS: SensorDeviceClass.HUMIDITY,
7673
ATTR_ICON: "mdi:water-percent",
7774
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
7875
ATTR_LABEL: "Humidity",
@@ -93,16 +90,16 @@
9390
ATTR_UNIQUE_ID: API_OZONE,
9491
},
9592
API_PRESSURE: {
96-
ATTR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
93+
ATTR_DEVICE_CLASS: SensorDeviceClass.PRESSURE,
9794
ATTR_ICON: "mdi:gauge",
98-
ATTR_UNIT_OF_MEASUREMENT: PRESSURE_HPA,
95+
ATTR_UNIT_OF_MEASUREMENT: UnitOfPressure.HPA,
9996
ATTR_LABEL: "Air pressure",
10097
ATTR_UNIQUE_ID: API_PRESSURE,
10198
},
10299
API_TEMP: {
103-
ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
100+
ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
104101
ATTR_ICON: "mdi:thermometer",
105-
ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT,
102+
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
106103
ATTR_LABEL: "Temperature",
107104
ATTR_UNIQUE_ID: API_TEMP,
108105
},

custom_components/uhoo/sensor.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pyuhoo.device import Device
44

55
from custom_components.uhoo import UhooDataUpdateCoordinator
6-
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
6+
from homeassistant.components.sensor import SensorStateClass, SensorEntity
77
from homeassistant.core import HomeAssistant
88
from homeassistant.helpers.entity_platform import AddEntitiesCallback
99
from homeassistant.helpers.typing import ConfigType
@@ -20,8 +20,7 @@
2020
MANUFACTURER,
2121
MODEL,
2222
SENSOR_TYPES,
23-
TEMP_CELSIUS,
24-
TEMP_FAHRENHEIT,
23+
UnitOfTemperature,
2524
)
2625

2726

@@ -92,7 +91,7 @@ def state(self):
9291
@property
9392
def state_class(self) -> str:
9493
"""Return the state class of this entity, from STATE_CLASSES, if any."""
95-
return str(STATE_CLASS_MEASUREMENT)
94+
return str(SensorStateClass.MEASUREMENT)
9695

9796
@property
9897
def icon(self) -> str:
@@ -104,8 +103,8 @@ def unit_of_measurement(self) -> str:
104103
"""Return unit of measurement."""
105104
if self._kind == API_TEMP:
106105
if self._coordinator.user_settings_temp == "f":
107-
return str(TEMP_FAHRENHEIT)
106+
return str(UnitOfTemperature.FAHRENHEIT)
108107
else:
109-
return str(TEMP_CELSIUS)
108+
return str(UnitOfTemperature.CELSIUS)
110109
else:
111110
return str(SENSOR_TYPES[self._kind][ATTR_UNIT_OF_MEASUREMENT])

tests/test_sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
ATTR_LABEL,
1919
SENSOR_TYPES,
2020
)
21-
from homeassistant.components.sensor import ATTR_STATE_CLASS, STATE_CLASS_MEASUREMENT
21+
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass
2222
from homeassistant.const import (
2323
ATTR_DEVICE_CLASS,
2424
ATTR_ICON,
@@ -56,7 +56,7 @@ def assert_expected_properties(
5656
assert state.state == f"{str(MOCK_DEVICE_DATA[sensor_type]['value'])}"
5757

5858
# Attributes
59-
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
59+
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
6060
assert state.attributes.get(ATTR_ICON) == SENSOR_TYPES[sensor_type][ATTR_ICON]
6161
assert (
6262
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)

0 commit comments

Comments
 (0)