Collection of changing entity properties to class attributes (#51248)

* Collection of changing entity properties to class attributes

* Apply suggestions from code review

Co-authored-by: Erik Montnemery <erik@montnemery.com>

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Franck Nijhof 2021-05-31 10:50:11 +02:00 committed by GitHub
parent 5acc3a1083
commit 258b388f41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 106 additions and 305 deletions

View file

@ -33,6 +33,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class AdvantageAirZoneFilter(AdvantageAirEntity, BinarySensorEntity):
"""Advantage Air Filter."""
_attr_device_class = DEVICE_CLASS_PROBLEM
@property
def name(self):
"""Return the name."""
@ -43,11 +45,6 @@ class AdvantageAirZoneFilter(AdvantageAirEntity, BinarySensorEntity):
"""Return a unique id."""
return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-filter'
@property
def device_class(self):
"""Return the device class of the vent."""
return DEVICE_CLASS_PROBLEM
@property
def is_on(self):
"""Return if filter needs cleaning."""
@ -57,6 +54,8 @@ class AdvantageAirZoneFilter(AdvantageAirEntity, BinarySensorEntity):
class AdvantageAirZoneMotion(AdvantageAirEntity, BinarySensorEntity):
"""Advantage Air Zone Motion."""
_attr_device_class = DEVICE_CLASS_MOTION
@property
def name(self):
"""Return the name."""
@ -67,11 +66,6 @@ class AdvantageAirZoneMotion(AdvantageAirEntity, BinarySensorEntity):
"""Return a unique id."""
return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-motion'
@property
def device_class(self):
"""Return the device class of the vent."""
return DEVICE_CLASS_MOTION
@property
def is_on(self):
"""Return if motion is detect."""
@ -81,6 +75,8 @@ class AdvantageAirZoneMotion(AdvantageAirEntity, BinarySensorEntity):
class AdvantageAirZoneMyZone(AdvantageAirEntity, BinarySensorEntity):
"""Advantage Air Zone MyZone."""
_attr_entity_registry_enabled_default = False
@property
def name(self):
"""Return the name."""
@ -95,8 +91,3 @@ class AdvantageAirZoneMyZone(AdvantageAirEntity, BinarySensorEntity):
def is_on(self):
"""Return if this zone is the myZone."""
return self._zone["number"] == self._ac["myZone"]
@property
def entity_registry_enabled_default(self):
"""Return false to disable this entity by default."""
return False

View file

@ -44,6 +44,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class AdvantageAirTimeTo(AdvantageAirEntity, SensorEntity):
"""Representation of Advantage Air timer control."""
_attr_unit_of_measurement = ADVANTAGE_AIR_SET_COUNTDOWN_UNIT
def __init__(self, instance, ac_key, action):
"""Initialize the Advantage Air timer control."""
super().__init__(instance, ac_key)
@ -65,11 +67,6 @@ class AdvantageAirTimeTo(AdvantageAirEntity, SensorEntity):
"""Return the current value."""
return self._ac[self._time_key]
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ADVANTAGE_AIR_SET_COUNTDOWN_UNIT
@property
def icon(self):
"""Return a representative icon of the timer."""
@ -86,6 +83,8 @@ class AdvantageAirTimeTo(AdvantageAirEntity, SensorEntity):
class AdvantageAirZoneVent(AdvantageAirEntity, SensorEntity):
"""Representation of Advantage Air Zone Vent Sensor."""
_attr_unit_of_measurement = PERCENTAGE
@property
def name(self):
"""Return the name."""
@ -103,11 +102,6 @@ class AdvantageAirZoneVent(AdvantageAirEntity, SensorEntity):
return self._zone["value"]
return 0
@property
def unit_of_measurement(self):
"""Return the percent sign."""
return PERCENTAGE
@property
def icon(self):
"""Return a representative icon."""
@ -119,6 +113,8 @@ class AdvantageAirZoneVent(AdvantageAirEntity, SensorEntity):
class AdvantageAirZoneSignal(AdvantageAirEntity, SensorEntity):
"""Representation of Advantage Air Zone wireless signal sensor."""
_attr_unit_of_measurement = PERCENTAGE
@property
def name(self):
"""Return the name."""
@ -134,11 +130,6 @@ class AdvantageAirZoneSignal(AdvantageAirEntity, SensorEntity):
"""Return the current value of the wireless signal."""
return self._zone["rssi"]
@property
def unit_of_measurement(self):
"""Return the percent sign."""
return PERCENTAGE
@property
def icon(self):
"""Return a representative icon."""

View file

@ -67,6 +67,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class AtenSwitch(SwitchEntity):
"""Represents an ATEN PE switch."""
_attr_device_class = DEVICE_CLASS_OUTLET
def __init__(self, device, mac, outlet, name):
"""Initialize an ATEN PE switch."""
self._device = device
@ -86,11 +88,6 @@ class AtenSwitch(SwitchEntity):
"""Return the name of the entity."""
return self._name
@property
def device_class(self) -> str:
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_OUTLET
@property
def is_on(self) -> bool:
"""Return True if entity is on."""

View file

@ -54,6 +54,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class CO2Sensor(SensorEntity):
"""Implementation of the CO2Signal sensor."""
_attr_icon = "mdi:molecule-co2"
_attr_unit_of_measurement = CO2_INTENSITY_UNIT
def __init__(self, token, country_code, lat, lon):
"""Initialize the sensor."""
self._token = token
@ -74,21 +77,11 @@ class CO2Sensor(SensorEntity):
"""Return the name of the sensor."""
return self._friendly_name
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return "mdi:molecule-co2"
@property
def state(self):
"""Return the state of the device."""
return self._data
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return CO2_INTENSITY_UNIT
@property
def extra_state_attributes(self):
"""Return the state attributes of the last update."""

View file

@ -127,6 +127,8 @@ class DeconzTampering(DeconzDevice, BinarySensorEntity):
TYPE = DOMAIN
_attr_device_class = DEVICE_CLASS_PROBLEM
@property
def unique_id(self) -> str:
"""Return a unique identifier for this device."""
@ -148,8 +150,3 @@ class DeconzTampering(DeconzDevice, BinarySensorEntity):
def name(self) -> str:
"""Return the name of the sensor."""
return f"{self._device.name} Tampered"
@property
def device_class(self) -> str:
"""Return the class of the sensor."""
return DEVICE_CLASS_PROBLEM

View file

@ -110,6 +110,7 @@ class DeconzThermostat(DeconzDevice, ClimateEntity):
"""Representation of a deCONZ thermostat."""
TYPE = DOMAIN
_attr_temperature_unit = TEMP_CELSIUS
def __init__(self, device, gateway):
"""Set up thermostat device."""
@ -238,11 +239,6 @@ class DeconzThermostat(DeconzDevice, ClimateEntity):
await self._device.async_set_config(data)
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def extra_state_attributes(self):
"""Return the state attributes of the thermostat."""

View file

@ -60,6 +60,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class DeLijnPublicTransportSensor(SensorEntity):
"""Representation of a Ruter sensor."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
def __init__(self, line):
"""Initialize the sensor."""
self.line = line
@ -104,11 +106,6 @@ class DeLijnPublicTransportSensor(SensorEntity):
"""Return True if entity is available."""
return self._available
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TIMESTAMP
@property
def name(self):
"""Return the name of the sensor."""

View file

@ -37,6 +37,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
"""Representation of an Emonitor power sensor entity."""
_attr_device_class = DEVICE_CLASS_POWER
_attr_unit_of_measurement = POWER_WATT
def __init__(self, coordinator: DataUpdateCoordinator, channel_number: int) -> None:
"""Initialize the channel sensor."""
self.channel_number = channel_number
@ -62,16 +65,6 @@ class EmonitorPowerSensor(CoordinatorEntity, SensorEntity):
"""Name of the sensor."""
return self.channel_data.label
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement."""
return POWER_WATT
@property
def device_class(self) -> str:
"""Device class of the sensor."""
return DEVICE_CLASS_POWER
def _paired_attr(self, attr_name: str) -> float:
"""Cumulative attributes for channel and paired channel."""
attr_val = getattr(self.channel_data, attr_name)

View file

@ -19,15 +19,12 @@ from . import KNOWN_DEVICES, HomeKitEntity
class HomeKitMotionSensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit motion sensor."""
_attr_device_class = DEVICE_CLASS_MOTION
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.MOTION_DETECTED]
@property
def device_class(self):
"""Define this binary_sensor as a motion sensor."""
return DEVICE_CLASS_MOTION
@property
def is_on(self):
"""Has motion been detected."""
@ -37,15 +34,12 @@ class HomeKitMotionSensor(HomeKitEntity, BinarySensorEntity):
class HomeKitContactSensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit contact sensor."""
_attr_device_class = DEVICE_CLASS_OPENING
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.CONTACT_STATE]
@property
def device_class(self):
"""Define this binary_sensor as a opening sensor."""
return DEVICE_CLASS_OPENING
@property
def is_on(self):
"""Return true if the binary sensor is on/open."""
@ -55,10 +49,7 @@ class HomeKitContactSensor(HomeKitEntity, BinarySensorEntity):
class HomeKitSmokeSensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit smoke sensor."""
@property
def device_class(self) -> str:
"""Return the class of this sensor."""
return DEVICE_CLASS_SMOKE
_attr_device_class = DEVICE_CLASS_SMOKE
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
@ -73,10 +64,7 @@ class HomeKitSmokeSensor(HomeKitEntity, BinarySensorEntity):
class HomeKitCarbonMonoxideSensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit BO sensor."""
@property
def device_class(self) -> str:
"""Return the class of this sensor."""
return DEVICE_CLASS_GAS
_attr_device_class = DEVICE_CLASS_GAS
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
@ -91,10 +79,7 @@ class HomeKitCarbonMonoxideSensor(HomeKitEntity, BinarySensorEntity):
class HomeKitOccupancySensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit occupancy sensor."""
@property
def device_class(self) -> str:
"""Return the class of this sensor."""
return DEVICE_CLASS_OCCUPANCY
_attr_device_class = DEVICE_CLASS_OCCUPANCY
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
@ -109,15 +94,12 @@ class HomeKitOccupancySensor(HomeKitEntity, BinarySensorEntity):
class HomeKitLeakSensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit leak sensor."""
_attr_device_class = DEVICE_CLASS_MOISTURE
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.LEAK_DETECTED]
@property
def device_class(self):
"""Define this binary_sensor as a leak sensor."""
return DEVICE_CLASS_MOISTURE
@property
def is_on(self):
"""Return true if a leak is detected from the binary sensor."""

