-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
144 lines (119 loc) · 4.07 KB
/
sensor.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# sensor.py
from datetime import timedelta
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import POWER_WATT, ENERGY_WATT_HOUR
from . import DOMAIN
SCAN_INTERVAL = timedelta(minutes=10)
async def async_setup_entry(hass, config_entry, async_add_entities):
voltalis = hass.data[DOMAIN][config_entry.entry_id]
unique_id = config_entry.entry_id # ou un autre identifiant unique pertinent
power_sensors = [VoltalisPowerSensor(voltalis, device) for device in voltalis.get_modulators()]
async_add_entities(power_sensors)
async_add_entities([
VoltalisImmediateConsumptionSensor(voltalis, unique_id),
VoltalisTotalConsumptionSensor(voltalis, unique_id)
])
class VoltalisPowerSensor(SensorEntity):
def __init__(self, voltalis, device):
self._voltalis = voltalis
self._device_id = device["csLinkId"]
self._name = device["name"]
self._state = None
@property
def unique_id(self):
return f"voltalis_{self._device_id}_power"
@property
def name(self):
return f"Voltalis {self._name}[{self._device_id}] power"
@property
def unit_of_measurement(self):
return POWER_WATT
@property
def state(self):
return self._state
async def async_update(self):
data = await self._voltalis.fetch_immediate_consumption_in_kw()
self._state = data["immediateConsumptionInkWByAppliance"].get(self._device_id, {}).get("consumption", 0) * 1000
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._device_id)},
"name": self._name,
"manufacturer": "Voltalis",
}
class VoltalisImmediateConsumptionSensor(SensorEntity):
def __init__(self, voltalis, unique_id):
self._voltalis = voltalis
self._state = None
self._name = "Voltalis Immediate Consumption"
self._unique_id = unique_id
@property
def unique_id(self):
return f"voltalis_immediate_consumption_{self._unique_id}"
@property
def name(self):
return self._name
@property
def icon(self):
return "mdi:home-lightning-bolt-outline"
@property
def unit_of_measurement(self):
return POWER_WATT
@property
def device_class(self):
return "power"
@property
def state_class(self):
return "total_increasing"
@property
def state(self):
return self._state
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._unique_id)},
"name": self._name,
"manufacturer": "Voltalis",
}
async def async_update(self):
data = await self._voltalis.fetch_immediate_consumption_in_kw()
self._state = data['immediateConsumptionInkW']['consumption'] * 1000
class VoltalisTotalConsumptionSensor(SensorEntity):
def __init__(self, voltalis, unique_id):
self._voltalis = voltalis
self._state = None
self._name = "Voltalis Consumption"
self._unique_id = unique_id
@property
def unique_id(self):
return f"voltalis_total_consumption_{self._unique_id}"
@property
def name(self):
return self._name
@property
def icon(self):
return "mdi:home-lightning-bolt-outline"
@property
def unit_of_measurement(self):
return ENERGY_WATT_HOUR
@property
def device_class(self):
return "energy"
@property
def state_class(self):
return "total_increasing"
@property
def state(self):
return self._state
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._unique_id)},
"name": self._name,
"manufacturer": "Voltalis",
}
async def async_update(self):
data = await self._voltalis.fetch_immediate_consumption_in_kw()
immediate_consumption = data['immediateConsumptionInkW']['consumption'] * 1000
consumption_duration = data['immediateConsumptionInkW']['duration'] / 3600
self._state = immediate_consumption * consumption_duration