Skip to content

Commit

Permalink
Emoncms API now provides a Unit of Measurement (home-assistant#32042)
Browse files Browse the repository at this point in the history
* Emoncms API provides a Unit

The EmonCMS API has been amended to include a 'unit' as part of it's payload.  By using this information, all the sensors can be created without the need for individual sensors to be setup by type.

The change is backward compatible so if a unit type has been specified in the configuration, this will be used by default.

If no unit is pecified either by the Home Assistant configuration, or the Emoncms API, then the default of W will be used as before.

* Update sensor.py

Check the 'unit' key is in the API call. Older systems may not have that key in the payload.

* Modified approach with new configuration item

* Removed new config item

Removed the configuration item. The integration attempts to get the unit from the API.

If this fails *or* the unit key of the API is blank, either the specified unit, or the default will be used.

If approved, documentation will be updated.

* Update homeassistant/components/emoncms/sensor.py

Co-Authored-By: springstan <[email protected]>

* Update homeassistant/components/emoncms/sensor.py

Co-Authored-By: springstan <[email protected]>

* Apply suggestions from code review

* Apply suggestions from code review v2

* Update homeassistant/components/emoncms/sensor.py

Co-Authored-By: springstan <[email protected]>

* Update sensor.py

Update `config_unit`

Co-authored-by: springstan <[email protected]>
  • Loading branch information
balloob and springstan authored Mar 5, 2020
1 parent bfe1e8f commit daff87f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ homeassistant/components/eight_sleep/* @mezz64
homeassistant/components/elgato/* @frenck
homeassistant/components/elv/* @majuss
homeassistant/components/emby/* @mezz64
homeassistant/components/emoncms/* @borpin
homeassistant/components/enigma2/* @fbradyirl
homeassistant/components/enocean/* @bdurrer
homeassistant/components/entur_public_transport/* @hfurubotten
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/emoncms/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"documentation": "https://www.home-assistant.io/integrations/emoncms",
"requirements": [],
"dependencies": [],
"codeowners": []
"codeowners": ["@borpin"]
}
9 changes: 7 additions & 2 deletions homeassistant/components/emoncms/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

DECIMALS = 2
DEFAULT_UNIT = POWER_WATT

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)

ONLY_INCL_EXCL_NONE = "only_include_exclude_or_none"
Expand Down Expand Up @@ -73,7 +72,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
url = config.get(CONF_URL)
sensorid = config.get(CONF_ID)
value_template = config.get(CONF_VALUE_TEMPLATE)
unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
config_unit = config.get(CONF_UNIT_OF_MEASUREMENT)
exclude_feeds = config.get(CONF_EXCLUDE_FEEDID)
include_only_feeds = config.get(CONF_ONLY_INCLUDE_FEEDID)
sensor_names = config.get(CONF_SENSOR_NAMES)
Expand Down Expand Up @@ -105,6 +104,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if sensor_names is not None:
name = sensor_names.get(int(elem["id"]), None)

unit = elem.get("unit")
if unit:
unit_of_measurement = unit
else:
unit_of_measurement = config_unit

sensors.append(
EmonCmsSensor(
hass,
Expand Down

0 comments on commit daff87f

Please sign in to comment.