View file

@ -43,15 +43,13 @@ SIMPLE_SENSOR = {
class HomeKitHumiditySensor(HomeKitEntity, SensorEntity):
"""Representation of a Homekit humidity sensor."""
_attr_device_class = DEVICE_CLASS_HUMIDITY
_attr_unit_of_measurement = PERCENTAGE
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT]
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_HUMIDITY
@property
def name(self):
"""Return the name of the device."""
@ -62,11 +60,6 @@ class HomeKitHumiditySensor(HomeKitEntity, SensorEntity):
"""Return the sensor icon."""
return HUMIDITY_ICON
@property
def unit_of_measurement(self):
"""Return units for the sensor."""
return PERCENTAGE
@property
def state(self):
"""Return the current humidity."""
@ -76,15 +69,13 @@ class HomeKitHumiditySensor(HomeKitEntity, SensorEntity):
class HomeKitTemperatureSensor(HomeKitEntity, SensorEntity):
"""Representation of a Homekit temperature sensor."""
_attr_device_class = DEVICE_CLASS_TEMPERATURE
_attr_unit_of_measurement = TEMP_CELSIUS
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.TEMPERATURE_CURRENT]
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_TEMPERATURE
@property
def name(self):
"""Return the name of the device."""
@ -95,11 +86,6 @@ class HomeKitTemperatureSensor(HomeKitEntity, SensorEntity):
"""Return the sensor icon."""
return TEMP_C_ICON
@property
def unit_of_measurement(self):
"""Return units for the sensor."""
return TEMP_CELSIUS
@property
def state(self):
"""Return the current temperature in Celsius."""
@ -109,15 +95,13 @@ class HomeKitTemperatureSensor(HomeKitEntity, SensorEntity):
class HomeKitLightSensor(HomeKitEntity, SensorEntity):
"""Representation of a Homekit light level sensor."""
_attr_device_class = DEVICE_CLASS_ILLUMINANCE
_attr_unit_of_measurement = LIGHT_LUX
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.LIGHT_LEVEL_CURRENT]
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_ILLUMINANCE
@property
def name(self):
"""Return the name of the device."""
@ -128,11 +112,6 @@ class HomeKitLightSensor(HomeKitEntity, SensorEntity):
"""Return the sensor icon."""
return BRIGHTNESS_ICON
@property
def unit_of_measurement(self):
"""Return units for the sensor."""
return LIGHT_LUX
@property
def state(self):
"""Return the current light level in lux."""
@ -142,6 +121,9 @@ class HomeKitLightSensor(HomeKitEntity, SensorEntity):
class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
"""Representation of a Homekit Carbon Dioxide sensor."""
_attr_icon = CO2_ICON
_attr_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.CARBON_DIOXIDE_LEVEL]
@ -151,16 +133,6 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
"""Return the name of the device."""
return f"{super().name} CO2"
@property
def icon(self):
"""Return the sensor icon."""
return CO2_ICON
@property
def unit_of_measurement(self):
"""Return units for the sensor."""
return CONCENTRATION_PARTS_PER_MILLION
@property
def state(self):
"""Return the current CO2 level in ppm."""
@ -170,6 +142,9 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
class HomeKitBatterySensor(HomeKitEntity, SensorEntity):
"""Representation of a Homekit battery sensor."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [
@ -178,11 +153,6 @@ class HomeKitBatterySensor(HomeKitEntity, SensorEntity):
CharacteristicsTypes.CHARGING_STATE,
]
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_BATTERY
@property
def name(self):
"""Return the name of the device."""
@ -210,11 +180,6 @@ class HomeKitBatterySensor(HomeKitEntity, SensorEntity):
return icon
@property
def unit_of_measurement(self):
"""Return units for the sensor."""
return PERCENTAGE
@property
def is_low_battery(self):
"""Return true if battery level is low."""

View file

@ -112,6 +112,8 @@ class HMCover(HMDevice, CoverEntity):
class HMGarage(HMCover):
"""Represents a Homematic Garage cover. Homematic garage covers do not support position attributes."""
_attr_device_class = DEVICE_CLASS_GARAGE
@property
def current_cover_position(self):
"""
@ -127,11 +129,6 @@ class HMGarage(HMCover):
"""Return whether the cover is closed."""
return self._hmdevice.is_closed(self._hm_get_state())
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_GARAGE
def _init_data_struct(self):
"""Generate a data dictionary (self._data) from metadata."""
self._state = "DOOR_STATE"

