-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgreat_britain_carbon_intensity.py
64 lines (49 loc) · 1.51 KB
/
great_britain_carbon_intensity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import asyncio
import logging
import requests
import json
from datetime import timedelta
from homeassistant.const import CONF_ELEVATION
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import (async_track_time_interval)
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = []
DOMAIN = 'great_britain_carbon_intensity'
FORECAST = "forecast"
INTERVAL = timedelta(minutes=5)
@asyncio.coroutine
def async_setup(hass, config):
carbon = GreatBritainCarbonIntensity(hass)
return True
class GreatBritainCarbonIntensity(Entity):
entity_id = DOMAIN + ".instance"
def __init__(self, hass):
self.hass = hass
self._state = None
async_track_time_interval(hass, self.timer_update, INTERVAL)
@property
def name(self):
return 'Carbon intensity'
@property
def state(self):
return str(self._state)
@property
def state_attributes(self):
return {
FORECAST: self._state
}
@property
def unit_of_measurement(self):
return "gCO2/kWh"
def update(self):
_LOGGER.warning("Updating")
resp = requests.get("https://api.carbonintensity.org.uk/intensity")
response_json = resp.json()
forecast = int(response_json['data'][0]['intensity']['forecast'])
self._state = forecast
@callback
def timer_update(self, time):
_LOGGER.warning("timer_update")
self.update()
self.async_schedule_update_ha_state()