Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit cfb541f

Browse files
authored
Home Energy Management integration (#33)
* Migrate sensors to extend SensorEntity. Temp, humidity, power and energy sensors are now marked as measured. This is to support long-term statistics. The energy sensors are now marked as an energy class instead of power. The 'total today energy' has a new property; last_reset. This is used by the build-in Energy Management to keep track of the energy consumption. The 'total today energy' will not pass a 'period' to the Ngenic API, doing so lead to missing meterings during the day. * Add hacs.json to limit the installation of v2.0.0 From v2.0.0 the minimum version of home assistant is 2021.6.0.
1 parent 1a2454f commit cfb541f

4 files changed

Lines changed: 27 additions & 10 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ Install HACS and add the repository to the Custom repositories under HACS Settin
2222

2323
* https://hacs.xyz/docs/installation/manual
2424
* https://hacs.xyz/docs/basic/getting_started
25-
2625
## Prerequisite
2726
### Obtain an API token
2827
An API token may be obtained from Ngenic here: https://developer.ngenic.se/
2928

3029
## Configuration
3130
Configure via UI: Configuration > Integrations
31+
32+
### Home Energy Management
33+
If you have an [Ngenic Track](https://ngenic.se/track/) you may track your energy consumption in Home Assistant's _Energy Management_.
34+
35+
There's one thing to consider: if your Track is placed on the energy meter you should add the _Ngenic energy sensor_ as a _Grid consumption_. If your Track is placed on something else (such as district heating meter), you should add the _Ngenic energy sensor_ as an _Individual device_.

custom_components/ngenic/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "ngenic",
33
"name": "Ngenic Tune",
4-
"version": "1.0.0",
4+
"version": "2.0.0",
55
"config_flow": true,
66
"documentation": "https://github.com/sfalkman/ngenic-hass-platform",
77
"issue_tracker": "https://github.com/sfalkman/ngenic-hass-platform/issues",

custom_components/ngenic/sensor.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
DEVICE_CLASS_TEMPERATURE,
1111
DEVICE_CLASS_HUMIDITY,
1212
DEVICE_CLASS_POWER,
13+
DEVICE_CLASS_ENERGY,
1314
ENERGY_KILO_WATT_HOUR,
1415
POWER_WATT
1516
)
16-
from homeassistant.helpers.entity import Entity
17+
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
1718
from homeassistant.helpers.event import async_track_time_interval
1819
import homeassistant.util.dt as dt_util
1920

@@ -221,7 +222,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
221222
# Add entities to hass (and trigger a state update)
222223
async_add_entities(devices, update_before_add=True)
223224

224-
class NgenicSensor(Entity):
225+
class NgenicSensor(SensorEntity):
225226
"""Representation of an Ngenic Sensor"""
226227

227228
def __init__(self, hass, ngenic, node, name, update_interval, measurement_type):
@@ -308,22 +309,23 @@ async def _async_update(self, event_time=None):
308309

309310
class NgenicTempSensor(NgenicSensor):
310311
device_class = DEVICE_CLASS_TEMPERATURE
312+
state_class = STATE_CLASS_MEASUREMENT
311313

312314
@property
313315
def unit_of_measurement(self):
314316
"""Return the unit of measurement."""
315317
return TEMP_CELSIUS
316-
317318
class NgenicHumiditySensor(NgenicSensor):
318319
device_class = DEVICE_CLASS_HUMIDITY
320+
state_class = STATE_CLASS_MEASUREMENT
319321

320322
@property
321323
def unit_of_measurement(self):
322324
"""Return the unit of measurement."""
323325
return "%"
324-
325326
class NgenicPowerSensor(NgenicSensor):
326327
device_class = DEVICE_CLASS_POWER
328+
state_class = STATE_CLASS_MEASUREMENT
327329

328330
@property
329331
def unit_of_measurement(self):
@@ -338,21 +340,27 @@ async def _async_fetch_measurement(self):
338340
return round(current*1000.0, 1)
339341

340342
class NgenicEnergySensor(NgenicSensor):
341-
device_class = DEVICE_CLASS_POWER
343+
device_class = DEVICE_CLASS_ENERGY
344+
state_class = STATE_CLASS_MEASUREMENT
342345

343346
@property
344347
def unit_of_measurement(self):
345348
"""Return the unit of measurement."""
346349
return ENERGY_KILO_WATT_HOUR
347350

351+
@property
352+
def last_reset(self):
353+
"""Return the time when the sensor value was last reset."""
354+
return dt_util.start_of_local_day()
355+
348356
async def _async_fetch_measurement(self):
349357
"""Ask for measurements for a duration.
350358
This requires some further inputs, so we'll override the _async_fetch_measurement method.
351359
"""
352360
from_dt, to_dt = get_from_to_datetime()
353361
# using datetime will return a list of measurements
354362
# we'll use the last item in that list
355-
current = await get_measurement_value(self._node, measurement_type=self._measurement_type, from_dt=from_dt, to_dt=to_dt, period="P1D")
363+
current = await get_measurement_value(self._node, measurement_type=self._measurement_type, from_dt=from_dt, to_dt=to_dt)
356364
return round(current, 1)
357365

358366
@property
@@ -361,7 +369,7 @@ def name(self):
361369
return "%s %s" % (self._name, "energy")
362370

363371
class NgenicEnergySensorMonth(NgenicSensor):
364-
device_class = DEVICE_CLASS_POWER
372+
device_class = DEVICE_CLASS_ENERGY
365373

366374
@property
367375
def unit_of_measurement(self):
@@ -389,7 +397,7 @@ def unique_id(self):
389397
return "%s-%s-%s-month" % (self._node.uuid(), self._measurement_type.name, "sensor")
390398

391399
class NgenicEnergySensorLastMonth(NgenicSensor):
392-
device_class = DEVICE_CLASS_POWER
400+
device_class = DEVICE_CLASS_ENERGY
393401

394402
@property
395403
def unit_of_measurement(self):

hacs.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Ngenic Tune",
3+
"iot_class": "Cloud Polling",
4+
"homeassistant": "2021.6.0"
5+
}

0 commit comments

Comments
 (0)