View file

@ -132,6 +132,8 @@ class IgnSismologiaFeedEntityManager:
class IgnSismologiaLocationEvent(GeolocationEvent):
"""This represents an external event with IGN Sismologia feed data."""
_attr_unit_of_measurement = LENGTH_KILOMETERS
def __init__(self, feed_manager, external_id):
"""Initialize entity with data from feed entry."""
self._feed_manager = feed_manager
@ -233,11 +235,6 @@ class IgnSismologiaLocationEvent(GeolocationEvent):
"""Return longitude value of this external event."""
return self._longitude
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return LENGTH_KILOMETERS
@property
def extra_state_attributes(self):
"""Return the device state attributes."""

View file

@ -27,6 +27,9 @@ async def async_setup_entry(
class RouterOnlineBinarySensor(BinarySensorEntity):
"""Representation router connection status."""
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
_attr_should_poll = False
def __init__(self, router: KeeneticRouter) -> None:
"""Initialize the APCUPSd binary device."""
self._router = router
@ -46,16 +49,6 @@ class RouterOnlineBinarySensor(BinarySensorEntity):
"""Return true if the UPS is online, else false."""
return self._router.available
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_CONNECTIVITY
@property
def should_poll(self) -> bool:
"""Return False since entity pushes its state to HA."""
return False
@property
def device_info(self):
"""Return a client description for device registry."""

View file

@ -29,17 +29,14 @@ class LutronOccupancySensor(LutronDevice, BinarySensorEntity):
reported as a single occupancy group.
"""
_attr_device_class = DEVICE_CLASS_OCCUPANCY
@property
def is_on(self):
"""Return true if the binary sensor is on."""
# Error cases will end up treated as unoccupied.
return self._lutron_device.state == OccupancyGroup.State.OCCUPIED
@property
def device_class(self):
"""Return that this is an occupancy sensor."""
return DEVICE_CLASS_OCCUPANCY
@property
def name(self):
"""Return the name of the device."""

View file

@ -166,6 +166,8 @@ class NMBSLiveBoard(SensorEntity):
class NMBSSensor(SensorEntity):
"""Get the the total travel time for a given connection."""
_attr_unit_of_measurement = TIME_MINUTES
def __init__(
self, api_client, name, show_on_map, station_from, station_to, excl_vias
):
@ -185,11 +187,6 @@ class NMBSSensor(SensorEntity):
"""Return the name of the sensor."""
return self._name
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return TIME_MINUTES
@property
def icon(self):
"""Return the sensor default icon or an alert icon if any delay."""

View file

@ -31,6 +31,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class ShellyCover(ShellyBlockEntity, CoverEntity):
"""Switch that controls a cover block on Shelly devices."""
_attr_device_class = DEVICE_CLASS_SHUTTER
def __init__(self, wrapper: ShellyDeviceWrapper, block: Block) -> None:
"""Initialize light."""
super().__init__(wrapper, block)
@ -76,11 +78,6 @@ class ShellyCover(ShellyBlockEntity, CoverEntity):
"""Flag supported features."""
return self._supported_features
@property
def device_class(self) -> str:
"""Return the class of the device."""
return DEVICE_CLASS_SHUTTER
async def async_close_cover(self, **kwargs):
"""Close cover."""
self.control_result = await self.set_state(go="close")

View file

@ -75,6 +75,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class SighthoundEntity(ImageProcessingEntity):
"""Create a sighthound entity."""
_attr_unit_of_measurement = ATTR_PEOPLE
def __init__(
self, api, camera_entity, name, save_file_folder, save_timestamped_file
):
@ -164,11 +166,6 @@ class SighthoundEntity(ImageProcessingEntity):
"""Return the state of the entity."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ATTR_PEOPLE
@property
def extra_state_attributes(self):
"""Return the attributes."""

View file

@ -33,6 +33,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
"""Representation of an Smart Meter Texas sensor."""
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
def __init__(self, meter: Meter, coordinator: DataUpdateCoordinator) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
@ -40,11 +42,6 @@ class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
self._state = None
self._available = False
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ENERGY_KILO_WATT_HOUR
@property
def name(self):
"""Device Name."""

View file

@ -63,6 +63,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity):
"""A binary sensor indicating whether the spa is currently online (connected to the cloud)."""
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
def __init__(self, coordinator, spa):
"""Initialize the entity."""
super().__init__(coordinator, spa, "Online", "online")
@ -80,15 +82,12 @@ class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity):
"""Return true if the binary sensor is on."""
return self._state is True
@property
def device_class(self) -> str:
"""Return the device class for this entity."""
return DEVICE_CLASS_CONNECTIVITY
class SmartTubReminder(SmartTubEntity, BinarySensorEntity):
"""Reminders for maintenance actions."""
_attr_device_class = DEVICE_CLASS_PROBLEM
def __init__(self, coordinator, spa, reminder):
"""Initialize the entity."""
super().__init__(
@ -120,11 +119,6 @@ class SmartTubReminder(SmartTubEntity, BinarySensorEntity):
ATTR_REMINDER_SNOOZED: self.reminder.snoozed,
}
@property
def device_class(self) -> str:
"""Return the device class for this entity."""
return DEVICE_CLASS_PROBLEM
async def async_snooze(self, days):
"""Snooze this reminder for the specified number of days."""
await self.reminder.snooze(days)
@ -137,6 +131,8 @@ class SmartTubError(SmartTubEntity, BinarySensorEntity):
There may be 0 or more errors. If there are >0, we show the first one.
"""
_attr_device_class = DEVICE_CLASS_PROBLEM
def __init__(self, coordinator, spa):
"""Initialize the entity."""
super().__init__(
@ -175,8 +171,3 @@ class SmartTubError(SmartTubEntity, BinarySensorEntity):
ATTR_CREATED_AT: error.created_at.isoformat(),
ATTR_UPDATED_AT: error.updated_at.isoformat(),
}
@property
def device_class(self) -> str:
"""Return the device class for this entity."""
return DEVICE_CLASS_PROBLEM

View file

@ -29,10 +29,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class SomaSensor(SomaEntity, SensorEntity):
"""Representation of a Soma cover device."""
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_BATTERY
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
@property
def name(self):
@ -44,11 +42,6 @@ class SomaSensor(SomaEntity, SensorEntity):
"""Return the state of the entity."""
return self.battery_state
@property
def unit_of_measurement(self):
"""Return the unit of measurement this sensor expresses itself in."""
return PERCENTAGE
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Update the sensor with the latest data."""

