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"
YEARLY_TYPE = "year"
ICON = "mdi:flash"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_USERNAME): cv.string,
@ -217,6 +215,8 @@ class AtomeData:
class AtomeSensor(SensorEntity):
"""Representation of a sensor entity for Atome."""
_attr_device_class = DEVICE_CLASS_POWER
def __init__(self, data, name, sensor_type):
"""Initialize the sensor."""
self._name = name
@ -251,16 +251,6 @@ class AtomeSensor(SensorEntity):
"""Return the 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):
"""Update device state."""
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):
"""Representation of a Bravia TV."""
_attr_device_class = DEVICE_CLASS_TV
def __init__(self, client, name, pin, unique_id, device_info, ignored_sources):
"""Initialize the Bravia TV device."""
@ -238,11 +240,6 @@ class BraviaTVDevice(MediaPlayerEntity):
"""Return the name of the device."""
return self._name
@property
def device_class(self):
"""Set the device class to TV."""
return DEVICE_CLASS_TV
@property
def unique_id(self):
"""Return a unique_id for this entity."""

View File

@ -36,6 +36,8 @@ async def async_setup_entry(
class FlickPricingSensor(SensorEntity):
"""Entity object for Flick Electric sensor."""
_attr_unit_of_measurement = UNIT_NAME
def __init__(self, api: FlickAPI) -> None:
"""Entity object for Flick Electric sensor."""
self._api: FlickAPI = api
@ -55,11 +57,6 @@ class FlickPricingSensor(SensorEntity):
"""Return the state of the sensor."""
return self._price.price
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return UNIT_NAME
@property
def extra_state_attributes(self):
"""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):
"""Base class for GreenEye Monitor sensors."""
_attr_should_poll = False
def __init__(self, monitor_serial_number, name, sensor_type, number):
"""Construct the entity."""
self._monitor_serial_number = monitor_serial_number
@ -96,11 +98,6 @@ class GEMSensor(SensorEntity):
self._sensor_type = sensor_type
self._number = number
@property
def should_poll(self):
"""GEM pushes changes, so this returns False."""
return False
@property
def unique_id(self):
"""Return a unique ID for this sensor."""
@ -148,6 +145,9 @@ class GEMSensor(SensorEntity):
class CurrentSensor(GEMSensor):
"""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):
"""Construct the entity."""
super().__init__(monitor_serial_number, name, "current", number)
@ -156,16 +156,6 @@ class CurrentSensor(GEMSensor):
def _get_sensor(self, monitor):
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
def state(self):
"""Return the current number of watts being used by the channel."""
@ -191,6 +181,8 @@ class CurrentSensor(GEMSensor):
class PulseCounter(GEMSensor):
"""Entity showing rate of change in one pulse counter of the monitor."""
_attr_icon = COUNTER_ICON
def __init__(
self,
monitor_serial_number,
@ -209,11 +201,6 @@ class PulseCounter(GEMSensor):
def _get_sensor(self, monitor):
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
def state(self):
"""Return the current rate of change for the given pulse counter."""
@ -253,6 +240,8 @@ class PulseCounter(GEMSensor):
class TemperatureSensor(GEMSensor):
"""Entity showing temperature from one temperature sensor."""
_attr_icon = TEMPERATURE_ICON
def __init__(self, monitor_serial_number, number, name, unit):
"""Construct the entity."""
super().__init__(monitor_serial_number, name, "temp", number)
@ -261,11 +250,6 @@ class TemperatureSensor(GEMSensor):
def _get_sensor(self, monitor):
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
def state(self):
"""Return the current temperature being reported by this sensor."""
@ -283,6 +267,9 @@ class TemperatureSensor(GEMSensor):
class VoltageSensor(GEMSensor):
"""Entity showing voltage."""
_attr_icon = VOLTAGE_ICON
_attr_unit_of_measurement = VOLT
def __init__(self, monitor_serial_number, number, name):
"""Construct the entity."""
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."""
return monitor
@property
def icon(self):
"""Return the icon that should represent this sensor in the UI."""
return VOLTAGE_ICON
@property
def state(self):
"""Return the current voltage being reported by this sensor."""
@ -303,8 +285,3 @@ class VoltageSensor(GEMSensor):
return None
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):
"""Implementation of a GTFS departure sensor."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
def __init__(
self,
gtfs: Any,
@ -576,11 +578,6 @@ class GTFSDepartureSensor(SensorEntity):
"""Icon to use in the frontend, if any."""
return self._icon
@property
def device_class(self) -> str:
"""Return the class of this device."""
return DEVICE_CLASS_TIMESTAMP
def update(self) -> None:
"""Get the latest data from GTFS and update the states."""
with self.lock:

View File

@ -32,6 +32,8 @@ def escape_characteristic_name(char_name):
class HomeKitEntity(Entity):
"""Representation of a Home Assistant HomeKit device."""
_attr_should_poll = False
def __init__(self, accessory, devinfo):
"""Initialise a generic HomeKit device."""
self._accessory = accessory
@ -99,14 +101,6 @@ class HomeKitEntity(Entity):
payload = self.service.build_update(characteristics)
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):
"""Configure an entity baed on its HomeKit characteristics metadata."""
self.pollable_characteristics = []

View File

@ -35,6 +35,8 @@ HA_MODE_TO_HK = {
class HomeKitHumidifier(HomeKitEntity, HumidifierEntity):
"""Representation of a HomeKit Controller Humidifier."""
_attr_device_class = DEVICE_CLASS_HUMIDIFIER
def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about."""
return [
@ -45,11 +47,6 @@ class HomeKitHumidifier(HomeKitEntity, HumidifierEntity):
CharacteristicsTypes.RELATIVE_HUMIDITY_HUMIDIFIER_THRESHOLD,
]
@property
def device_class(self) -> str:
"""Return the device class of the device."""
return DEVICE_CLASS_HUMIDIFIER
@property
def supported_features(self):
"""Return the list of supported features."""
@ -140,6 +137,8 @@ class HomeKitHumidifier(HomeKitEntity, HumidifierEntity):
class HomeKitDehumidifier(HomeKitEntity, HumidifierEntity):
"""Representation of a HomeKit Controller Humidifier."""
_attr_device_class = DEVICE_CLASS_DEHUMIDIFIER
def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about."""
return [
@ -151,11 +150,6 @@ class HomeKitDehumidifier(HomeKitEntity, HumidifierEntity):
CharacteristicsTypes.RELATIVE_HUMIDITY_DEHUMIDIFIER_THRESHOLD,
]
@property
def device_class(self) -> str:
"""Return the device class of the device."""
return DEVICE_CLASS_DEHUMIDIFIER
@property
def supported_features(self):
"""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):
"""Representation of a HomeKit Controller Television."""
_attr_device_class = DEVICE_CLASS_TV
def get_characteristic_types(self):
"""Define the homekit characteristics the entity cares about."""
return [
@ -70,11 +72,6 @@ class HomeKitTelevision(HomeKitEntity, MediaPlayerEntity):
CharacteristicsTypes.IDENTIFIER,
]
@property
def device_class(self):
"""Define the device class for a HomeKit enabled TV."""
return DEVICE_CLASS_TV
@property
def supported_features(self):
"""Flag media player features that are supported."""

