From 3c6f0d11a64c2c82bab03a35cc4ecc75c8550774 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Mon, 19 Jul 2021 12:02:09 -0400 Subject: [PATCH] Use entity class attributes for Citybikes (#53167) * Use entity class attributes for citybikes * tweak --- homeassistant/components/citybikes/sensor.py | 50 +++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/citybikes/sensor.py b/homeassistant/components/citybikes/sensor.py index bc323a51151..7d54d259051 100644 --- a/homeassistant/components/citybikes/sensor.py +++ b/homeassistant/components/citybikes/sensor.py @@ -265,50 +265,32 @@ class CityBikesNetwork: class CityBikesStation(SensorEntity): """CityBikes API Sensor.""" + _attr_unit_of_measurement = "bikes" + _attr_icon = "mdi:bike" + def __init__(self, network, station_id, entity_id): """Initialize the sensor.""" self._network = network self._station_id = station_id - self._station_data = {} self.entity_id = entity_id - @property - def state(self): - """Return the state of the sensor.""" - return self._station_data.get(ATTR_FREE_BIKES) - - @property - def name(self): - """Return the name of the sensor.""" - return self._station_data.get(ATTR_NAME) - async def async_update(self): """Update station state.""" for station in self._network.stations: if station[ATTR_ID] == self._station_id: - self._station_data = station + station_data = station break - - @property - def extra_state_attributes(self): - """Return the state attributes.""" - if self._station_data: - return { + self._attr_name = station_data.get(ATTR_NAME) + self._attr_state = station_data.get(ATTR_FREE_BIKES) + self._attr_extra_state_attributes = ( + { ATTR_ATTRIBUTION: CITYBIKES_ATTRIBUTION, - ATTR_UID: self._station_data.get(ATTR_EXTRA, {}).get(ATTR_UID), - ATTR_LATITUDE: self._station_data[ATTR_LATITUDE], - ATTR_LONGITUDE: self._station_data[ATTR_LONGITUDE], - ATTR_EMPTY_SLOTS: self._station_data[ATTR_EMPTY_SLOTS], - ATTR_TIMESTAMP: self._station_data[ATTR_TIMESTAMP], + ATTR_UID: station_data.get(ATTR_EXTRA, {}).get(ATTR_UID), + ATTR_LATITUDE: station_data[ATTR_LATITUDE], + ATTR_LONGITUDE: station_data[ATTR_LONGITUDE], + ATTR_EMPTY_SLOTS: station_data[ATTR_EMPTY_SLOTS], + ATTR_TIMESTAMP: station_data[ATTR_TIMESTAMP], } - return {ATTR_ATTRIBUTION: CITYBIKES_ATTRIBUTION} - - @property - def unit_of_measurement(self): - """Return the unit of measurement.""" - return "bikes" - - @property - def icon(self): - """Return the icon.""" - return "mdi:bike" + if station_data + else {ATTR_ATTRIBUTION: CITYBIKES_ATTRIBUTION} + )