Use feed name assigned in EmonCMS if there is one (#10021)

The default names for the feeds created by the EmonCMS component are
like 'emoncms1_feedid_10', but
- This is the display name. The ID should be lowercase and underscored,
  but the display name should be readable. The ID gets derived from it
  and comes out formatted correctly.
- EmonCMS lets you assign names to feeds, so it makes sense to use those
  if they exist, rather than feed IDs. The ID is pretty meaningless and
  basically means you have to override every name to make it readable.
- Including the ID identifying the EmonCMS instance (i.e. the '1') makes
  the name clunkier and would only be useful for people with multiple
  EmonCMS instances, which is likely to be an extremely small group since
  one hub can run as many feeds as you need it to.

This changes the default behavior but still uses configured 'name' if
it's set, so it won't break the configuration of people who have
customized their feed names in HA config.
This commit is contained in:
Klaas Hoekema 2017-10-22 06:12:36 -04:00 committed by Pascal Vizeli
parent 62caea6bfb
commit 9418c61b25

View file

@ -112,8 +112,13 @@ class EmonCmsSensor(Entity):
unit_of_measurement, sensorid, elem):
"""Initialize the sensor."""
if name is None:
self._name = "emoncms{}_feedid_{}".format(
sensorid, elem["id"])
# Suppress ID in sensor name if it's 1, since most people won't
# have more than one EmonCMS source and it's redundant to show the
# ID if there's only one.
id_for_name = '' if str(sensorid) == '1' else sensorid
# Use the feed name assigned in EmonCMS or fall back to the feed ID
feed_name = elem.get('name') or 'Feed {}'.format(elem['id'])
self._name = "EmonCMS{} {}".format(id_for_name, feed_name)
else:
self._name = name
self._identifier = get_id(