View file

@ -30,6 +30,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class SomfyThermostatBatterySensor(SomfyEntity, SensorEntity):
"""Representation of a Somfy thermostat battery."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, coordinator, device_id, api):
"""Initialize the Somfy device."""
super().__init__(coordinator, device_id, api)
@ -44,13 +47,3 @@ class SomfyThermostatBatterySensor(SomfyEntity, SensorEntity):
def state(self) -> int:
"""Return the state of the sensor."""
return self._climate.get_battery()
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_BATTERY
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of the sensor."""
return PERCENTAGE

View file

@ -75,16 +75,13 @@ class SyncThruBinarySensor(CoordinatorEntity, BinarySensorEntity):
class SyncThruOnlineSensor(SyncThruBinarySensor):
"""Implementation of a sensor that checks whether is turned on/online."""
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
def __init__(self, syncthru, name):
"""Initialize the sensor."""
super().__init__(syncthru, name)
self._id_suffix = "_online"
@property
def device_class(self):
"""Class of the sensor."""
return DEVICE_CLASS_CONNECTIVITY
@property
def is_on(self):
"""Set the state to whether the printer is online."""
@ -94,16 +91,13 @@ class SyncThruOnlineSensor(SyncThruBinarySensor):
class SyncThruProblemSensor(SyncThruBinarySensor):
"""Implementation of a sensor that checks whether the printer works correctly."""
_attr_device_class = DEVICE_CLASS_PROBLEM
def __init__(self, syncthru, name):
"""Initialize the sensor."""
super().__init__(syncthru, name)
self._id_suffix = "_problem"
@property
def device_class(self):
"""Class of the sensor."""
return DEVICE_CLASS_PROBLEM
@property
def is_on(self):
"""Set the state to whether there is a problem with the printer."""

