From c8f64840957a72aa826fdae6616d631736eea4c3 Mon Sep 17 00:00:00 2001 From: Bogdan Vlaicu Date: Wed, 16 Oct 2019 03:52:30 -0400 Subject: [PATCH] New sensor platform integration for Orange and Rockland Utility smart energy meter (#27571) * New sensor platform integration for Orange and Rockland Utility smart energy meter * New sensor platform integration for Orange and Rockland Utility smart energy meter * bumped the oru py version to 0.1.9 * Added PLATFORM_SCHEMA Adde unique_id property Changed logger level from info to debug when printing the updated sensor value Set the SCAN_INTERVAL to 15 mins Added exception handling durin init when creating the oru meter instance * Various fixes base on the PR review + Added SCAN_INTERVAL for 15 mins * fixed path to documentation --- .coveragerc | 1 + CODEOWNERS | 1 + homeassistant/components/oru/__init__.py | 1 + homeassistant/components/oru/manifest.json | 8 ++ homeassistant/components/oru/sensor.py | 92 ++++++++++++++++++++++ requirements_all.txt | 3 + 6 files changed, 106 insertions(+) create mode 100644 homeassistant/components/oru/__init__.py create mode 100644 homeassistant/components/oru/manifest.json create mode 100644 homeassistant/components/oru/sensor.py diff --git a/.coveragerc b/.coveragerc index 69ce7f8322c..52cf74f384a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -485,6 +485,7 @@ omit = homeassistant/components/openweathermap/weather.py homeassistant/components/opple/light.py homeassistant/components/orangepi_gpio/* + homeassistant/components/oru/* homeassistant/components/orvibo/switch.py homeassistant/components/osramlightify/light.py homeassistant/components/otp/sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index 8e52210cec7..547fe504892 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -215,6 +215,7 @@ homeassistant/components/opentherm_gw/* @mvn23 homeassistant/components/openuv/* @bachya homeassistant/components/openweathermap/* @fabaff homeassistant/components/orangepi_gpio/* @pascallj +homeassistant/components/oru/* @bvlaicu homeassistant/components/owlet/* @oblogic7 homeassistant/components/panel_custom/* @home-assistant/frontend homeassistant/components/panel_iframe/* @home-assistant/frontend diff --git a/homeassistant/components/oru/__init__.py b/homeassistant/components/oru/__init__.py new file mode 100644 index 00000000000..d1517ab0bf1 --- /dev/null +++ b/homeassistant/components/oru/__init__.py @@ -0,0 +1 @@ +"""The Orange and Rockland Utility smart energy meter integration.""" diff --git a/homeassistant/components/oru/manifest.json b/homeassistant/components/oru/manifest.json new file mode 100644 index 00000000000..ff5e74fd260 --- /dev/null +++ b/homeassistant/components/oru/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "oru", + "name": "Orange and Rockland Utility Smart Energy Meter Sensor", + "documentation": "https://www.home-assistant.io/integrations/oru", + "dependencies": [], + "codeowners": ["@bvlaicu"], + "requirements": ["oru==0.1.9"] +} \ No newline at end of file diff --git a/homeassistant/components/oru/sensor.py b/homeassistant/components/oru/sensor.py new file mode 100644 index 00000000000..e68d8e1c45a --- /dev/null +++ b/homeassistant/components/oru/sensor.py @@ -0,0 +1,92 @@ +"""Platform for sensor integration.""" +from datetime import timedelta +import logging + +import voluptuous as vol + +from oru import Meter +from oru import MeterError + +from homeassistant.components.sensor import PLATFORM_SCHEMA +import homeassistant.helpers.config_validation as cv +from homeassistant.const import ENERGY_KILO_WATT_HOUR +from homeassistant.helpers.entity import Entity + +_LOGGER = logging.getLogger(__name__) + +CONF_METER_NUMBER = "meter_number" + +SCAN_INTERVAL = timedelta(minutes=15) + +SENSOR_NAME = "ORU Current Energy Usage" +SENSOR_ICON = "mdi:counter" + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_METER_NUMBER): cv.string}) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the sensor platform.""" + + meter_number = config[CONF_METER_NUMBER] + + try: + meter = Meter(meter_number) + + except MeterError: + _LOGGER.error("Unable to create Oru meter") + return + + add_entities([CurrentEnergyUsageSensor(meter)], True) + + _LOGGER.debug("Oru meter_number = %s", meter_number) + + +class CurrentEnergyUsageSensor(Entity): + """Representation of the sensor.""" + + def __init__(self, meter): + """Initialize the sensor.""" + self._state = None + self._available = None + self.meter = meter + + @property + def unique_id(self): + """Return a unique, HASS-friendly identifier for this entity.""" + return self.meter.meter_id + + @property + def name(self): + """Return the name of the sensor.""" + return SENSOR_NAME + + @property + def icon(self): + """Return the icon of the sensor.""" + return SENSOR_ICON + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def unit_of_measurement(self): + """Return the unit of measurement.""" + return ENERGY_KILO_WATT_HOUR + + def update(self): + """Fetch new state data for the sensor.""" + try: + last_read = self.meter.last_read() + + self._state = last_read + self._available = True + + _LOGGER.debug( + "%s = %s %s", self.name, self._state, self.unit_of_measurement + ) + except MeterError as err: + self._available = False + + _LOGGER.error("Unexpected oru meter error: %s", err) diff --git a/requirements_all.txt b/requirements_all.txt index 567acd712a1..51b28bdf352 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -916,6 +916,9 @@ openwebifpy==3.1.1 # homeassistant.components.luci openwrt-luci-rpc==1.1.1 +# homeassistant.components.oru +oru==0.1.9 + # homeassistant.components.orvibo orvibo==1.1.1