Use entity class attributes for Co2signal (#53032)

This commit is contained in:
Daniel Hjelseth Høyer 2021-07-15 06:47:24 +02:00 committed by GitHub
parent fbad453c89
commit f152369944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,10 +47,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
_LOGGER.debug("Setting up the sensor using the %s", country_code) _LOGGER.debug("Setting up the sensor using the %s", country_code)
devs = [] add_entities([CO2Sensor(token, country_code, lat, lon)], True)
devs.append(CO2Sensor(token, country_code, lat, lon))
add_entities(devs, True)
class CO2Sensor(SensorEntity): class CO2Sensor(SensorEntity):
@ -65,42 +62,27 @@ class CO2Sensor(SensorEntity):
self._country_code = country_code self._country_code = country_code
self._latitude = lat self._latitude = lat
self._longitude = lon self._longitude = lon
self._data = None
if country_code is not None: if country_code is not None:
device_name = country_code device_name = country_code
else: else:
device_name = f"{round(self._latitude, 2)}/{round(self._longitude, 2)}" device_name = f"{round(self._latitude, 2)}/{round(self._longitude, 2)}"
self._friendly_name = f"CO2 intensity - {device_name}" self._attr_name = f"CO2 intensity - {device_name}"
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
@property
def name(self):
"""Return the name of the sensor."""
return self._friendly_name
@property
def state(self):
"""Return the state of the device."""
return self._data
@property
def extra_state_attributes(self):
"""Return the state attributes of the last update."""
return {ATTR_ATTRIBUTION: ATTRIBUTION}
def update(self): def update(self):
"""Get the latest data and updates the states.""" """Get the latest data and updates the states."""
_LOGGER.debug("Update data for %s", self._friendly_name) _LOGGER.debug("Update data for %s", self.name)
if self._country_code is not None: if self._country_code is not None:
self._data = CO2Signal.get_latest_carbon_intensity( data = CO2Signal.get_latest_carbon_intensity(
self._token, country_code=self._country_code self._token, country_code=self._country_code
) )
else: else:
self._data = CO2Signal.get_latest_carbon_intensity( data = CO2Signal.get_latest_carbon_intensity(
self._token, latitude=self._latitude, longitude=self._longitude self._token, latitude=self._latitude, longitude=self._longitude
) )
self._data = round(self._data, 2) self._attr_state = round(data, 2)