View File

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

View File

@ -53,6 +53,9 @@ def add_entities(account, async_add_entities, tracked):
class IcloudDeviceBatterySensor(SensorEntity):
"""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:
"""Initialize the battery sensor."""
self._account = account
@ -69,21 +72,11 @@ class IcloudDeviceBatterySensor(SensorEntity):
"""Sensor name."""
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
def state(self) -> int:
"""Battery state percentage."""
return self._device.battery_level
@property
def unit_of_measurement(self) -> str:
"""Battery state measured in percentage."""
return PERCENTAGE
@property
def icon(self) -> str:
"""Battery state icon handling."""

View File

@ -184,6 +184,8 @@ class IPPPrinterSensor(IPPSensor):
class IPPUptimeSensor(IPPSensor):
"""Defines a IPP uptime sensor."""
_attr_device_class = DEVICE_CLASS_TIMESTAMP
def __init__(
self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator
) -> None:
@ -203,8 +205,3 @@ class IPPUptimeSensor(IPPSensor):
"""Return the state of the sensor."""
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
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):
"""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):
"""Initialize the Islamic prayer time sensor."""
self.sensor_type = sensor_type
@ -38,11 +42,6 @@ class IslamicPrayerTimeSensor(SensorEntity):
"""Return the unique id of the entity."""
return self.sensor_type
@property
def icon(self):
"""Icon to display in the front end."""
return PRAYER_TIMES_ICON
@property
def state(self):
"""Return the state of the sensor."""
@ -52,16 +51,6 @@ class IslamicPrayerTimeSensor(SensorEntity):
.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):
"""Handle entity which will be added."""
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):
"""Lightwave TRV Battery."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, name, lwlink, serial):
"""Initialize the Lightwave Trv battery sensor."""
self._name = name
@ -32,11 +35,6 @@ class LightwaveBattery(SensorEntity):
self._lwlink = lwlink
self._serial = serial
@property
def device_class(self):
"""Return the device class of the sensor."""
return DEVICE_CLASS_BATTERY
@property
def name(self):
"""Return the name of the sensor."""
@ -47,11 +45,6 @@ class LightwaveBattery(SensorEntity):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the state of the sensor."""
return PERCENTAGE
def update(self):
"""Communicate with a Lightwave RTF Proxy to get state."""
(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):
"""Representation of a Linode droplet sensor."""
_attr_device_class = DEVICE_CLASS_MOVING
def __init__(self, li, node_id):
"""Initialize a new Linode sensor."""
self._linode = li
@ -69,11 +71,6 @@ class LinodeBinarySensor(BinarySensorEntity):
"""Return true if the binary sensor is on."""
return self._state
@property
def device_class(self):
"""Return the class of this sensor."""
return DEVICE_CLASS_MOVING
@property
def extra_state_attributes(self):
"""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):
"""Representation of a MyQ gateway."""
_attr_device_class = DEVICE_CLASS_CONNECTIVITY
def __init__(self, coordinator, device):
"""Initialize with API object, device id."""
super().__init__(coordinator)
self._device = device
@property
def device_class(self):
"""We track connectivity for gateways."""
return DEVICE_CLASS_CONNECTIVITY
@property
def name(self):
"""Return the name of the garage door if any."""

View File

@ -18,8 +18,6 @@ CONF_AGENCY = "agency"
CONF_ROUTE = "route"
CONF_STOP = "stop"
ICON = "mdi:bus"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_AGENCY): cv.string,
@ -114,6 +112,9 @@ class NextBusDepartureSensor(SensorEntity):
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):
"""Initialize sensor with all required config."""
self.agency = agency
@ -144,11 +145,6 @@ class NextBusDepartureSensor(SensorEntity):
return self._name
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TIMESTAMP
@property
def state(self):
"""Return current state of the sensor."""
@ -159,13 +155,6 @@ class NextBusDepartureSensor(SensorEntity):
"""Return additional state 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):
"""Update sensor with new departures times."""
# 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):
"""Representation of a Nuki Lock Doorsensor."""
_attr_device_class = DEVICE_CLASS_DOOR
@property
def name(self):
"""Return the name of the lock."""
@ -66,8 +68,3 @@ class NukiDoorsensorEntity(NukiEntity, BinarySensorEntity):
def is_on(self):
"""Return true if the door is open."""
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):
"""Representation of the sensor."""
_attr_icon = SENSOR_ICON
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
def __init__(self, meter):
"""Initialize the sensor."""
self._state = None
@ -57,21 +60,11 @@ class CurrentEnergyUsageSensor(SensorEntity):
"""Return the name of the sensor."""
return SENSOR_NAME
@property
def icon(self):
"""Return the icon of the sensor."""
return SENSOR_ICON
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ENERGY_KILO_WATT_HOUR
def update(self):
"""Fetch new state data for the sensor."""
try:

