Move attribution to standalone attribute [e-g] (#80513)

pull/80516/head
epenet 2022-10-18 10:04:54 +02:00 committed by GitHub
parent cb530e398c
commit c717fd19de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 47 deletions

View File

@ -7,7 +7,7 @@ import async_timeout
from homeassistant.components.sensor import SensorEntity, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION, LENGTH_METERS
from homeassistant.const import LENGTH_METERS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceEntryType
@ -90,7 +90,10 @@ async def async_setup_entry(
class Measurement(CoordinatorEntity, SensorEntity):
"""A gauge at a flood monitoring station."""
attribution = "This uses Environment Agency flood and river level data from the real-time data API"
_attr_attribution = (
"This uses Environment Agency flood and river level data "
"from the real-time data API"
)
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(self, coordinator, key):
@ -166,11 +169,6 @@ class Measurement(CoordinatorEntity, SensorEntity):
return None
return UNIT_MAPPING.get(measure["unit"], measure["unitName"])
@property
def extra_state_attributes(self):
"""Return the sensor specific state attributes."""
return {ATTR_ATTRIBUTION: self.attribution}
@property
def native_value(self):
"""Return the current sensor value."""

View File

@ -8,7 +8,6 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
@ -25,8 +24,6 @@ import homeassistant.util.dt as dt_util
API_CLIENT_NAME = "homeassistant-homeassistant"
ATTRIBUTION = "Data provided by entur.org under NLOD"
CONF_STOP_IDS = "stop_ids"
CONF_EXPAND_PLATFORMS = "expand_platforms"
CONF_WHITELIST_LINES = "line_whitelist"
@ -160,6 +157,8 @@ class EnturProxy:
class EnturPublicTransportSensor(SensorEntity):
"""Implementation of a Entur public transport sensor."""
_attr_attribution = "Data provided by entur.org under NLOD"
def __init__(
self, api: EnturProxy, name: str, stop: str, show_on_map: bool
) -> None:
@ -185,7 +184,6 @@ class EnturPublicTransportSensor(SensorEntity):
@property
def extra_state_attributes(self) -> dict:
"""Return the state attributes."""
self._attributes[ATTR_ATTRIBUTION] = ATTRIBUTION
self._attributes[ATTR_STOP_ID] = self._stop
return self._attributes

View File

@ -7,14 +7,12 @@ from pyetherscan import get_balance
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION, CONF_ADDRESS, CONF_NAME, CONF_TOKEN
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_TOKEN
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
ATTRIBUTION = "Data provided by etherscan.io"
CONF_TOKEN_ADDRESS = "token_address"
SCAN_INTERVAL = timedelta(minutes=5)
@ -54,6 +52,8 @@ def setup_platform(
class EtherscanSensor(SensorEntity):
"""Representation of an Etherscan.io sensor."""
_attr_attribution = "Data provided by etherscan.io"
def __init__(self, name, address, token, token_address):
"""Initialize the sensor."""
self._name = name
@ -78,11 +78,6 @@ class EtherscanSensor(SensorEntity):
"""Return the unit of measurement this sensor expresses itself in."""
return self._unit_of_measurement
@property
def extra_state_attributes(self):
"""Return the state attributes of the sensor."""
return {ATTR_ATTRIBUTION: ATTRIBUTION}
def update(self) -> None:
"""Get the latest state of the sensor."""

View File

@ -19,12 +19,7 @@ from homeassistant.components.sensor import (
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
SensorEntity,
)
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_UNIT_SYSTEM,
)
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_UNIT_SYSTEM
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -345,6 +340,7 @@ class FitbitSensor(SensorEntity):
"""Implementation of a Fitbit sensor."""
entity_description: FitbitSensorEntityDescription
_attr_attribution = ATTRIBUTION
def __init__(
self,
@ -396,7 +392,7 @@ class FitbitSensor(SensorEntity):
@property
def extra_state_attributes(self) -> dict[str, str | None]:
"""Return the state attributes."""
attrs: dict[str, str | None] = {ATTR_ATTRIBUTION: ATTRIBUTION}
attrs: dict[str, str | None] = {}
if self.extra is not None:
attrs["model"] = self.extra.get("deviceVersion")

View File

@ -8,12 +8,7 @@ from pyflick import FlickAPI, FlickPrice
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_FRIENDLY_NAME,
CURRENCY_CENT,
ENERGY_KILO_WATT_HOUR,
)
from homeassistant.const import ATTR_FRIENDLY_NAME, CURRENCY_CENT, ENERGY_KILO_WATT_HOUR
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utcnow
@ -24,7 +19,6 @@ _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(minutes=5)
ATTRIBUTION = "Data provided by Flick Electric"
FRIENDLY_NAME = "Flick Power Price"
@ -40,6 +34,7 @@ async def async_setup_entry(
class FlickPricingSensor(SensorEntity):
"""Entity object for Flick Electric sensor."""
_attr_attribution = "Data provided by Flick Electric"
_attr_native_unit_of_measurement = f"{CURRENCY_CENT}/{ENERGY_KILO_WATT_HOUR}"
def __init__(self, api: FlickAPI) -> None:
@ -47,7 +42,6 @@ class FlickPricingSensor(SensorEntity):
self._api: FlickAPI = api
self._price: FlickPrice = None
self._attributes: dict[str, Any] = {
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_FRIENDLY_NAME: FRIENDLY_NAME,
}

View File

@ -6,7 +6,6 @@ import logging
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_LATITUDE,
ATTR_LONGITUDE,
LENGTH_KILOMETERS,
@ -124,7 +123,7 @@ class GeonetnzVolcanoSensor(SensorEntity):
self._distance = round(feed_entry.distance_to_home, 1)
self._latitude = round(feed_entry.coordinates[0], 5)
self._longitude = round(feed_entry.coordinates[1], 5)
self._attribution = feed_entry.attribution
self._attr_attribution = feed_entry.attribution
self._alert_level = feed_entry.alert_level
self._activity = feed_entry.activity
self._hazards = feed_entry.hazards
@ -159,7 +158,6 @@ class GeonetnzVolcanoSensor(SensorEntity):
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_ATTRIBUTION, self._attribution),
(ATTR_ACTIVITY, self._activity),
(ATTR_HAZARDS, self._hazards),
(ATTR_LONGITUDE, self._longitude),

View File

@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_NAME,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONF_NAME,
@ -154,6 +153,7 @@ async def async_setup_entry(
class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity):
"""Define an GIOS sensor."""
_attr_attribution = ATTRIBUTION
_attr_has_entity_name = True
entity_description: GiosSensorEntityDescription
@ -174,7 +174,6 @@ class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity):
)
self._attr_unique_id = f"{coordinator.gios.station_id}-{description.key}"
self._attrs: dict[str, Any] = {
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_STATION: self.coordinator.gios.station_name,
}
self.entity_description = description