View file

@ -29,22 +29,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TradfriSensor(TradfriBaseDevice, SensorEntity):
"""The platform class required by Home Assistant."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, device, api, gateway_id):
"""Initialize the device."""
super().__init__(device, api, gateway_id)
self._unique_id = f"{gateway_id}-{device.id}"
@property
def device_class(self):
"""Return the devices' state attributes."""
return DEVICE_CLASS_BATTERY
@property
def state(self):
"""Return the current state of the device."""
return self._device.device_info.battery_level
@property
def unit_of_measurement(self):
"""Return the unit_of_measurement of the device."""
return PERCENTAGE

View file

@ -118,6 +118,8 @@ def next_departuredate(departure):
class TrainSensor(SensorEntity):
"""Contains data about a train depature."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
def __init__(self, train_api, name, from_station, to_station, weekday, time):
"""Initialize the sensor."""
self._train_api = train_api
@ -176,11 +178,6 @@ class TrainSensor(SensorEntity):
ATTR_DEVIATIONS: deviations,
}
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TIMESTAMP
@property
def name(self):
"""Return the name of the sensor."""

View file

@ -240,6 +240,8 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
class UpCloudServerEntity(CoordinatorEntity):
"""Entity class for UpCloud servers."""
_attr_device_class = DEFAULT_COMPONENT_DEVICE_CLASS
def __init__(
self,
coordinator: DataUpdateCoordinator[dict[str, upcloud_api.Server]],
@ -284,11 +286,6 @@ class UpCloudServerEntity(CoordinatorEntity):
"""Return true if the server is on."""
return self.state == STATE_ON
@property
def device_class(self) -> str:
"""Return the class of this server."""
return DEFAULT_COMPONENT_DEVICE_CLASS
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the UpCloud server."""

