From df67a8bdabd28568a572bc7c4161975068fd4a3e Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Tue, 2 Jul 2019 17:57:51 -0700 Subject: [PATCH 01/12] Add support for Rainforest Eagle-200 --- .coveragerc | 1 + CODEOWNERS | 1 + .../components/rainforest_eagle/__init__.py | 1 + .../components/rainforest_eagle/manifest.json | 10 +++ .../components/rainforest_eagle/sensor.py | 87 +++++++++++++++++++ requirements_all.txt | 3 + 6 files changed, 103 insertions(+) create mode 100644 homeassistant/components/rainforest_eagle/__init__.py create mode 100644 homeassistant/components/rainforest_eagle/manifest.json create mode 100644 homeassistant/components/rainforest_eagle/sensor.py diff --git a/.coveragerc b/.coveragerc index 7bc58a9cec13ab..728acce8650a18 100644 --- a/.coveragerc +++ b/.coveragerc @@ -492,6 +492,7 @@ omit = homeassistant/components/rainmachine/binary_sensor.py homeassistant/components/rainmachine/sensor.py homeassistant/components/rainmachine/switch.py + homeassistant/components/rainforest_eagle/sensor.py homeassistant/components/raspihats/* homeassistant/components/raspyrfm/* homeassistant/components/recollect_waste/sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index 0bf06d9945f5f9..dbf69812f1ce79 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -207,6 +207,7 @@ homeassistant/components/qnap/* @colinodell homeassistant/components/quantum_gateway/* @cisasteelersfan homeassistant/components/qwikswitch/* @kellerza homeassistant/components/raincloud/* @vanstinator +homeassistant/components/rainforest_eagle/* @gtdiehl homeassistant/components/rainmachine/* @bachya homeassistant/components/random/* @fabaff homeassistant/components/repetier/* @MTrab diff --git a/homeassistant/components/rainforest_eagle/__init__.py b/homeassistant/components/rainforest_eagle/__init__.py new file mode 100644 index 00000000000000..9de4d85797f19a --- /dev/null +++ b/homeassistant/components/rainforest_eagle/__init__.py @@ -0,0 +1 @@ +"""The rainforest_eagle component.""" diff --git a/homeassistant/components/rainforest_eagle/manifest.json b/homeassistant/components/rainforest_eagle/manifest.json new file mode 100644 index 00000000000000..7da2b5065380cb --- /dev/null +++ b/homeassistant/components/rainforest_eagle/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "rainforest_eagle", + "name": "Rainforest Eagle-200", + "documentation": "https://www.home-assistant.io/components/rainforest_eagle", + "requirements": [ + "eagle200_reader==0.1.4" + ], + "dependencies": [], + "codeowners": ["@gtdiehl"] +} diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py new file mode 100644 index 00000000000000..7c6750aba1c83f --- /dev/null +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -0,0 +1,87 @@ +"""Support for the Rainforest Eagle-200 energy monitor.""" +import logging + +import voluptuous as vol + +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import ( + CONF_IP_ADDRESS, CONF_MONITORED_CONDITIONS, ENERGY_KILO_WATT_HOUR) +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity import Entity + +CONF_CLOUD_ID = 'cloud_id' +CONF_INSTALL_CODE = 'install_code' +POWER_KILO_WATT = 'kW' + +_LOGGER = logging.getLogger(__name__) + +SENSORS = { + "instantanous_demand": ( + "Eagle-200 Meter Power Demand", POWER_KILO_WATT), + "summation_delivered": ( + "Eagle-200 Total Meter Energy Delivered", + ENERGY_KILO_WATT_HOUR), + "summation_received": ( + "Eagle-200 Total Meter Energy Received", + ENERGY_KILO_WATT_HOUR), + "summation_total": ( + "Eagle-200 Net Meter Energy (Delivered minus Received)", + ENERGY_KILO_WATT_HOUR) + } + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_IP_ADDRESS): cv.string, + vol.Required(CONF_CLOUD_ID): cv.string, + vol.Required(CONF_INSTALL_CODE): cv.string, + vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)): + vol.All(cv.ensure_list, [vol.In(list(SENSORS))]) +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Create the Eagle-200 sensor.""" + ip_address = config[CONF_IP_ADDRESS] + cloud_id = config[CONF_CLOUD_ID] + install_code = config[CONF_INSTALL_CODE] + monitored_conditions = config[CONF_MONITORED_CONDITIONS] + for condition in monitored_conditions: + add_devices([Eagle(ip_address, cloud_id, install_code, condition, + SENSORS[condition][0], SENSORS[condition][1])]) + + +class Eagle(Entity): + """Implementation of the Rainforest Eagle-200 sensor.""" + + def __init__( + self, ip_address, cloud_id, install_code, sensor_type, name, unit): + """Initialize the sensor.""" + self._ip_address = ip_address + self._cloud_id = cloud_id + self._install_code = install_code + self._type = sensor_type + self._name = name + self._unit_of_measurement = unit + self._state = None + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @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 self._unit_of_measurement + + def update(self): + """Get the energy demand from the Rainforest Eagle.""" + from eagle200_reader import EagleReader + + self._state = getattr(EagleReader( + self._ip_address, self._cloud_id, + self._install_code), self._type)() diff --git a/requirements_all.txt b/requirements_all.txt index 07f537113122c7..0a4cc8b84bb761 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -387,6 +387,9 @@ dsmr_parser==0.12 # homeassistant.components.dweet dweepy==0.3.0 +# homeassistant.components.rainforest_eagle +eagle200_reader==0.1.4 + # homeassistant.components.ebusd ebusdpy==0.0.16 From 37be16e10ab4f63f0f44d31f82652c6c15295bed Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Wed, 3 Jul 2019 13:06:22 -0700 Subject: [PATCH 02/12] Removed direct access selector to monitored conditions --- homeassistant/components/rainforest_eagle/sensor.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 7c6750aba1c83f..663b1b63ca4a37 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -5,7 +5,7 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( - CONF_IP_ADDRESS, CONF_MONITORED_CONDITIONS, ENERGY_KILO_WATT_HOUR) + CONF_IP_ADDRESS, ENERGY_KILO_WATT_HOUR) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity @@ -32,9 +32,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_IP_ADDRESS): cv.string, vol.Required(CONF_CLOUD_ID): cv.string, - vol.Required(CONF_INSTALL_CODE): cv.string, - vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)): - vol.All(cv.ensure_list, [vol.In(list(SENSORS))]) + vol.Required(CONF_INSTALL_CODE): cv.string }) @@ -43,7 +41,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): ip_address = config[CONF_IP_ADDRESS] cloud_id = config[CONF_CLOUD_ID] install_code = config[CONF_INSTALL_CODE] - monitored_conditions = config[CONF_MONITORED_CONDITIONS] + monitored_conditions = list(SENSORS) for condition in monitored_conditions: add_devices([Eagle(ip_address, cloud_id, install_code, condition, SENSORS[condition][0], SENSORS[condition][1])]) From 24e0232b196f6558922c4d87590822584b611731 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Wed, 3 Jul 2019 21:12:24 -0700 Subject: [PATCH 03/12] Refactored code to use throttle on the update function --- .../components/rainforest_eagle/sensor.py | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 663b1b63ca4a37..7ed6c97d51a1d0 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -1,13 +1,17 @@ """Support for the Rainforest Eagle-200 energy monitor.""" +from datetime import timedelta import logging +from requests.exceptions import ( + ConnectionError as ConnectError, HTTPError, Timeout) import voluptuous as vol from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( - CONF_IP_ADDRESS, ENERGY_KILO_WATT_HOUR) + CONF_IP_ADDRESS, CONF_SCAN_INTERVAL, ENERGY_KILO_WATT_HOUR) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity +from homeassistant.util import Throttle CONF_CLOUD_ID = 'cloud_id' CONF_INSTALL_CODE = 'install_code' @@ -15,6 +19,8 @@ _LOGGER = logging.getLogger(__name__) +SCAN_INTERVAL = timedelta(seconds=30) + SENSORS = { "instantanous_demand": ( "Eagle-200 Meter Power Demand", POWER_KILO_WATT), @@ -41,21 +47,28 @@ def setup_platform(hass, config, add_devices, discovery_info=None): ip_address = config[CONF_IP_ADDRESS] cloud_id = config[CONF_CLOUD_ID] install_code = config[CONF_INSTALL_CODE] + interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL) + + eagle_data = EagleData(ip_address, cloud_id, install_code, interval) + eagle_data.update() + monitored_conditions = list(SENSORS) + sensors = [] for condition in monitored_conditions: - add_devices([Eagle(ip_address, cloud_id, install_code, condition, - SENSORS[condition][0], SENSORS[condition][1])]) + sensors.append(EagleSensor( + eagle_data, condition, SENSORS[condition][0], + SENSORS[condition][1])) + add_devices(sensors) -class Eagle(Entity): + +class EagleSensor(Entity): """Implementation of the Rainforest Eagle-200 sensor.""" def __init__( - self, ip_address, cloud_id, install_code, sensor_type, name, unit): + self, eagle_data, sensor_type, name, unit): """Initialize the sensor.""" - self._ip_address = ip_address - self._cloud_id = cloud_id - self._install_code = install_code + self.eagle_data = eagle_data self._type = sensor_type self._name = name self._unit_of_measurement = unit @@ -77,9 +90,41 @@ def unit_of_measurement(self): return self._unit_of_measurement def update(self): - """Get the energy demand from the Rainforest Eagle.""" + """Get the energy information from the Rainforest Eagle.""" + self.eagle_data.update() + data = self.eagle_data.data + self._state = self.get_state(data) + + def get_state(self, data): + """Get the sensor value from the dictionary.""" + state = data.get(self._type) + return state + + +class EagleData: + """Get the latest data from the Eagle-200 device.""" + + def __init__(self, ip_address, cloud_id, install_code, interval): + """Initialize the data object.""" + self._ip_address = ip_address + self._cloud_id = cloud_id + self._install_code = install_code + self.interval = interval + + self.data = {} + + # Apply throttling to update method using configured interval. + self.update = Throttle(interval)(self._update) + + def _update(self): + """Get the latest data from the Eagle-200 device.""" from eagle200_reader import EagleReader - self._state = getattr(EagleReader( - self._ip_address, self._cloud_id, - self._install_code), self._type)() + for sensor_type in SENSORS: + try: + self.data.update({sensor_type: getattr(EagleReader( + self._ip_address, self._cloud_id, self._install_code), + sensor_type)()}) + except (ConnectError, HTTPError, Timeout, ValueError) as error: + _LOGGER.error("Unable to connect to the Eagle-200: %s", error) + self.data.update({sensor_type: None}) From d9b1f5fb6a61bb19456da74749a67acacf45455a Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Wed, 3 Jul 2019 21:38:45 -0700 Subject: [PATCH 04/12] Fixed issue in new code to use only one EagleReader instance --- .../components/rainforest_eagle/sensor.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 7ed6c97d51a1d0..4a626f1de96f3d 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -97,6 +97,9 @@ def update(self): def get_state(self, data): """Get the sensor value from the dictionary.""" + if data is None: + return None + state = data.get(self._type) return state @@ -120,11 +123,13 @@ def _update(self): """Get the latest data from the Eagle-200 device.""" from eagle200_reader import EagleReader - for sensor_type in SENSORS: - try: - self.data.update({sensor_type: getattr(EagleReader( - self._ip_address, self._cloud_id, self._install_code), - sensor_type)()}) - except (ConnectError, HTTPError, Timeout, ValueError) as error: - _LOGGER.error("Unable to connect to the Eagle-200: %s", error) - self.data.update({sensor_type: None}) + try: + eagle_reader = EagleReader( + self._ip_address, self._cloud_id, self._install_code) + + for sensor_type in SENSORS: + self.data.update({sensor_type: getattr( + eagle_reader, sensor_type)()}) + except (ConnectError, HTTPError, Timeout, ValueError) as error: + _LOGGER.error("Unable to connect to the Eagle-200: %s", error) + self.data = None From 1c0e6c10303739e4067e2e77ca9bbb917b2c91c1 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Fri, 12 Jul 2019 22:22:23 -0700 Subject: [PATCH 05/12] Resolve comments --- .../components/rainforest_eagle/manifest.json | 2 +- .../components/rainforest_eagle/sensor.py | 68 ++++++++++++------- requirements_all.txt | 2 +- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/manifest.json b/homeassistant/components/rainforest_eagle/manifest.json index 7da2b5065380cb..354eaf8b313105 100644 --- a/homeassistant/components/rainforest_eagle/manifest.json +++ b/homeassistant/components/rainforest_eagle/manifest.json @@ -3,7 +3,7 @@ "name": "Rainforest Eagle-200", "documentation": "https://www.home-assistant.io/components/rainforest_eagle", "requirements": [ - "eagle200_reader==0.1.4" + "eagle200_reader==0.2.1" ], "dependencies": [], "codeowners": ["@gtdiehl"] diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 4a626f1de96f3d..fc34b07742346e 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -2,13 +2,15 @@ from datetime import timedelta import logging +from eagle200_reader import EagleReader from requests.exceptions import ( ConnectionError as ConnectError, HTTPError, Timeout) import voluptuous as vol from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( - CONF_IP_ADDRESS, CONF_SCAN_INTERVAL, ENERGY_KILO_WATT_HOUR) + CONF_IP_ADDRESS, CONF_SCAN_INTERVAL, DEVICE_CLASS_POWER, + ENERGY_KILO_WATT_HOUR) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.util import Throttle @@ -19,7 +21,7 @@ _LOGGER = logging.getLogger(__name__) -SCAN_INTERVAL = timedelta(seconds=30) +MIN_SCAN_INTERVAL = timedelta(seconds=30) SENSORS = { "instantanous_demand": ( @@ -42,24 +44,28 @@ }) -def setup_platform(hass, config, add_devices, discovery_info=None): +def setup_platform(hass, config, add_entities, discovery_info=None): """Create the Eagle-200 sensor.""" ip_address = config[CONF_IP_ADDRESS] cloud_id = config[CONF_CLOUD_ID] install_code = config[CONF_INSTALL_CODE] - interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL) + interval = config.get(CONF_SCAN_INTERVAL) - eagle_data = EagleData(ip_address, cloud_id, install_code, interval) - eagle_data.update() + try: + eagle_data = EagleData(ip_address, cloud_id, install_code, interval) + except (ConnectError, HTTPError, Timeout, ValueError) as error: + _LOGGER.error("Failed to setup Eagle-200 sensor: %s", error) + else: + eagle_data.update() - monitored_conditions = list(SENSORS) - sensors = [] - for condition in monitored_conditions: - sensors.append(EagleSensor( - eagle_data, condition, SENSORS[condition][0], - SENSORS[condition][1])) + monitored_conditions = list(SENSORS) + sensors = [] + for condition in monitored_conditions: + sensors.append(EagleSensor( + eagle_data, condition, SENSORS[condition][0], + SENSORS[condition][1])) - add_devices(sensors) + add_entities(sensors) class EagleSensor(Entity): @@ -74,6 +80,11 @@ def __init__( self._unit_of_measurement = unit self._state = None + @property + def device_class(self): + """Return the device class of the sensor.""" + return DEVICE_CLASS_POWER + @property def name(self): """Return the name of the sensor.""" @@ -89,11 +100,17 @@ def unit_of_measurement(self): """Return the unit of measurement.""" return self._unit_of_measurement + @Throttle(MIN_SCAN_INTERVAL) def update(self): """Get the energy information from the Rainforest Eagle.""" - self.eagle_data.update() - data = self.eagle_data.data - self._state = self.get_state(data) + try: + self.eagle_data.update() + except (ConnectError, HTTPError, Timeout, ValueError) as error: + _LOGGER.error("Unable to update Eagle-200 %s: %s", + self._name, error) + else: + data = self.eagle_data.data + self._state = self.get_state(data) def get_state(self, data): """Get the sensor value from the dictionary.""" @@ -113,23 +130,22 @@ def __init__(self, ip_address, cloud_id, install_code, interval): self._cloud_id = cloud_id self._install_code = install_code self.interval = interval - self.data = {} + try: + self.eagle_reader = EagleReader( + self._ip_address, self._cloud_id, self._install_code) + except (ConnectError, HTTPError, Timeout, ValueError) as error: + _LOGGER.error("Unable to connect during setup: %s", error) + self.data = None + # Apply throttling to update method using configured interval. self.update = Throttle(interval)(self._update) def _update(self): """Get the latest data from the Eagle-200 device.""" - from eagle200_reader import EagleReader - try: - eagle_reader = EagleReader( - self._ip_address, self._cloud_id, self._install_code) - - for sensor_type in SENSORS: - self.data.update({sensor_type: getattr( - eagle_reader, sensor_type)()}) + self.data = self.eagle_reader.update() except (ConnectError, HTTPError, Timeout, ValueError) as error: - _LOGGER.error("Unable to connect to the Eagle-200: %s", error) + _LOGGER.error("Unable to connect during update: %s", error) self.data = None diff --git a/requirements_all.txt b/requirements_all.txt index 0a4cc8b84bb761..9ec0043c43014a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -388,7 +388,7 @@ dsmr_parser==0.12 dweepy==0.3.0 # homeassistant.components.rainforest_eagle -eagle200_reader==0.1.4 +eagle200_reader==0.2.1 # homeassistant.components.ebusd ebusdpy==0.0.16 From b66414d034eb40f477f59744b35f7497bfc52602 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Mon, 15 Jul 2019 14:05:17 -0700 Subject: [PATCH 06/12] Resolved comments --- .../components/rainforest_eagle/sensor.py | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index fc34b07742346e..1a0a9dbcc1e7ba 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -9,7 +9,7 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( - CONF_IP_ADDRESS, CONF_SCAN_INTERVAL, DEVICE_CLASS_POWER, + CONF_IP_ADDRESS, DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity @@ -49,10 +49,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): ip_address = config[CONF_IP_ADDRESS] cloud_id = config[CONF_CLOUD_ID] install_code = config[CONF_INSTALL_CODE] - interval = config.get(CONF_SCAN_INTERVAL) try: - eagle_data = EagleData(ip_address, cloud_id, install_code, interval) + eagle_data = EagleData(ip_address, cloud_id, install_code) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Failed to setup Eagle-200 sensor: %s", error) else: @@ -82,8 +81,9 @@ def __init__( @property def device_class(self): - """Return the device class of the sensor.""" - return DEVICE_CLASS_POWER + """Return the power device class for the instantanous_demand sensor.""" + if self._type == 'instantanous_demand': + return DEVICE_CLASS_POWER @property def name(self): @@ -105,12 +105,11 @@ def update(self): """Get the energy information from the Rainforest Eagle.""" try: self.eagle_data.update() + data = self.eagle_data.data + self._state = self.get_state(data) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to update Eagle-200 %s: %s", self._name, error) - else: - data = self.eagle_data.data - self._state = self.get_state(data) def get_state(self, data): """Get the sensor value from the dictionary.""" @@ -124,12 +123,11 @@ def get_state(self, data): class EagleData: """Get the latest data from the Eagle-200 device.""" - def __init__(self, ip_address, cloud_id, install_code, interval): + def __init__(self, ip_address, cloud_id, install_code): """Initialize the data object.""" self._ip_address = ip_address self._cloud_id = cloud_id self._install_code = install_code - self.interval = interval self.data = {} try: @@ -139,10 +137,7 @@ def __init__(self, ip_address, cloud_id, install_code, interval): _LOGGER.error("Unable to connect during setup: %s", error) self.data = None - # Apply throttling to update method using configured interval. - self.update = Throttle(interval)(self._update) - - def _update(self): + def update(self): """Get the latest data from the Eagle-200 device.""" try: self.data = self.eagle_reader.update() From 26d7acdeb757465ad3587491516f8f3431839fa6 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Mon, 15 Jul 2019 20:34:05 -0700 Subject: [PATCH 07/12] Resolved comments and added Debug statement --- .../components/rainforest_eagle/sensor.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 1a0a9dbcc1e7ba..e6df0b456bd962 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -54,17 +54,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None): eagle_data = EagleData(ip_address, cloud_id, install_code) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Failed to setup Eagle-200 sensor: %s", error) - else: - eagle_data.update() - monitored_conditions = list(SENSORS) - sensors = [] - for condition in monitored_conditions: - sensors.append(EagleSensor( - eagle_data, condition, SENSORS[condition][0], - SENSORS[condition][1])) + eagle_data.update() + monitored_conditions = list(SENSORS) + sensors = [] + for condition in monitored_conditions: + sensors.append(EagleSensor( + eagle_data, condition, SENSORS[condition][0], + SENSORS[condition][1])) - add_entities(sensors) + add_entities(sensors) class EagleSensor(Entity): @@ -85,6 +84,8 @@ def device_class(self): if self._type == 'instantanous_demand': return DEVICE_CLASS_POWER + return None + @property def name(self): """Return the name of the sensor.""" @@ -100,17 +101,17 @@ def unit_of_measurement(self): """Return the unit of measurement.""" return self._unit_of_measurement - @Throttle(MIN_SCAN_INTERVAL) def update(self): """Get the energy information from the Rainforest Eagle.""" try: self.eagle_data.update() - data = self.eagle_data.data - self._state = self.get_state(data) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to update Eagle-200 %s: %s", self._name, error) + data = self.eagle_data.data + self._state = self.get_state(data) + def get_state(self, data): """Get the sensor value from the dictionary.""" if data is None: @@ -137,10 +138,12 @@ def __init__(self, ip_address, cloud_id, install_code): _LOGGER.error("Unable to connect during setup: %s", error) self.data = None + @Throttle(MIN_SCAN_INTERVAL) def update(self): """Get the latest data from the Eagle-200 device.""" try: self.data = self.eagle_reader.update() + _LOGGER.debug("API data: %s", self.data) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to connect during update: %s", error) self.data = None From fc354708f5b6aa3fa857431ccaac7b46389d4b15 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Tue, 16 Jul 2019 17:19:10 -0700 Subject: [PATCH 08/12] Added return statements --- homeassistant/components/rainforest_eagle/sensor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index e6df0b456bd962..d2a1789bb31393 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -54,6 +54,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): eagle_data = EagleData(ip_address, cloud_id, install_code) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Failed to setup Eagle-200 sensor: %s", error) + return eagle_data.update() monitored_conditions = list(SENSORS) @@ -108,6 +109,8 @@ def update(self): except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to update Eagle-200 %s: %s", self._name, error) + self._name = None + return data = self.eagle_data.data self._state = self.get_state(data) From aeb66375f7dcb1f5e544a0caf677072481e32720 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Tue, 16 Jul 2019 17:23:40 -0700 Subject: [PATCH 09/12] Fixed typo --- homeassistant/components/rainforest_eagle/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index d2a1789bb31393..a4e3fa8f270961 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -109,7 +109,7 @@ def update(self): except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to update Eagle-200 %s: %s", self._name, error) - self._name = None + self._state = None return data = self.eagle_data.data From d0a0c4c2ebb98e9386689da88fef117830a2db9d Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Wed, 17 Jul 2019 11:19:18 -0700 Subject: [PATCH 10/12] Resolved comments and added debug statements --- .../components/rainforest_eagle/sensor.py | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index a4e3fa8f270961..041e777850bdf9 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -51,11 +51,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): install_code = config[CONF_INSTALL_CODE] try: - eagle_data = EagleData(ip_address, cloud_id, install_code) + eagle_reader = EagleReader(ip_address, cloud_id, install_code) except (ConnectError, HTTPError, Timeout, ValueError) as error: - _LOGGER.error("Failed to setup Eagle-200 sensor: %s", error) + _LOGGER.error("Failed to connect during setup: %s", error) return + eagle_data = EagleData(eagle_reader) eagle_data.update() monitored_conditions = list(SENSORS) sensors = [] @@ -104,48 +105,34 @@ def unit_of_measurement(self): def update(self): """Get the energy information from the Rainforest Eagle.""" - try: - self.eagle_data.update() - except (ConnectError, HTTPError, Timeout, ValueError) as error: - _LOGGER.error("Unable to update Eagle-200 %s: %s", - self._name, error) - self._state = None - return - + self.eagle_data.update() data = self.eagle_data.data self._state = self.get_state(data) def get_state(self, data): """Get the sensor value from the dictionary.""" if data is None: + _LOGGER.debug("Empty data during update") return None state = data.get(self._type) + _LOGGER.debug("Updating: %s - %s", self._type, data.get(self._type)) return state class EagleData: """Get the latest data from the Eagle-200 device.""" - def __init__(self, ip_address, cloud_id, install_code): + def __init__(self, eagle_reader): """Initialize the data object.""" - self._ip_address = ip_address - self._cloud_id = cloud_id - self._install_code = install_code + self._eagle_reader = eagle_reader self.data = {} - try: - self.eagle_reader = EagleReader( - self._ip_address, self._cloud_id, self._install_code) - except (ConnectError, HTTPError, Timeout, ValueError) as error: - _LOGGER.error("Unable to connect during setup: %s", error) - self.data = None - @Throttle(MIN_SCAN_INTERVAL) def update(self): """Get the latest data from the Eagle-200 device.""" try: - self.data = self.eagle_reader.update() + self.data = self._eagle_reader.update() _LOGGER.debug("API data: %s", self.data) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to connect during update: %s", error) From 2e798214c7bc884b118f90f7933b9d6fc8d8aa62 Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Wed, 17 Jul 2019 13:31:54 -0700 Subject: [PATCH 11/12] Moved get_status method into Data object and decorated it with @staticmethod --- .../components/rainforest_eagle/sensor.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 041e777850bdf9..b5844698c99a15 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -107,17 +107,7 @@ def update(self): """Get the energy information from the Rainforest Eagle.""" self.eagle_data.update() data = self.eagle_data.data - self._state = self.get_state(data) - - def get_state(self, data): - """Get the sensor value from the dictionary.""" - if data is None: - _LOGGER.debug("Empty data during update") - return None - - state = data.get(self._type) - _LOGGER.debug("Updating: %s - %s", self._type, data.get(self._type)) - return state + self._state = EagleData.get_state(data, self._type) class EagleData: @@ -137,3 +127,14 @@ def update(self): except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to connect during update: %s", error) self.data = None + + @staticmethod + def get_state(data, sensor_type): + """Get the sensor value from the dictionary.""" + if data is None: + _LOGGER.debug("Empty data during update") + return None + + state = data.get(sensor_type) + _LOGGER.debug("Updating: %s - %s", sensor_type, data.get(sensor_type)) + return state From 64326b6ab5530eb1f4bd7cd3912768885e6527bb Mon Sep 17 00:00:00 2001 From: gtdiehl Date: Wed, 17 Jul 2019 16:50:48 -0700 Subject: [PATCH 12/12] Resolved comments --- .../components/rainforest_eagle/sensor.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index b5844698c99a15..fb652c75d297bc 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -106,8 +106,7 @@ def unit_of_measurement(self): def update(self): """Get the energy information from the Rainforest Eagle.""" self.eagle_data.update() - data = self.eagle_data.data - self._state = EagleData.get_state(data, self._type) + self._state = self.eagle_data.get_state(self._type) class EagleData: @@ -126,15 +125,10 @@ def update(self): _LOGGER.debug("API data: %s", self.data) except (ConnectError, HTTPError, Timeout, ValueError) as error: _LOGGER.error("Unable to connect during update: %s", error) - self.data = None + self.data = {} - @staticmethod - def get_state(data, sensor_type): + def get_state(self, sensor_type): """Get the sensor value from the dictionary.""" - if data is None: - _LOGGER.debug("Empty data during update") - return None - - state = data.get(sensor_type) - _LOGGER.debug("Updating: %s - %s", sensor_type, data.get(sensor_type)) + state = self.data.get(sensor_type) + _LOGGER.debug("Updating: %s - %s", sensor_type, state) return state