-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsensor.py
164 lines (140 loc) · 5.01 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Support for Enphase Envoy solar energy monitor."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import COORDINATOR, DOMAIN, NAME, SENSORS
ICON = "mdi:flash"
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up envoy sensor platform."""
data = hass.data[DOMAIN][config_entry.entry_id]
coordinator = data[COORDINATOR]
name = data[NAME]
entities = []
for sensor_description in SENSORS:
if (
sensor_description.key == "inverters"
and coordinator.data.get("inverters_production") is not None
):
for inverter in coordinator.data["inverters_production"]:
entity_name = f"{name} {sensor_description.name} {inverter}"
split_name = entity_name.split(" ")
serial_number = split_name[-1]
entities.append(
Envoy(
sensor_description,
entity_name,
name,
config_entry.unique_id,
serial_number,
coordinator,
)
)
elif (
sensor_description.key == "batteryPercentage"
and coordinator.data.get("batteryPercentage") is not None
):
entities.append(
Envoy(
sensor_description,
"Envoy Attached Battery Percentage",
"batteryPercentage",
config_entry.unique_id,
None,
coordinator
)
)
elif sensor_description.key not in ("inverters", "batteryPercentage") :
data = coordinator.data.get(sensor_description.key)
if isinstance(data, str) and "not available" in data:
continue
entity_name = f"{name} {sensor_description.name}"
entities.append(
Envoy(
sensor_description,
entity_name,
name,
config_entry.unique_id,
None,
coordinator,
)
)
async_add_entities(entities)
class Envoy(CoordinatorEntity, SensorEntity):
"""Envoy entity."""
def __init__(
self,
description,
name,
device_name,
device_serial_number,
serial_number,
coordinator,
):
"""Initialize Envoy entity."""
self.entity_description = description
self._name = name
self._serial_number = serial_number
self._device_name = device_name
self._device_serial_number = device_serial_number
super().__init__(coordinator)
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unique_id(self):
"""Return the unique id of the sensor."""
if self._serial_number:
return self._serial_number
if self._device_serial_number:
return f"{self._device_serial_number}_{self.entity_description.key}"
@property
def native_value(self):
"""Return the state of the sensor."""
if self.entity_description.key != "inverters":
value = self.coordinator.data.get(self.entity_description.key)
elif (
self.entity_description.key == "inverters"
and self.coordinator.data.get("inverters_production") is not None
):
value = self.coordinator.data.get("inverters_production").get(
self._serial_number
)[0]
else:
return None
return value
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
@property
def extra_state_attributes(self):
"""Return the state attributes."""
if (
self.entity_description.key == "inverters"
and self.coordinator.data.get("inverters_production") is not None
):
value = self.coordinator.data.get("inverters_production").get(
self._serial_number
)[1]
return {"last_reported": value}
return None
@property
def device_info(self) -> DeviceInfo | None:
"""Return the device_info of the device."""
if not self._device_serial_number:
return None
return DeviceInfo(
identifiers={(DOMAIN, str(self._device_serial_number))},
manufacturer="Enphase",
model="Envoy",
name=self._device_name,
)