View file

@ -54,6 +54,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class VultrBinarySensor(BinarySensorEntity):
"""Representation of a Vultr subscription sensor."""
_attr_device_class = DEFAULT_DEVICE_CLASS
def __init__(self, vultr, subscription, name):
"""Initialize a new Vultr binary sensor."""
self._vultr = vultr
@ -80,11 +82,6 @@ class VultrBinarySensor(BinarySensorEntity):
"""Return true if the binary sensor is on."""
return self.data["power_status"] == "running"
@property
def device_class(self):
"""Return the class of this sensor."""
return DEFAULT_DEVICE_CLASS
@property
def extra_state_attributes(self):
"""Return the state attributes of the Vultr subscription."""

View file

@ -73,6 +73,8 @@ class WashingtonStateTransportSensor(SensorEntity):
can read them and make them available.
"""
_attr_icon = ICON
def __init__(self, name, access_code):
"""Initialize the sensor."""
self._data = {}
@ -90,15 +92,12 @@ class WashingtonStateTransportSensor(SensorEntity):
"""Return the state of the sensor."""
return self._state
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
class WashingtonStateTravelTimeSensor(WashingtonStateTransportSensor):
"""Travel time sensor from WSDOT."""
_attr_unit_of_measurement = TIME_MINUTES
def __init__(self, name, access_code, travel_time_id):
"""Construct a travel time sensor."""
self._travel_time_id = travel_time_id
@ -135,11 +134,6 @@ class WashingtonStateTravelTimeSensor(WashingtonStateTransportSensor):
)
return attrs
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return TIME_MINUTES
def _parse_wsdot_timestamp(timestamp):
"""Convert WSDOT timestamp to datetime."""

View file

@ -369,6 +369,8 @@ class XBeeDigitalOut(XBeeDigitalIn):
class XBeeAnalogIn(SensorEntity):
"""Representation of a GPIO pin configured as an analog input."""
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, config, device):
"""Initialize the XBee analog in device."""
self._config = config
@ -418,11 +420,6 @@ class XBeeAnalogIn(SensorEntity):
"""Return the state of the entity."""
return self._value
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return PERCENTAGE
def update(self):
"""Get the latest reading from the ADC."""
try:

View file

@ -46,6 +46,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class XBeeTemperatureSensor(SensorEntity):
"""Representation of XBee Pro temperature sensor."""
_attr_unit_of_measurement = TEMP_CELSIUS
def __init__(self, config, device):
"""Initialize the sensor."""
self._config = config
@ -62,11 +64,6 @@ class XBeeTemperatureSensor(SensorEntity):
"""Return the state of the sensor."""
return self._temp
@property
def unit_of_measurement(self):
"""Return the unit of measurement the value is expressed in."""
return TEMP_CELSIUS
def update(self):
"""Get the latest data."""
try:

View file

@ -273,6 +273,9 @@ class XiaomiGatewaySensor(XiaomiGatewayDevice, SensorEntity):
class XiaomiGatewayIlluminanceSensor(SensorEntity):
"""Representation of the gateway device's illuminance sensor."""
_attr_device_class = DEVICE_CLASS_ILLUMINANCE
_attr_unit_of_measurement = UNIT_LUMEN
def __init__(self, gateway_device, gateway_name, gateway_device_id):
"""Initialize the entity."""
self._gateway = gateway_device
@ -302,16 +305,6 @@ class XiaomiGatewayIlluminanceSensor(SensorEntity):
"""Return true when state is known."""
return self._available
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity."""
return UNIT_LUMEN
@property
def device_class(self):
"""Return the device class of this entity."""
return DEVICE_CLASS_ILLUMINANCE
@property
def state(self):
"""Return the state of the device."""

View file

@ -262,6 +262,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
"""Representation of a XiaomiGatewaySwitch."""
_attr_device_class = DEVICE_CLASS_SWITCH
def __init__(self, coordinator, sub_device, entry, variable):
"""Initialize the XiaomiSensor."""
super().__init__(coordinator, sub_device, entry)
@ -270,11 +272,6 @@ class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
self._unique_id = f"{sub_device.sid}-ch{self._channel}"
self._name = f"{sub_device.name} ch{self._channel} ({sub_device.sid})"
@property
def device_class(self):
"""Return the device class of this entity."""
return DEVICE_CLASS_SWITCH
@property
def is_on(self):
"""Return true if switch is on."""

View file

@ -177,6 +177,8 @@ class ZhaCover(ZhaEntity, CoverEntity):
class Shade(ZhaEntity, CoverEntity):
"""ZHA Shade."""
_attr_device_class = DEVICE_CLASS_SHADE
def __init__(
self,
unique_id: str,
@ -199,11 +201,6 @@ class Shade(ZhaEntity, CoverEntity):
"""
return self._position
@property
def device_class(self) -> str | None:
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_SHADE
@property
def is_closed(self) -> bool | None:
"""Return True if shade is closed."""
@ -289,10 +286,7 @@ class Shade(ZhaEntity, CoverEntity):
class KeenVent(Shade):
"""Keen vent cover."""
@property
def device_class(self) -> str | None:
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_DAMPER
_attr_device_class = DEVICE_CLASS_DAMPER
async def async_open_cover(self, **kwargs):
"""Open the cover."""