View File

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

View File

@ -64,6 +64,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class SkybeaconHumid(SensorEntity):
"""Representation of a Skybeacon humidity sensor."""
_attr_unit_of_measurement = PERCENTAGE
def __init__(self, name, mon):
"""Initialize a sensor."""
self.mon = mon
@ -79,11 +81,6 @@ class SkybeaconHumid(SensorEntity):
"""Return the state of the device."""
return self.mon.data["humid"]
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return PERCENTAGE
@property
def extra_state_attributes(self):
"""Return the state attributes of the sensor."""
@ -93,6 +90,8 @@ class SkybeaconHumid(SensorEntity):
class SkybeaconTemp(SensorEntity):
"""Representation of a Skybeacon temperature sensor."""
_attr_unit_of_measurement = TEMP_CELSIUS
def __init__(self, name, mon):
"""Initialize a sensor."""
self.mon = mon
@ -108,11 +107,6 @@ class SkybeaconTemp(SensorEntity):
"""Return the state of the device."""
return self.mon.data["temp"]
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return TEMP_CELSIUS
@property
def extra_state_attributes(self):
"""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):
"""Representation a cover."""
_attr_device_class = DEVICE_CLASS_WINDOW
def __init__(self, cover):
"""Initialize a SmartHabCover."""
self._cover = cover
@ -69,11 +71,6 @@ class SmartHabCover(CoverEntity):
"""Return if the cover is closed or not."""
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):
"""Open the cover."""
await self._cover.async_open()

View File

@ -92,7 +92,8 @@ class UkTransportSensor(SensorEntity):
"""
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):
"""Initialize the sensor."""
@ -113,16 +114,6 @@ class UkTransportSensor(SensorEntity):
"""Return the state of the sensor."""
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):
"""Perform an API request."""
request_params = dict(
@ -144,7 +135,7 @@ class UkTransportSensor(SensorEntity):
class UkTransportLiveBusTimeSensor(UkTransportSensor):
"""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):
"""Construct a live bus time sensor."""
@ -206,7 +197,7 @@ class UkTransportLiveBusTimeSensor(UkTransportSensor):
class UkTransportLiveTrainTimeSensor(UkTransportSensor):
"""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):
"""Construct a live bus time sensor."""

View File

@ -86,16 +86,13 @@ class UniFiBandwidthSensor(UniFiClient, SensorEntity):
DOMAIN = DOMAIN
_attr_unit_of_measurement = DATA_MEGABYTES
@property
def name(self) -> str:
"""Return the name of the client."""
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:
"""Config entry options are updated, remove entity if option is disabled."""
if not self.controller.option_allow_bandwidth_sensors:
@ -134,10 +131,7 @@ class UniFiUpTimeSensor(UniFiClient, SensorEntity):
DOMAIN = DOMAIN
TYPE = UPTIME_SENSOR
@property
def device_class(self) -> str:
"""Return device class."""
return DEVICE_CLASS_TIMESTAMP
_attr_device_class = DEVICE_CLASS_TIMESTAMP
@property
def name(self) -> str:

View File

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