Use identity checks for sensor device class enums (#106383)

This commit is contained in:
J. Nick Koston 2023-12-25 08:35:44 -10:00 committed by GitHub
parent 3016dbc2bd
commit 50f64e053e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -511,7 +511,7 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
if ( if (
native_unit_of_measurement native_unit_of_measurement
in {UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT} in {UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT}
and self.device_class == SensorDeviceClass.TEMPERATURE and self.device_class is SensorDeviceClass.TEMPERATURE
): ):
return self.hass.config.units.temperature_unit return self.hass.config.units.temperature_unit
@ -572,7 +572,7 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
return None return None
# Received a datetime # Received a datetime
if device_class == SensorDeviceClass.TIMESTAMP: if device_class is SensorDeviceClass.TIMESTAMP:
try: try:
# We cast the value, to avoid using isinstance, but satisfy # We cast the value, to avoid using isinstance, but satisfy
# typechecking. The errors are guarded in this try. # typechecking. The errors are guarded in this try.
@ -594,7 +594,7 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
) from err ) from err
# Received a date value # Received a date value
if device_class == SensorDeviceClass.DATE: if device_class is SensorDeviceClass.DATE:
try: try:
# We cast the value, to avoid using isinstance, but satisfy # We cast the value, to avoid using isinstance, but satisfy
# typechecking. The errors are guarded in this try. # typechecking. The errors are guarded in this try.
@ -609,8 +609,8 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
# Enum checks # Enum checks
if ( if (
options := self.options options := self.options
) is not None or device_class == SensorDeviceClass.ENUM: ) is not None or device_class is SensorDeviceClass.ENUM:
if device_class != SensorDeviceClass.ENUM: if device_class is not SensorDeviceClass.ENUM:
reason = "is missing the enum device class" reason = "is missing the enum device class"
if device_class is not None: if device_class is not None:
reason = f"has device class '{device_class}' instead of 'enum'" reason = f"has device class '{device_class}' instead of 'enum'"