Repository navigation
Expand file tree
/
Copy pathsensor.py
More file actions
151 lines (141 loc) · 5.3 KB
/
Copy pathsensor.py
File metadata and controls
151 lines (141 loc) · 5.3 KB
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
from __future__ import annotations
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
SENSORS: list[SensorEntityDescription] = [
SensorEntityDescription(
key="pv_energy_today",
name="PV Energy Today",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:solar-power",
),
SensorEntityDescription(
key="load_energy_today",
name="Load Energy Today",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:transmission-tower-import",
),
SensorEntityDescription(
key="grid_import_energy_today",
name="Grid Import Energy Today",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:transmission-tower-import",
),
SensorEntityDescription(
key="grid_export_energy_today",
name="Grid Export Energy Today",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:transmission-tower-export",
),
SensorEntityDescription(
key="pv_power",
name="PV Power",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:solar-power",
),
SensorEntityDescription(
key="load_power",
name="Load Power",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:home-lightning-bolt",
),
SensorEntityDescription(
key="grid_import_power",
name="Grid Import Power",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:transmission-tower-import",
),
SensorEntityDescription(
key="grid_export_power",
name="Grid Export Power",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:transmission-tower-export",
),
SensorEntityDescription(
key="battery_power",
name="Battery Power",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:battery-charging-50",
),
SensorEntityDescription(
key="battery_soc",
name="Battery SoC",
device_class=SensorDeviceClass.BATTERY,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:battery-heart-variant",
),
# Retained for entity compatibility with 0.2.5.
SensorEntityDescription(
key="energy_today",
name="Energy Today",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:counter",
),
SensorEntityDescription(
key="last_error",
name="Last Error",
icon="mdi:alert-circle",
entity_registry_enabled_default=True,
),
]
async def async_setup_entry(hass, entry, async_add_entities) -> None:
coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
device_info = DeviceInfo(
identifiers={(DOMAIN, entry.unique_id or entry.entry_id)},
name="Sol-Ark Plant",
manufacturer="Sol-Ark",
model="MySolArk Cloud",
configuration_url="https://www.mysolark.com/",
)
async_add_entities(
SolarkSensorEntity(coordinator, description, device_info, entry)
for description in SENSORS
)
class SolarkSensorEntity(CoordinatorEntity, SensorEntity):
_attr_has_entity_name = True
def __init__(self, coordinator, description, device_info, entry) -> None:
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.entry_id}_{description.key}"
self._attr_name = description.name
self._attr_device_info = device_info
self._attr_icon = description.icon
self._attr_device_class = description.device_class
self._attr_native_unit_of_measurement = description.native_unit_of_measurement
@property
def native_value(self):
key = self.entity_description.key
if key == "last_error":
return (self.coordinator.data or {}).get("last_error")
metrics = (self.coordinator.data or {}).get("metrics", {})
if key == "pv_energy_today":
return metrics.get("energy_today")
return metrics.get(key)