Generate entity id correctly, was using friendly_name.

This commit is contained in:
pavoni 2016-02-01 17:45:18 +00:00
parent 10a41a22dc
commit 95748a6880

View File

@ -9,16 +9,20 @@ https://home-assistant.io/components/sensor.template/
""" """
import logging import logging
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity, generate_entity_id
from homeassistant.core import EVENT_STATE_CHANGED from homeassistant.core import EVENT_STATE_CHANGED
from homeassistant.const import ( from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_FRIENDLY_NAME,
CONF_VALUE_TEMPLATE, CONF_VALUE_TEMPLATE,
ATTR_UNIT_OF_MEASUREMENT) ATTR_UNIT_OF_MEASUREMENT)
from homeassistant.util import template from homeassistant.util import template, slugify
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
from homeassistant.components.sensor import DOMAIN
ENTITY_ID_FORMAT = DOMAIN + '.{}'
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_SENSORS = 'sensors' CONF_SENSORS = 'sensors'
STATE_ERROR = 'error' STATE_ERROR = 'error'
@ -34,9 +38,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False return False
for device, device_config in config[CONF_SENSORS].items(): for device, device_config in config[CONF_SENSORS].items():
if device != slugify(device):
_LOGGER.error("Found invalid key for sensor.template: %s. "
"Use %s instead", device, slugify(device))
continue
if not isinstance(device_config, dict): if not isinstance(device_config, dict):
_LOGGER.error("Missing configuration data for sensor %s", device) _LOGGER.error("Missing configuration data for sensor %s", device)
continue continue
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device) friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT) unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
state_template = device_config.get(CONF_VALUE_TEMPLATE) state_template = device_config.get(CONF_VALUE_TEMPLATE)
@ -44,9 +55,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error( _LOGGER.error(
"Missing %s for sensor %s", CONF_VALUE_TEMPLATE, device) "Missing %s for sensor %s", CONF_VALUE_TEMPLATE, device)
continue continue
sensors.append( sensors.append(
SensorTemplate( SensorTemplate(
hass, hass,
device,
friendly_name, friendly_name,
unit_of_measurement, unit_of_measurement,
state_template) state_template)
@ -64,10 +77,15 @@ class SensorTemplate(Entity):
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def __init__(self, def __init__(self,
hass, hass,
device_id,
friendly_name, friendly_name,
unit_of_measurement, unit_of_measurement,
state_template): state_template):
self.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, device_id,
hass=hass)
self.hass = hass self.hass = hass
self._name = friendly_name self._name = friendly_name
self._unit_of_measurement = unit_of_measurement self._unit_of_measurement = unit_of_measurement