Collection of changing entity properties to class attributes - 2 (#51345)

pull/51354/head
Franck Nijhof 2021-06-01 21:43:55 +02:00 committed by GitHub
parent e5dff49440
commit fcdd8b11a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 70 additions and 215 deletions

View File

@ -39,8 +39,6 @@ WEEKLY_TYPE = "week"
MONTHLY_TYPE = "month" MONTHLY_TYPE = "month"
YEARLY_TYPE = "year" YEARLY_TYPE = "year"
ICON = "mdi:flash"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_USERNAME): cv.string,
@ -217,6 +215,8 @@ class AtomeData:
class AtomeSensor(SensorEntity): class AtomeSensor(SensorEntity):
"""Representation of a sensor entity for Atome.""" """Representation of a sensor entity for Atome."""
_attr_device_class = DEVICE_CLASS_POWER
def __init__(self, data, name, sensor_type): def __init__(self, data, name, sensor_type):
"""Initialize the sensor.""" """Initialize the sensor."""
self._name = name self._name = name
@ -251,16 +251,6 @@ class AtomeSensor(SensorEntity):
"""Return the unit of measurement.""" """Return the unit of measurement."""
return self._unit_of_measurement return self._unit_of_measurement
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_POWER
def update(self): def update(self):
"""Update device state.""" """Update device state."""
update_function = getattr(self._data, f"update_{self._sensor_type}_usage") update_function = getattr(self._data, f"update_{self._sensor_type}_usage")

View File

@ -121,6 +121,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class BraviaTVDevice(MediaPlayerEntity): class BraviaTVDevice(MediaPlayerEntity):
"""Representation of a Bravia TV.""" """Representation of a Bravia TV."""
_attr_device_class = DEVICE_CLASS_TV
def __init__(self, client, name, pin, unique_id, device_info, ignored_sources): def __init__(self, client, name, pin, unique_id, device_info, ignored_sources):
"""Initialize the Bravia TV device.""" """Initialize the Bravia TV device."""
@ -238,11 +240,6 @@ class BraviaTVDevice(MediaPlayerEntity):
"""Return the name of the device.""" """Return the name of the device."""
return self._name return self._name
@property
def device_class(self):
"""Set the device class to TV."""
return DEVICE_CLASS_TV
@property @property
def unique_id(self): def unique_id(self):
"""Return a unique_id for this entity.""" """Return a unique_id for this entity."""

View File

