Skip to content

Commit

Permalink
Add rainforest_eagle support for legacy hardware (home-assistant#28082)
Browse files Browse the repository at this point in the history
* Add compatibility with Legacy EAGLE models.

* added mdns resultion via zeroconf

* Added requirements to requirements_all.txt

* Fixed model determination bug

* Fixed formatting issue for Eagle-200 responses.

* Remove mDNS resolution, IP now required.

* added pypi deps, fixed handling of optional params

manifest updates

* Fixed import order per isort.

* Two pylint fixes.

* More consistent try-fail structure to guessing hardware.  Errors actually fail.
  • Loading branch information
jcalbert authored Feb 16, 2020
1 parent f6d9e6b commit fee0d8d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ homeassistant/components/quantum_gateway/* @cisasteelersfan
homeassistant/components/qwikswitch/* @kellerza
homeassistant/components/rainbird/* @konikvranik
homeassistant/components/raincloud/* @vanstinator
homeassistant/components/rainforest_eagle/* @gtdiehl
homeassistant/components/rainforest_eagle/* @gtdiehl @jcalbert
homeassistant/components/rainmachine/* @bachya
homeassistant/components/random/* @fabaff
homeassistant/components/repetier/* @MTrab
Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/rainforest_eagle/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"domain": "rainforest_eagle",
"name": "Rainforest Eagle-200",
"documentation": "https://www.home-assistant.io/integrations/rainforest_eagle",
"requirements": ["eagle200_reader==0.2.1"],
"requirements": [
"eagle200_reader==0.2.1",
"uEagle==0.0.1"
],
"dependencies": [],
"codeowners": ["@gtdiehl"]
"codeowners": ["@gtdiehl",
"@jcalbert"]
}
40 changes: 39 additions & 1 deletion homeassistant/components/rainforest_eagle/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from eagle200_reader import EagleReader
from requests.exceptions import ConnectionError as ConnectError, HTTPError, Timeout
from uEagle import Eagle as LegacyReader
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
Expand Down Expand Up @@ -49,14 +50,33 @@
)


def hwtest(cloud_id, install_code, ip_address):
"""Try API call 'get_network_info' to see if target device is Legacy or Eagle-200."""
reader = LeagleReader(cloud_id, install_code, ip_address)
response = reader.get_network_info()

# Branch to test if target is Legacy Model
if "NetworkInfo" in response:
if response["NetworkInfo"].get("ModelId", None) == "Z109-EAGLE":
return reader

# Branch to test if target is Eagle-200 Model
if "Response" in response:
if response["Response"].get("Command", None) == "get_network_info":
return EagleReader(ip_address, cloud_id, install_code)

# Catch-all if hardware ID tests fail
raise ValueError("Couldn't determine device model.")


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]

try:
eagle_reader = EagleReader(ip_address, cloud_id, install_code)
eagle_reader = hwtest(cloud_id, install_code, ip_address)
except (ConnectError, HTTPError, Timeout, ValueError) as error:
_LOGGER.error("Failed to connect during setup: %s", error)
return
Expand Down Expand Up @@ -138,3 +158,21 @@ def get_state(self, sensor_type):
state = self.data.get(sensor_type)
_LOGGER.debug("Updating: %s - %s", sensor_type, state)
return state


class LeagleReader(LegacyReader):
"""Wraps uEagle to make it behave like eagle_reader, offering update()."""

def update(self):
"""Fetch and return the four sensor values in a dict."""
out = {}

resp = self.get_instantaneous_demand()["InstantaneousDemand"]
out["instantanous_demand"] = resp["Demand"]

resp = self.get_current_summation()["CurrentSummation"]
out["summation_delivered"] = resp["SummationDelivered"]
out["summation_received"] = resp["SummationReceived"]
out["summation_total"] = out["summation_delivered"] - out["summation_received"]

return out
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,9 @@ twentemilieu==0.2.0
# homeassistant.components.twilio
twilio==6.32.0

# homeassistant.components.rainforest_eagle
uEagle==0.0.1

# homeassistant.components.unifiled
unifiled==0.11

Expand Down

0 comments on commit fee0d8d

Please sign in to comment.