diff --git a/custom_components/nhc2/__init__.py b/custom_components/nhc2/__init__.py index 9e31f45..701c64f 100644 --- a/custom_components/nhc2/__init__.py +++ b/custom_components/nhc2/__init__.py @@ -4,8 +4,7 @@ import homeassistant.helpers.config_validation as cv import voluptuous as vol from homeassistant import config_entries -from homeassistant.const import CONF_HOST, CONF_USERNAME, \ - CONF_PASSWORD, CONF_ADDRESS, CONF_PORT +from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD, CONF_ADDRESS, CONF_PORT from homeassistant.const import EVENT_HOMEASSISTANT_STOP from .config_flow import Nhc2FlowHandler # noqa pylint_disable=unused-import @@ -64,7 +63,8 @@ async def async_setup(hass, config): "switch", "light", "fan", - "cover" + "cover", + "sensor" ) async def async_setup_entry(hass, entry): diff --git a/custom_components/nhc2/climate.py b/custom_components/nhc2/climate.py index 384b5f1..0226765 100644 --- a/custom_components/nhc2/climate.py +++ b/custom_components/nhc2/climate.py @@ -123,7 +123,7 @@ def state(self): @property def current_temperature(self): """Return the current temperature.""" - return self._current_temperature + return self._nhc2thermostat.current_temperature @property def target_temperature(self): diff --git a/custom_components/nhc2/const.py b/custom_components/nhc2/const.py index 54b011e..41bed2e 100644 --- a/custom_components/nhc2/const.py +++ b/custom_components/nhc2/const.py @@ -9,6 +9,7 @@ COVER = 'Cover' FAN = 'Fan' CLIMATE = 'Thermostat' +ENERGY = 'CentralMeter' CONF_SWITCHES_AS_LIGHTS = 'switches_as_lights' DEFAULT_PORT = 8883 KEY_MANUAL = 'MANUAL_IP_HOST' diff --git a/custom_components/nhc2/sensor.py b/custom_components/nhc2/sensor.py new file mode 100644 index 0000000..e79aa05 --- /dev/null +++ b/custom_components/nhc2/sensor.py @@ -0,0 +1,98 @@ +"""Support for NHC2 lights.""" +import logging + +from homeassistant.components.sensor import PLATFORM_SCHEMA, STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT, STATE_CLASS_TOTAL_INCREASING, SensorEntity +from homeassistant.const import ENERGY_KILO_WATT_HOUR, POWER_WATT, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER +from homeassistant.core import CALLBACK_TYPE, callback + +from nhc2_coco import CoCo +from nhc2_coco.coco_energy import CoCoEnergyMeter +from nhc2_coco.coco_device_class import CoCoDeviceClass + +from .const import DOMAIN, ENERGY, KEY_GATEWAY, BRAND +from .helpers import nhc2_entity_processor + +KEY_GATEWAY = KEY_GATEWAY +KEY_ENTITY = 'nhc2_energymeters' + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Load NHC2 energy meters based on a config entry.""" + hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = [] + gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id] + _LOGGER.debug('Platform is starting') + gateway.get_devices(CoCoDeviceClass.ENERGYMETERS, + nhc2_entity_processor(hass, + config_entry, + async_add_entities, + KEY_ENTITY, + lambda x: NHC2HassEnergyMeter(x)) + ) + +class NHC2HassEnergyMeter(SensorEntity): + """Representation of an NHC2 Energy Meter.""" + + def __init__(self, nhc2energymeter: CoCoEnergyMeter, optimistic=True): + """Initialize an energy meter.""" + self._nhc2energymeter = nhc2energymeter + self._state = self._nhc2energymeter._state + nhc2energymeter.on_change = self._on_change + + @property + def native_unit_of_measurement(self): + """Return the unit the value is expressed in.""" + return POWER_WATT + + @property + def device_class(self): + """Return the device class the sensor belongs to.""" + return DEVICE_CLASS_POWER + + @property + def state_class(self): + """Return the state class the sensor belongs to.""" + return STATE_CLASS_MEASUREMENT + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + def _on_change(self): + self._state = self._nhc2energymeter._state + self.schedule_update_ha_state() + + @property + def unique_id(self): + """Return the energy meters UUID.""" + return self._nhc2energymeter.uuid + + @property + def uuid(self): + """Return the energy meters UUID.""" + return self._nhc2energymeter.uuid + + @property + def should_poll(self): + """Return false, since the energy meters will push state.""" + return False + + @property + def name(self): + """Return the energy meters name.""" + return self._nhc2energymeter.name + + @property + def device_info(self): + """Return the device info.""" + return { + 'identifiers': { + (DOMAIN, self.unique_id) + }, + 'name': self.name, + 'manufacturer': BRAND, + 'model': ENERGY, + 'via_hub': (DOMAIN, self._nhc2energymeter.profile_creation_id), + }