@ -36,6 +36,8 @@ async def async_setup_entry(
class FlickPricingSensor(SensorEntity): class FlickPricingSensor(SensorEntity):
"""Entity object for Flick Electric sensor.""" """Entity object for Flick Electric sensor."""
_attr_unit_of_measurement = UNIT_NAME
def __init__(self, api: FlickAPI) -> None: def __init__(self, api: FlickAPI) -> None:
"""Entity object for Flick Electric sensor.""" """Entity object for Flick Electric sensor."""
self._api: FlickAPI = api self._api: FlickAPI = api
@ -55,11 +57,6 @@ class FlickPricingSensor(SensorEntity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._price.price return self._price.price
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return UNIT_NAME
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""

View File

@ -88,6 +88,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class GEMSensor(SensorEntity): class GEMSensor(SensorEntity):
"""Base class for GreenEye Monitor sensors.""" """Base class for GreenEye Monitor sensors."""
_attr_should_poll = False
def __init__(self, monitor_serial_number, name, sensor_type, number): def __init__(self, monitor_serial_number, name, sensor_type, number):
"""Construct the entity.""" """Construct the entity."""
self._monitor_serial_number = monitor_serial_number self._monitor_serial_number = monitor_serial_number
@ -96,11 +98,6 @@ class GEMSensor(SensorEntity):
self._sensor_type = sensor_type self._sensor_type = sensor_type
self._number = number self._number = number
@property
def should_poll(self):
"""GEM pushes changes, so this returns False."""
return False
@property @property
def unique_id(self): def unique_id(self):
"""Return a unique ID for this sensor.""" """Return a unique ID for this sensor."""
@ -148,6 +145,9 @@ class GEMSensor(SensorEntity):
class CurrentSensor(GEMSensor): class CurrentSensor(GEMSensor):
"""Entity showing power usage on one channel of the monitor.""" """Entity showing power usage on one channel of the monitor."""
_attr_icon = CURRENT_SENSOR_ICON
_attr_unit_of_measurement = UNIT_WATTS
def __init__(self, monitor_serial_number, number, name, net_metering): def __init__(self, monitor_serial_number, number, name, net_metering):
"""Construct the entity.""" """Construct the entity."""
super().__init__(monitor_serial_number, name, "current", number) super().__init__(monitor_serial_number, name, "current", number)
@ -156,16 +156,6 @@ class CurrentSensor(GEMSensor):
def _get_sensor(self, monitor): def _get_sensor(self, monitor):
return monitor.channels[self._number - 1] return monitor.channels[self._number - 1]
@property
def icon(self):
"""Return the icon that should represent this sensor in the UI."""
return CURRENT_SENSOR_ICON
@property
def unit_of_measurement(self):
"""Return the unit of measurement used by this sensor."""
return UNIT_WATTS
@property @property
def state(self): def state(self):
"""Return the current number of watts being used by the channel.""" """Return the current number of watts being used by the channel."""
@ -191,6 +181,8 @@ class CurrentSensor(GEMSensor):
class PulseCounter(GEMSensor): class PulseCounter(GEMSensor):
"""Entity showing rate of change in one pulse counter of the monitor.""" """Entity showing rate of change in one pulse counter of the monitor."""
_attr_icon = COUNTER_ICON
def __init__( def __init__(
self, self,
monitor_serial_number, monitor_serial_number,
@ -209,11 +201,6 @@ class PulseCounter(GEMSensor):
def _get_sensor(self, monitor): def _get_sensor(self, monitor):
return monitor.pulse_counters[self._number - 1] return monitor.pulse_counters[self._number - 1]
@property
def icon(self):
"""Return the icon that should represent this sensor in the UI."""
return COUNTER_ICON
@property @property
def state(self): def state(self):
"""Return the current rate of change for the given pulse counter.""" """Return the current rate of change for the given pulse counter."""
@ -253,6 +240,8 @@ class PulseCounter(GEMSensor):
class TemperatureSensor(GEMSensor): class TemperatureSensor(GEMSensor):
"""Entity showing temperature from one temperature sensor.""" """Entity showing temperature from one temperature sensor."""
_attr_icon = TEMPERATURE_ICON
def __init__(self, monitor_serial_number, number, name, unit): def __init__(self, monitor_serial_number, number, name, unit):
"""Construct the entity.""" """Construct the entity."""
super().__init__(monitor_serial_number, name, "temp", number) super().__init__(monitor_serial_number, name, "temp", number)
@ -261,11 +250,6 @@ class TemperatureSensor(GEMSensor):
def _get_sensor(self, monitor): def _get_sensor(self, monitor):
return monitor.temperature_sensors[self._number - 1] return monitor.temperature_sensors[self._number - 1]
@property
def icon(self):
"""Return the icon that should represent this sensor in the UI."""
return TEMPERATURE_ICON
@property @property
def state(self): def state(self):
"""Return the current temperature being reported by this sensor.""" """Return the current temperature being reported by this sensor."""
@ -283,6 +267,9 @@ class TemperatureSensor(GEMSensor):
class VoltageSensor(GEMSensor): class VoltageSensor(GEMSensor):
"""Entity showing voltage.""" """Entity showing voltage."""
_attr_icon = VOLTAGE_ICON
_attr_unit_of_measurement = VOLT
def __init__(self, monitor_serial_number, number, name): def __init__(self, monitor_serial_number, number, name):
"""Construct the entity.""" """Construct the entity."""
super().__init__(monitor_serial_number, name, "volts", number) super().__init__(monitor_serial_number, name, "volts", number)
@ -291,11 +278,6 @@ class VoltageSensor(GEMSensor):
"""Wire the updates to the monitor itself, since there is no voltage element in the API.""" """Wire the updates to the monitor itself, since there is no voltage element in the API."""
return monitor return monitor
@property
def icon(self):
"""Return the icon that should represent this sensor in the UI."""
return VOLTAGE_ICON
@property @property
def state(self): def state(self):
"""Return the current voltage being reported by this sensor.""" """Return the current voltage being reported by this sensor."""
@ -303,8 +285,3 @@ class VoltageSensor(GEMSensor):
return None return None
return self._sensor.voltage return self._sensor.voltage
@property
def unit_of_measurement(self):
"""Return the unit of measurement for this sensor."""
return VOLT

View File

@ -518,6 +518,8 @@ def setup_platform(
class GTFSDepartureSensor(SensorEntity): class GTFSDepartureSensor(SensorEntity):
"""Implementation of a GTFS departure sensor.""" """Implementation of a GTFS departure sensor."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
def __init__( def __init__(
self, self,
gtfs: Any, gtfs: Any,
@ -576,11 +578,6 @@ class GTFSDepartureSensor(SensorEntity):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""
return self._icon return self._icon
@property
def device_class(self) -> str:
"""Return the class of this device."""
return DEVICE_CLASS_TIMESTAMP
def update(self) -> None: def update(self) -> None:
"""Get the latest data from GTFS and update the states.""" """Get the latest data from GTFS and update the states."""
with self.lock: with self.lock:

View File

@ -32,6 +32,8 @@ def escape_characteristic_name(char_name):
class HomeKitEntity(Entity): class HomeKitEntity(Entity):
"""Representation of a Home Assistant HomeKit device.""" """Representation of a Home Assistant HomeKit device."""
_attr_should_poll = False
def __init__(self, accessory, devinfo): def __init__(self, accessory, devinfo):
"""Initialise a generic HomeKit device.""" """Initialise a generic HomeKit device."""
self._accessory = accessory self._accessory = accessory
@ -99,14 +101,6 @@ class HomeKitEntity(Entity):
payload = self.service.build_update(characteristics) payload = self.service.build_update(characteristics)
return await self._accessory.put_characteristics(payload) return await self._accessory.put_characteristics(payload)
@property
def should_poll(self) -> bool:
"""Return False.
Data update is triggered from HKDevice.
"""
return False
def setup(self): def setup(self):
"""Configure an entity baed on its HomeKit characteristics metadata.""" """Configure an entity baed on its HomeKit characteristics metadata."""
self.pollable_characteristics = [] self.pollable_characteristics = []

View File

@ -35,6 +35,8 @@ HA_MODE_TO_HK = {
class HomeKitHumidifier(HomeKitEntity, HumidifierEntity): class HomeKitHumidifier(HomeKitEntity, HumidifierEntity):
"""Representation of a HomeKit Controller Humidifier.""" """Representation of a HomeKit Controller Humidifier."""
_attr_device_class = DEVICE_CLASS_HUMIDIFIER
def get_characteristic_types(self): def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about.""" """Define the homekit characteristics the entity cares about."""
return [ return [
@ -45,11 +47,6 @@ class HomeKitHumidifier(HomeKitEntity, HumidifierEntity):
CharacteristicsTypes.RELATIVE_HUMIDITY_HUMIDIFIER_THRESHOLD, CharacteristicsTypes.RELATIVE_HUMIDITY_HUMIDIFIER_THRESHOLD,
] ]
@property
def device_class(self) -> str:
"""Return the device class of the device."""
return DEVICE_CLASS_HUMIDIFIER
@property @property
def supported_features(self): def supported_features(self):
"""Return the list of supported features.""" """Return the list of supported features."""
@ -140,6 +137,8 @@ class HomeKitHumidifier(HomeKitEntity, HumidifierEntity):
class HomeKitDehumidifier(HomeKitEntity, HumidifierEntity): class HomeKitDehumidifier(HomeKitEntity, HumidifierEntity):
"""Representation of a HomeKit Controller Humidifier.""" """Representation of a HomeKit Controller Humidifier."""
_attr_device_class = DEVICE_CLASS_DEHUMIDIFIER
def get_characteristic_types(self): def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about.""" """Define the homekit characteristics the entity cares about."""
return [ return [
@ -151,11 +150,6 @@ class HomeKitDehumidifier(HomeKitEntity, HumidifierEntity):
CharacteristicsTypes.RELATIVE_HUMIDITY_DEHUMIDIFIER_THRESHOLD, CharacteristicsTypes.RELATIVE_HUMIDITY_DEHUMIDIFIER_THRESHOLD,
] ]
@property
def device_class(self) -> str:
"""Return the device class of the device."""
return DEVICE_CLASS_DEHUMIDIFIER
@property @property
def supported_features(self): def supported_features(self):
"""Return the list of supported features.""" """Return the list of supported features."""

View File

@ -57,6 +57,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class HomeKitTelevision(HomeKitEntity, MediaPlayerEntity): class HomeKitTelevision(HomeKitEntity, MediaPlayerEntity):
"""Representation of a HomeKit Controller Television.""" """Representation of a HomeKit Controller Television."""
_attr_device_class = DEVICE_CLASS_TV
def get_characteristic_types(self): def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about.""" """Define the homekit characteristics the entity cares about."""
return [ return [
@ -70,11 +72,6 @@ class HomeKitTelevision(HomeKitEntity, MediaPlayerEntity):
CharacteristicsTypes.IDENTIFIER, CharacteristicsTypes.IDENTIFIER,
] ]
@property
def device_class(self):
"""Define the device class for a HomeKit enabled TV."""
return DEVICE_CLASS_TV
@property @property
def supported_features(self): def supported_features(self):
"""Flag media player features that are supported.""" """Flag media player features that are supported."""

View File

@ -74,10 +74,7 @@ class HMBinarySensor(HMDevice, BinarySensorEntity):
class HMBatterySensor(HMDevice, BinarySensorEntity): class HMBatterySensor(HMDevice, BinarySensorEntity):
"""Representation of an HomeMatic low battery sensor.""" """Representation of an HomeMatic low battery sensor."""
@property _attr_device_class = DEVICE_CLASS_BATTERY
def device_class(self):
"""Return battery as a device class."""
return DEVICE_CLASS_BATTERY
@property @property
def is_on(self): def is_on(self):

View File

@ -53,6 +53,9 @@ def add_entities(account, async_add_entities, tracked):
class IcloudDeviceBatterySensor(SensorEntity): class IcloudDeviceBatterySensor(SensorEntity):
"""Representation of a iCloud device battery sensor.""" """Representation of a iCloud device battery sensor."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, account: IcloudAccount, device: IcloudDevice) -> None: def __init__(self, account: IcloudAccount, device: IcloudDevice) -> None:
"""Initialize the battery sensor.""" """Initialize the battery sensor."""
self._account = account self._account = account
@ -69,21 +72,11 @@ class IcloudDeviceBatterySensor(SensorEntity):
"""Sensor name.""" """Sensor name."""
return f"{self._device.name} battery state" return f"{self._device.name} battery state"
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_BATTERY
@property @property
def state(self) -> int: def state(self) -> int:
"""Battery state percentage.""" """Battery state percentage."""
return self._device.battery_level return self._device.battery_level
@property
def unit_of_measurement(self) -> str:
"""Battery state measured in percentage."""
return PERCENTAGE
@property @property
def icon(self) -> str: def icon(self) -> str:
"""Battery state icon handling.""" """Battery state icon handling."""

View File

@ -184,6 +184,8 @@ class IPPPrinterSensor(IPPSensor):
class IPPUptimeSensor(IPPSensor): class IPPUptimeSensor(IPPSensor):
"""Defines a IPP uptime sensor.""" """Defines a IPP uptime sensor."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
def __init__( def __init__(
self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator
) -> None: ) -> None:
@ -203,8 +205,3 @@ class IPPUptimeSensor(IPPSensor):
"""Return the state of the sensor.""" """Return the state of the sensor."""
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime) uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
return uptime.replace(microsecond=0).isoformat() return uptime.replace(microsecond=0).isoformat()
@property
def device_class(self) -> str | None:
"""Return the class of this sensor."""
return DEVICE_CLASS_TIMESTAMP

View File

@ -23,6 +23,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class IslamicPrayerTimeSensor(SensorEntity): class IslamicPrayerTimeSensor(SensorEntity):
"""Representation of an Islamic prayer time sensor.""" """Representation of an Islamic prayer time sensor."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
_attr_icon = PRAYER_TIMES_ICON
_attr_should_poll = False
def __init__(self, sensor_type, client): def __init__(self, sensor_type, client):
"""Initialize the Islamic prayer time sensor.""" """Initialize the Islamic prayer time sensor."""
self.sensor_type = sensor_type self.sensor_type = sensor_type
@ -38,11 +42,6 @@ class IslamicPrayerTimeSensor(SensorEntity):
"""Return the unique id of the entity.""" """Return the unique id of the entity."""
return self.sensor_type return self.sensor_type
@property
def icon(self):
"""Icon to display in the front end."""
return PRAYER_TIMES_ICON
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
@ -52,16 +51,6 @@ class IslamicPrayerTimeSensor(SensorEntity):
.isoformat() .isoformat()
) )
@property
def should_poll(self):
"""Disable polling."""
return False
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TIMESTAMP
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Handle entity which will be added.""" """Handle entity which will be added."""
self.async_on_remove( self.async_on_remove(

View File

@ -25,6 +25,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class LightwaveBattery(SensorEntity): class LightwaveBattery(SensorEntity):
"""Lightwave TRV Battery.""" """Lightwave TRV Battery."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, name, lwlink, serial): def __init__(self, name, lwlink, serial):
"""Initialize the Lightwave Trv battery sensor.""" """Initialize the Lightwave Trv battery sensor."""
self._name = name self._name = name
@ -32,11 +35,6 @@ class LightwaveBattery(SensorEntity):
self._lwlink = lwlink self._lwlink = lwlink
self._serial = serial self._serial = serial
@property
def device_class(self):
"""Return the device class of the sensor."""
return DEVICE_CLASS_BATTERY
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
@ -47,11 +45,6 @@ class LightwaveBattery(SensorEntity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@property
def unit_of_measurement(self):
"""Return the state of the sensor."""
return PERCENTAGE
def update(self): def update(self):
"""Communicate with a Lightwave RTF Proxy to get state.""" """Communicate with a Lightwave RTF Proxy to get state."""
(dummy_temp, dummy_targ, battery, dummy_output) = self._lwlink.read_trv_status( (dummy_temp, dummy_targ, battery, dummy_output) = self._lwlink.read_trv_status(

View File

@ -50,6 +50,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class LinodeBinarySensor(BinarySensorEntity): class LinodeBinarySensor(BinarySensorEntity):
"""Representation of a Linode droplet sensor.""" """Representation of a Linode droplet sensor."""
_attr_device_class = DEVICE_CLASS_MOVING
def __init__(self, li, node_id): def __init__(self, li, node_id):
"""Initialize a new Linode sensor.""" """Initialize a new Linode sensor."""
self._linode = li self._linode = li
@ -69,11 +71,6 @@ class LinodeBinarySensor(BinarySensorEntity):
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return self._state return self._state
@property
def device_class(self):
"""Return the class of this sensor."""
return DEVICE_CLASS_MOVING
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes of the Linode Node.""" """Return the state attributes of the Linode Node."""

View File

@ -32,16 +32,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class MyQBinarySensorEntity(CoordinatorEntity, BinarySensorEntity): class MyQBinarySensorEntity(CoordinatorEntity, BinarySensorEntity):
"""Representation of a MyQ gateway.""" """Representation of a MyQ gateway."""
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
def __init__(self, coordinator, device): def __init__(self, coordinator, device):
"""Initialize with API object, device id.""" """Initialize with API object, device id."""
super().__init__(coordinator) super().__init__(coordinator)
self._device = device self._device = device
@property
def device_class(self):
"""We track connectivity for gateways."""
return DEVICE_CLASS_CONNECTIVITY
@property @property
def name(self): def name(self):
"""Return the name of the garage door if any.""" """Return the name of the garage door if any."""

View File

@ -18,8 +18,6 @@ CONF_AGENCY = "agency"
CONF_ROUTE = "route" CONF_ROUTE = "route"
CONF_STOP = "stop" CONF_STOP = "stop"
ICON = "mdi:bus"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_AGENCY): cv.string, vol.Required(CONF_AGENCY): cv.string,
@ -114,6 +112,9 @@ class NextBusDepartureSensor(SensorEntity):
the future using fuzzy logic and matching. the future using fuzzy logic and matching.
""" """
_attr_device_class = DEVICE_CLASS_TIMESTAMP
_attr_icon = "mdi:bus"
def __init__(self, client, agency, route, stop, name=None): def __init__(self, client, agency, route, stop, name=None):
"""Initialize sensor with all required config.""" """Initialize sensor with all required config."""
self.agency = agency self.agency = agency
@ -144,11 +145,6 @@ class NextBusDepartureSensor(SensorEntity):
return self._name return self._name
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TIMESTAMP
@property @property
def state(self): def state(self):
"""Return current state of the sensor.""" """Return current state of the sensor."""
@ -159,13 +155,6 @@ class NextBusDepartureSensor(SensorEntity):
"""Return additional state attributes.""" """Return additional state attributes."""
return self._attributes return self._attributes
@property
def icon(self):
"""Return icon to be used for this sensor."""
# Would be nice if we could determine if the line is a train or bus
# however that doesn't seem to be available to us. Using bus for now.
return ICON
def update(self): def update(self):
"""Update sensor with new departures times.""" """Update sensor with new departures times."""
# Note: using Multi because there is a bug with the single stop impl # Note: using Multi because there is a bug with the single stop impl

View File

@ -29,6 +29,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
class NukiDoorsensorEntity(NukiEntity, BinarySensorEntity): class NukiDoorsensorEntity(NukiEntity, BinarySensorEntity):
"""Representation of a Nuki Lock Doorsensor.""" """Representation of a Nuki Lock Doorsensor."""
_attr_device_class = DEVICE_CLASS_DOOR
@property @property
def name(self): def name(self):
"""Return the name of the lock.""" """Return the name of the lock."""
@ -66,8 +68,3 @@ class NukiDoorsensorEntity(NukiEntity, BinarySensorEntity):
def is_on(self): def is_on(self):
"""Return true if the door is open.""" """Return true if the door is open."""
return self.door_sensor_state == STATE_DOORSENSOR_OPENED return self.door_sensor_state == STATE_DOORSENSOR_OPENED
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_DOOR

View File

@ -41,6 +41,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class CurrentEnergyUsageSensor(SensorEntity): class CurrentEnergyUsageSensor(SensorEntity):
"""Representation of the sensor.""" """Representation of the sensor."""
_attr_icon = SENSOR_ICON
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
def __init__(self, meter): def __init__(self, meter):
"""Initialize the sensor.""" """Initialize the sensor."""
self._state = None self._state = None
@ -57,21 +60,11 @@ class CurrentEnergyUsageSensor(SensorEntity):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return SENSOR_NAME return SENSOR_NAME
@property
def icon(self):
"""Return the icon of the sensor."""
return SENSOR_ICON
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ENERGY_KILO_WATT_HOUR
def update(self): def update(self):
"""Fetch new state data for the sensor.""" """Fetch new state data for the sensor."""
try: try:

View File

@ -123,6 +123,8 @@ async def async_setup_entry(
class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity): class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
"""Representation of a Philips TV exposing the JointSpace API.""" """Representation of a Philips TV exposing the JointSpace API."""
_attr_device_class = DEVICE_CLASS_TV
def __init__( def __init__(
self, self,
coordinator: PhilipsTVDataUpdateCoordinator, coordinator: PhilipsTVDataUpdateCoordinator,
@ -315,11 +317,6 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
if app: if app:
return app.get("label") return app.get("label")
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TV
@property @property
def unique_id(self): def unique_id(self):
"""Return unique identifier if known.""" """Return unique identifier if known."""

View File

@ -64,6 +64,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class SkybeaconHumid(SensorEntity): class SkybeaconHumid(SensorEntity):
"""Representation of a Skybeacon humidity sensor.""" """Representation of a Skybeacon humidity sensor."""
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, name, mon): def __init__(self, name, mon):
"""Initialize a sensor.""" """Initialize a sensor."""
self.mon = mon self.mon = mon
@ -79,11 +81,6 @@ class SkybeaconHumid(SensorEntity):
"""Return the state of the device.""" """Return the state of the device."""
return self.mon.data["humid"] return self.mon.data["humid"]
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return PERCENTAGE
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
@ -93,6 +90,8 @@ class SkybeaconHumid(SensorEntity):
class SkybeaconTemp(SensorEntity): class SkybeaconTemp(SensorEntity):
"""Representation of a Skybeacon temperature sensor.""" """Representation of a Skybeacon temperature sensor."""
_attr_unit_of_measurement = TEMP_CELSIUS
def __init__(self, name, mon): def __init__(self, name, mon):
"""Initialize a sensor.""" """Initialize a sensor."""
self.mon = mon self.mon = mon
@ -108,11 +107,6 @@ class SkybeaconTemp(SensorEntity):
"""Return the state of the device.""" """Return the state of the device."""
return self.mon.data["temp"] return self.mon.data["temp"]
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return TEMP_CELSIUS
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""

View File

@ -37,6 +37,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class SmartHabCover(CoverEntity): class SmartHabCover(CoverEntity):
"""Representation a cover.""" """Representation a cover."""
_attr_device_class = DEVICE_CLASS_WINDOW
def __init__(self, cover): def __init__(self, cover):
"""Initialize a SmartHabCover.""" """Initialize a SmartHabCover."""
self._cover = cover self._cover = cover
@ -69,11 +71,6 @@ class SmartHabCover(CoverEntity):
"""Return if the cover is closed or not.""" """Return if the cover is closed or not."""
return self._cover.state == 0 return self._cover.state == 0
@property
def device_class(self) -> str:
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_WINDOW
async def async_open_cover(self, **kwargs): async def async_open_cover(self, **kwargs):
"""Open the cover.""" """Open the cover."""
await self._cover.async_open() await self._cover.async_open()

View File

@ -92,7 +92,8 @@ class UkTransportSensor(SensorEntity):
""" """
TRANSPORT_API_URL_BASE = "https://transportapi.com/v3/uk/" TRANSPORT_API_URL_BASE = "https://transportapi.com/v3/uk/"
ICON = "mdi:train" _attr_icon = "mdi:train"
_attr_unit_of_measurement = TIME_MINUTES
def __init__(self, name, api_app_id, api_app_key, url): def __init__(self, name, api_app_id, api_app_key, url):
"""Initialize the sensor.""" """Initialize the sensor."""
@ -113,16 +114,6 @@ class UkTransportSensor(SensorEntity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return TIME_MINUTES
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self.ICON
def _do_api_request(self, params): def _do_api_request(self, params):
"""Perform an API request.""" """Perform an API request."""
request_params = dict( request_params = dict(
@ -144,7 +135,7 @@ class UkTransportSensor(SensorEntity):
class UkTransportLiveBusTimeSensor(UkTransportSensor): class UkTransportLiveBusTimeSensor(UkTransportSensor):
"""Live bus time sensor from UK transportapi.com.""" """Live bus time sensor from UK transportapi.com."""
ICON = "mdi:bus" _attr_icon = "mdi:bus"
def __init__(self, api_app_id, api_app_key, stop_atcocode, bus_direction, interval): def __init__(self, api_app_id, api_app_key, stop_atcocode, bus_direction, interval):
"""Construct a live bus time sensor.""" """Construct a live bus time sensor."""
@ -206,7 +197,7 @@ class UkTransportLiveBusTimeSensor(UkTransportSensor):
class UkTransportLiveTrainTimeSensor(UkTransportSensor): class UkTransportLiveTrainTimeSensor(UkTransportSensor):
"""Live train time sensor from UK transportapi.com.""" """Live train time sensor from UK transportapi.com."""
ICON = "mdi:train" _attr_icon = "mdi:train"
def __init__(self, api_app_id, api_app_key, station_code, calling_at, interval): def __init__(self, api_app_id, api_app_key, station_code, calling_at, interval):
"""Construct a live bus time sensor.""" """Construct a live bus time sensor."""

View File

@ -86,16 +86,13 @@ class UniFiBandwidthSensor(UniFiClient, SensorEntity):
DOMAIN = DOMAIN DOMAIN = DOMAIN
_attr_unit_of_measurement = DATA_MEGABYTES
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the client.""" """Return the name of the client."""
return f"{super().name} {self.TYPE.upper()}" return f"{super().name} {self.TYPE.upper()}"
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity."""
return DATA_MEGABYTES
async def options_updated(self) -> None: async def options_updated(self) -> None:
"""Config entry options are updated, remove entity if option is disabled.""" """Config entry options are updated, remove entity if option is disabled."""
if not self.controller.option_allow_bandwidth_sensors: if not self.controller.option_allow_bandwidth_sensors:
@ -134,10 +131,7 @@ class UniFiUpTimeSensor(UniFiClient, SensorEntity):
DOMAIN = DOMAIN DOMAIN = DOMAIN
TYPE = UPTIME_SENSOR TYPE = UPTIME_SENSOR
@property _attr_device_class = DEVICE_CLASS_TIMESTAMP
def device_class(self) -> str:
"""Return device class."""
return DEVICE_CLASS_TIMESTAMP
@property @property
def name(self) -> str: def name(self) -> str:

View File

@ -29,12 +29,9 @@ async def async_setup_entry(
class WithingsHealthBinarySensor(BaseWithingsSensor, BinarySensorEntity): class WithingsHealthBinarySensor(BaseWithingsSensor, BinarySensorEntity):
"""Implementation of a Withings sensor.""" """Implementation of a Withings sensor."""
_attr_device_class = DEVICE_CLASS_OCCUPANCY
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return self._state_data return self._state_data
@property
def device_class(self) -> str:
"""Provide the device class."""
return DEVICE_CLASS_OCCUPANCY