From f3285f96bb910b93bd2a4e14c94c9701f8494d59 Mon Sep 17 00:00:00 2001
From: Jef D <danielsjf@users.noreply.github.com>
Date: Sat, 26 Jan 2019 19:02:46 +0100
Subject: [PATCH] Add Co2signal sensor (#19204)

* Initial commit for the co2signal sensor

* Clean code

* Run script gen_requirements_all.py

* remove unintended character

* Remove redundancy

* Remove unused imports

* Code style

* Code style fixes

* Code style

* Fix comments PR

Comments by @fabaff
* Remove redundant comments and variables
* Follow the latest home-assistant guidelines

* Bump CO2Signal version

* Round API result

* Improve default latitude/longitude handling

* Improve friendly name

* Improve config handling

* Make lines shorter

* Style

* Convert default to variable

None does not pass cv.string

* Message if not inclusive

* Shorten line

* Update requirements

* Update co2signal.py

Group imports;  remove empty lines; refactor use of location_type; remove logging messages; remove unused functions; add global variables

* Update co2signal.py

Import platform schema from sensor

* Small fix

* Update co2signal.py

Remove last mentions of location_type

* Review changes

Add attribution
Formatting

* Missing whitespace

* Update co2signal.py

Fix pylint

* Update co2signal.py

Change blank lines

* Update co2signal.py

Initialise _data
---
 homeassistant/components/sensor/co2signal.py | 113 +++++++++++++++++++
 requirements_all.txt                         |   3 +
 2 files changed, 116 insertions(+)
 create mode 100644 homeassistant/components/sensor/co2signal.py

diff --git a/homeassistant/components/sensor/co2signal.py b/homeassistant/components/sensor/co2signal.py
new file mode 100644
index 00000000000..ad46f3b494f
--- /dev/null
+++ b/homeassistant/components/sensor/co2signal.py
@@ -0,0 +1,113 @@
+"""
+Support for the CO2signal platform.
+
+For more details about this platform, please refer to the documentation
+"""
+import logging
+
+import voluptuous as vol
+
+import homeassistant.helpers.config_validation as cv
+from homeassistant.const import (
+    ATTR_ATTRIBUTION, CONF_TOKEN, CONF_LATITUDE, CONF_LONGITUDE)
+from homeassistant.components.sensor import PLATFORM_SCHEMA
+from homeassistant.helpers.entity import Entity
+
+CONF_COUNTRY_CODE = "country_code"
+
+REQUIREMENTS = ['co2signal==0.4.1']
+
+_LOGGER = logging.getLogger(__name__)
+
+ATTRIBUTION = 'Data provided by CO2signal'
+
+MSG_LOCATION = "Please use either coordinates or the country code. " \
+               "For the coordinates, " \
+               "you need to use both latitude and longitude."
+CO2_INTENSITY_UNIT = "CO2eq/kWh"
+PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
+    vol.Required(CONF_TOKEN): cv.string,
+    vol.Inclusive(CONF_LATITUDE, 'coords', msg=MSG_LOCATION): cv.latitude,
+    vol.Inclusive(CONF_LONGITUDE, 'coords', msg=MSG_LOCATION): cv.longitude,
+    vol.Optional(CONF_COUNTRY_CODE): cv.string,
+})
+
+
+def setup_platform(hass, config, add_entities, discovery_info=None):
+    """Set up the CO2signal sensor."""
+    token = config[CONF_TOKEN]
+    lat = config.get(CONF_LATITUDE, hass.config.latitude)
+    lon = config.get(CONF_LONGITUDE, hass.config.longitude)
+    country_code = config.get(CONF_COUNTRY_CODE)
+
+    _LOGGER.debug("Setting up the sensor using the %s", country_code)
+
+    devs = []
+
+    devs.append(CO2Sensor(token,
+                          country_code,
+                          lat,
+                          lon))
+    add_entities(devs, True)
+
+
+class CO2Sensor(Entity):
+    """Implementation of the CO2Signal sensor."""
+
+    def __init__(self, token, country_code, lat, lon):
+        """Initialize the sensor."""
+        self._token = token
+        self._country_code = country_code
+        self._latitude = lat
+        self._longitude = lon
+        self._data = None
+
+        if country_code is not None:
+            device_name = country_code
+        else:
+            device_name = '{lat}/{lon}'\
+                .format(lat=round(self._latitude, 2),
+                        lon=round(self._longitude, 2))
+
+        self._friendly_name = 'CO2 intensity - {}'.format(device_name)
+
+    @property
+    def name(self):
+        """Return the name of the sensor."""
+        return self._friendly_name
+
+    @property
+    def icon(self):
+        """Icon to use in the frontend, if any."""
+        return 'mdi:periodic-table-co2'
+
+    @property
+    def state(self):
+        """Return the state of the device."""
+        return self._data
+
+    @property
+    def unit_of_measurement(self):
+        """Return the unit of measurement of this entity, if any."""
+        return CO2_INTENSITY_UNIT
+
+    @property
+    def device_state_attributes(self):
+        """Return the state attributes of the last update."""
+        return {ATTR_ATTRIBUTION: ATTRIBUTION}
+
+    def update(self):
+        """Get the latest data and updates the states."""
+        import CO2Signal
+
+        _LOGGER.debug("Update data for %s", self._friendly_name)
+
+        if self._country_code is not None:
+            self._data = CO2Signal.get_latest_carbon_intensity(
+                self._token, country_code=self._country_code)
+        else:
+            self._data = CO2Signal.get_latest_carbon_intensity(
+                self._token,
+                latitude=self._latitude, longitude=self._longitude)
+
+        self._data = round(self._data, 2)
diff --git a/requirements_all.txt b/requirements_all.txt
index 83a72aebb33..7db8d380850 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -255,6 +255,9 @@ caldav==0.5.0
 # homeassistant.components.notify.ciscospark
 ciscosparkapi==0.4.2
 
+# homeassistant.components.sensor.co2signal
+co2signal==0.4.1
+
 # homeassistant.components.coinbase
 coinbase==2.1.0
 
-- 
GitLab