From 8e6dd6285341d288298c0301d919b24630d2a193 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Mon, 8 Aug 2016 17:54:59 -0700 Subject: [PATCH] Add an OhmConnect sensor (#2758) * Add an OhmConnect sensor * use .get --- .coveragerc | 1 + homeassistant/components/sensor/ohmconnect.py | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 homeassistant/components/sensor/ohmconnect.py diff --git a/.coveragerc b/.coveragerc index 388698f26f3..cd338ddccf0 100644 --- a/.coveragerc +++ b/.coveragerc @@ -205,6 +205,7 @@ omit = homeassistant/components/sensor/loopenergy.py homeassistant/components/sensor/neurio_energy.py homeassistant/components/sensor/nzbget.py + homeassistant/components/sensor/ohmconnect.py homeassistant/components/sensor/onewire.py homeassistant/components/sensor/openweathermap.py homeassistant/components/sensor/openexchangerates.py diff --git a/homeassistant/components/sensor/ohmconnect.py b/homeassistant/components/sensor/ohmconnect.py new file mode 100644 index 00000000000..fcd50d8edc5 --- /dev/null +++ b/homeassistant/components/sensor/ohmconnect.py @@ -0,0 +1,74 @@ +""" +Support for OhmConnect. + +For more details about this platform, please refer to the documentation +https://home-assistant.io/components/sensor.ohmconnect/ +""" +import logging +from datetime import timedelta +import xml.etree.ElementTree as ET +import requests + +from homeassistant.util import Throttle +from homeassistant.helpers.entity import Entity + +_LOGGER = logging.getLogger(__name__) + +# Return cached results if last scan was less then this time ago. +MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1) + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the OhmConnect sensors.""" + ohmid = config.get("id") + if ohmid is None: + _LOGGER.error("You must provide your OhmConnect ID!") + return False + + add_devices([OhmconnectSensor(config.get("name", "OhmConnect Status"), + ohmid)]) + + +class OhmconnectSensor(Entity): + """Representation of a OhmConnect sensor.""" + + def __init__(self, name, ohmid): + """Initialize the sensor.""" + self._name = name + self._ohmid = ohmid + self._data = {} + self.update() + + @property + def name(self): + """The name of the sensor.""" + return self._name + + @property + def state(self): + """Return the state of the sensor.""" + if self._data.get("active") == "True": + return "Active" + else: + return "Inactive" + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return {"Address": self._data.get("address"), "ID": self._ohmid} + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update(self): + """Get the latest data from OhmConnect.""" + try: + url = ("https://login.ohmconnect.com" + "/verify-ohm-hour/{}").format(self._ohmid) + response = requests.get(url, timeout=10) + root = ET.fromstring(response.text) + + for child in root: + self._data[child.tag] = child.text + except requests.exceptions.ConnectionError: + _LOGGER.error("No route to host/endpoint: %s", url) + self.data = {}