-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensor.py
57 lines (46 loc) · 1.64 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
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity, DataUpdateCoordinator
from datetime import timedelta
from .const import DOMAIN
import logging
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the sensor platform."""
data = hass.data[DOMAIN][config_entry.entry_id]
api = data["api"]
poll_interval = data["poll_interval"]
# Create a coordinator to manage polling
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name="SmartHub Energy",
update_method=api.get_energy_data,
update_interval=timedelta(minutes=poll_interval),
)
# Fetch initial data
await coordinator.async_refresh()
# Create and add the sensor
sensors = [SmartHubEnergySensor(coordinator)]
async_add_entities(sensors)
class SmartHubEnergySensor(CoordinatorEntity, Entity):
"""Representation of the SmartHub Energy sensor."""
def __init__(self, coordinator):
"""Initialize the sensor."""
super().__init__(coordinator)
@property
def name(self):
"""Return the name of the sensor."""
return "SmartHub Energy Sensor"
@property
def state(self):
"""Return the state of the sensor."""
return self.coordinator.data.get("current_energy_usage")
@property
def extra_state_attributes(self):
"""Return additional attributes."""
return {
"state_class": "total",
"unit_of_measurement": "kWh",
"device_class": "energy",
"icon": "mdi:power-plug",
}