|
1 | 1 | """Sensor platform for carbon intensity UK.""" |
| 2 | +import logging |
| 3 | + |
2 | 4 | from custom_components.carbon_intensity_uk.const import ( |
3 | 5 | DEFAULT_NAME, |
4 | 6 | DOMAIN, |
5 | 7 | ICON, |
6 | 8 | HIGH_ICON, |
7 | 9 | LOW_ICON, |
8 | 10 | MODERATE_ICON, |
9 | | - SENSOR, |
10 | 11 | ) |
11 | 12 | from custom_components.carbon_intensity_uk.entity import CarbonIntensityEntity |
12 | 13 |
|
| 14 | +_LOGGER = logging.getLogger(__name__) |
| 15 | + |
| 16 | +SENSOR_TYPES = [ |
| 17 | + { |
| 18 | + "key": "current_period_index", |
| 19 | + "name": f"{DEFAULT_NAME} Current Index", |
| 20 | + "unit": None, |
| 21 | + "icon": ICON, |
| 22 | + "dynamic_icon": True, |
| 23 | + }, |
| 24 | + { |
| 25 | + "key": "current_period_forecast", |
| 26 | + "name": f"{DEFAULT_NAME} Current Intensity", |
| 27 | + "unit": "gCO2/kWh", |
| 28 | + "icon": "mdi:molecule-co2", |
| 29 | + "dynamic_icon": False, |
| 30 | + }, |
| 31 | + { |
| 32 | + "key": "current_period_national_index", |
| 33 | + "name": f"{DEFAULT_NAME} National Index", |
| 34 | + "unit": None, |
| 35 | + "icon": ICON, |
| 36 | + "dynamic_icon": False, |
| 37 | + }, |
| 38 | + { |
| 39 | + "key": "current_period_national_forecast", |
| 40 | + "name": f"{DEFAULT_NAME} National Intensity", |
| 41 | + "unit": "gCO2/kWh", |
| 42 | + "icon": "mdi:molecule-co2", |
| 43 | + "dynamic_icon": False, |
| 44 | + }, |
| 45 | + { |
| 46 | + "key": "current_low_carbon_percentage", |
| 47 | + "name": f"{DEFAULT_NAME} Low Carbon Percentage", |
| 48 | + "unit": "%", |
| 49 | + "icon": "mdi:wind-turbine", |
| 50 | + "dynamic_icon": False, |
| 51 | + }, |
| 52 | + { |
| 53 | + "key": "current_fossil_fuel_percentage", |
| 54 | + "name": f"{DEFAULT_NAME} Fossil Fuel Percentage", |
| 55 | + "unit": "%", |
| 56 | + "icon": "mdi:fire", |
| 57 | + "dynamic_icon": False, |
| 58 | + }, |
| 59 | + { |
| 60 | + "key": "lowest_period_index", |
| 61 | + "name": f"{DEFAULT_NAME} Lowest Period Index", |
| 62 | + "unit": None, |
| 63 | + "icon": "mdi:leaf", |
| 64 | + "dynamic_icon": False, |
| 65 | + }, |
| 66 | + { |
| 67 | + "key": "lowest_period_forecast", |
| 68 | + "name": f"{DEFAULT_NAME} Lowest Period Intensity", |
| 69 | + "unit": "gCO2/kWh", |
| 70 | + "icon": "mdi:molecule-co2", |
| 71 | + "dynamic_icon": False, |
| 72 | + }, |
| 73 | + { |
| 74 | + "key": "optimal_window_index", |
| 75 | + "name": f"{DEFAULT_NAME} Optimal Window Index", |
| 76 | + "unit": None, |
| 77 | + "icon": "mdi:clock-check", |
| 78 | + "dynamic_icon": False, |
| 79 | + }, |
| 80 | + { |
| 81 | + "key": "optimal_window_forecast", |
| 82 | + "name": f"{DEFAULT_NAME} Optimal Window Intensity", |
| 83 | + "unit": "gCO2/kWh", |
| 84 | + "icon": "mdi:molecule-co2", |
| 85 | + "dynamic_icon": False, |
| 86 | + }, |
| 87 | +] |
| 88 | + |
13 | 89 |
|
14 | 90 | async def async_setup_entry(hass, entry, async_add_devices): |
15 | 91 | """Setup sensor platform.""" |
16 | 92 | coordinator = hass.data[DOMAIN][entry.entry_id] |
17 | | - async_add_devices([CarbonIntensitySensor(coordinator, entry)]) |
| 93 | + sensors = [CarbonIntensitySensor(coordinator, entry, sensor) for sensor in SENSOR_TYPES] |
| 94 | + _LOGGER.debug("Registering %d Carbon Intensity UK sensors", len(sensors)) |
| 95 | + async_add_devices(sensors) |
18 | 96 |
|
19 | 97 |
|
20 | 98 | class CarbonIntensitySensor(CarbonIntensityEntity): |
21 | 99 | """Carbon Intensity Sensor class.""" |
22 | 100 |
|
| 101 | + def __init__(self, coordinator, config_entry, sensor_type): |
| 102 | + super().__init__(coordinator, config_entry) |
| 103 | + self._sensor_type = sensor_type |
| 104 | + |
23 | 105 | @property |
24 | 106 | def name(self): |
25 | | - """Return the name of the sensor.""" |
26 | | - return f"{DEFAULT_NAME}" |
| 107 | + return self._sensor_type["name"] |
| 108 | + |
| 109 | + @property |
| 110 | + def unique_id(self): |
| 111 | + return f"{self.config_entry.entry_id}_{self._sensor_type['key']}" |
27 | 112 |
|
28 | 113 | @property |
29 | 114 | def state(self): |
30 | | - """Return the state of the sensor.""" |
31 | | - return self.coordinator.data.get("current_period_index") |
| 115 | + return self.coordinator.data.get(self._sensor_type["key"]) if self.coordinator.data else None |
| 116 | + |
| 117 | + @property |
| 118 | + def unit_of_measurement(self): |
| 119 | + return self._sensor_type.get("unit") |
32 | 120 |
|
33 | 121 | @property |
34 | 122 | def icon(self): |
35 | | - """Return the icon of the sensor.""" |
36 | | - index = self.coordinator.data.get("current_period_index") |
37 | | - if index == "high": |
38 | | - return HIGH_ICON |
39 | | - elif index == "moderate": |
40 | | - return MODERATE_ICON |
41 | | - elif index == "low": |
42 | | - return LOW_ICON |
43 | | - else: |
44 | | - return ICON |
| 123 | + if self._sensor_type["dynamic_icon"]: |
| 124 | + index = self.coordinator.data.get("current_period_index") if self.coordinator.data else None |
| 125 | + if index == "high": |
| 126 | + return HIGH_ICON |
| 127 | + elif index == "moderate": |
| 128 | + return MODERATE_ICON |
| 129 | + elif index == "low": |
| 130 | + return LOW_ICON |
| 131 | + return self._sensor_type["icon"] |
0 commit comments