Skip to content

Commit

Permalink
Home Energy Management integration (#33)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sfalkman authored Aug 7, 2021
1 parent 1a2454f commit cfb541f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ Install HACS and add the repository to the Custom repositories under HACS Settin

* https://hacs.xyz/docs/installation/manual
* https://hacs.xyz/docs/basic/getting_started

## Prerequisite
### Obtain an API token
An API token may be obtained from Ngenic here: https://developer.ngenic.se/

## Configuration
Configure via UI: Configuration > Integrations

### Home Energy Management
If you have an [Ngenic Track](https://ngenic.se/track/) you may track your energy consumption in Home Assistant's _Energy Management_.

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_.
2 changes: 1 addition & 1 deletion custom_components/ngenic/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "ngenic",
"name": "Ngenic Tune",
"version": "1.0.0",
"version": "2.0.0",
"config_flow": true,
"documentation": "https://github.com/sfalkman/ngenic-hass-platform",
"issue_tracker": "https://github.com/sfalkman/ngenic-hass-platform/issues",
Expand Down
24 changes: 16 additions & 8 deletions custom_components/ngenic/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_ENERGY,
ENERGY_KILO_WATT_HOUR,
POWER_WATT
)
from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.helpers.event import async_track_time_interval
import homeassistant.util.dt as dt_util

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

class NgenicSensor(Entity):
class NgenicSensor(SensorEntity):
"""Representation of an Ngenic Sensor"""

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

class NgenicTempSensor(NgenicSensor):
device_class = DEVICE_CLASS_TEMPERATURE
state_class = STATE_CLASS_MEASUREMENT

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS

class NgenicHumiditySensor(NgenicSensor):
device_class = DEVICE_CLASS_HUMIDITY
state_class = STATE_CLASS_MEASUREMENT

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return "%"

class NgenicPowerSensor(NgenicSensor):
device_class = DEVICE_CLASS_POWER
state_class = STATE_CLASS_MEASUREMENT

@property
def unit_of_measurement(self):
Expand All @@ -338,21 +340,27 @@ async def _async_fetch_measurement(self):
return round(current*1000.0, 1)

class NgenicEnergySensor(NgenicSensor):
device_class = DEVICE_CLASS_POWER
device_class = DEVICE_CLASS_ENERGY
state_class = STATE_CLASS_MEASUREMENT

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return ENERGY_KILO_WATT_HOUR

@property
def last_reset(self):
"""Return the time when the sensor value was last reset."""
return dt_util.start_of_local_day()

async def _async_fetch_measurement(self):
"""Ask for measurements for a duration.
This requires some further inputs, so we'll override the _async_fetch_measurement method.
"""
from_dt, to_dt = get_from_to_datetime()
# using datetime will return a list of measurements
# we'll use the last item in that list
current = await get_measurement_value(self._node, measurement_type=self._measurement_type, from_dt=from_dt, to_dt=to_dt, period="P1D")
current = await get_measurement_value(self._node, measurement_type=self._measurement_type, from_dt=from_dt, to_dt=to_dt)
return round(current, 1)

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

class NgenicEnergySensorMonth(NgenicSensor):
device_class = DEVICE_CLASS_POWER
device_class = DEVICE_CLASS_ENERGY

@property
def unit_of_measurement(self):
Expand Down Expand Up @@ -389,7 +397,7 @@ def unique_id(self):
return "%s-%s-%s-month" % (self._node.uuid(), self._measurement_type.name, "sensor")

class NgenicEnergySensorLastMonth(NgenicSensor):
device_class = DEVICE_CLASS_POWER
device_class = DEVICE_CLASS_ENERGY

@property
def unit_of_measurement(self):
Expand Down
5 changes: 5 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Ngenic Tune",
"iot_class": "Cloud Polling",
"homeassistant": "2021.6.0"
}

0 comments on commit cfb541f

Please sign in to comment.