View File

@ -10,7 +10,6 @@ from googlemaps.distance_matrix import distance_matrix
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_API_KEY,
CONF_MODE,
CONF_NAME,
@ -104,6 +103,8 @@ async def async_setup_entry(
class GoogleTravelTimeSensor(SensorEntity):
"""Representation of a Google travel time sensor."""
_attr_attribution = ATTRIBUTION
def __init__(self, config_entry, name, api_key, origin, destination, client):
"""Initialize the sensor."""
self._name = name
@ -178,7 +179,6 @@ class GoogleTravelTimeSensor(SensorEntity):
res["distance"] = _data["distance"]["text"]
res["origin"] = self._resolved_origin
res["destination"] = self._resolved_destination
res[ATTR_ATTRIBUTION] = ATTRIBUTION
return res
@property

View File

@ -16,7 +16,7 @@ from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
)
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME, CONF_OFFSET, STATE_UNKNOWN
from homeassistant.const import CONF_NAME, CONF_OFFSET, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -648,6 +648,11 @@ class GTFSDepartureSensor(SensorEntity):
# Assign attributes, icon and name
self.update_attributes()
if self._agency:
self._attr_attribution = self._agency.agency_name
else:
self._attr_attribution = None
if self._route:
self._icon = ICONS.get(self._route.route_type, ICON)
else:
@ -702,11 +707,6 @@ class GTFSDepartureSensor(SensorEntity):
elif ATTR_INFO in self._attributes:
del self._attributes[ATTR_INFO]
if self._agency:
self._attributes[ATTR_ATTRIBUTION] = self._agency.agency_name
elif ATTR_ATTRIBUTION in self._attributes:
del self._attributes[ATTR_ATTRIBUTION]
# Add extra metadata
key = "agency_id"
if self._agency and key not in self._attributes: