def _convert_states(states): """Convert state definitions to State objects.""" result = {} for entity_id in states: entity_id = cv.entity_id(entity_id) if isinstance(states[entity_id], dict): entity_attrs = states[entity_id].copy() state = entity_attrs.pop(ATTR_STATE, None) attributes = entity_attrs else: state = states[entity_id] attributes = {} # YAML translates 'on' to a boolean # http://yaml.org/type/bool.html if isinstance(state, bool): state = STATE_ON if state else STATE_OFF elif not isinstance(state, str): raise vol.Invalid(f"State for {entity_id} should be a string") result[entity_id] = State(entity_id, state, attributes) return result
def validate_entity_config(values): """Validate config entry for CONF_ENTITY.""" entities = {} for entity_id, config in values.items(): entity = cv.entity_id(entity_id) domain, _ = split_entity_id(entity) if not isinstance(config, dict): raise vol.Invalid('The configuration for {} must be ' ' a dictionary.'.format(entity)) if domain in ('alarm_control_panel', 'lock'): config = CODE_SCHEMA(config) elif domain == media_player.DOMAIN: config = FEATURE_SCHEMA(config) feature_list = {} for feature in config[CONF_FEATURE_LIST]: params = MEDIA_PLAYER_SCHEMA(feature) key = params.pop(CONF_FEATURE) if key in feature_list: raise vol.Invalid( 'A feature can be added only once for {}'.format( entity)) feature_list[key] = params config[CONF_FEATURE_LIST] = feature_list elif domain == 'switch': config = SWITCH_TYPE_SCHEMA(config) else: config = BASIC_INFO_SCHEMA(config) entities[entity] = config return entities
def validate_entity_config(values): """Validate config entry for CONF_ENTITY.""" if not isinstance(values, dict): raise vol.Invalid("expected a dictionary") entities = {} for entity_id, config in values.items(): entity = cv.entity_id(entity_id) domain, _ = split_entity_id(entity) if not isinstance(config, dict): raise vol.Invalid(f"The configuration for {entity} must be a dictionary.") if domain in ("alarm_control_panel", "lock"): config = CODE_SCHEMA(config) elif domain == media_player.const.DOMAIN: config = FEATURE_SCHEMA(config) feature_list = {} for feature in config[CONF_FEATURE_LIST]: params = MEDIA_PLAYER_SCHEMA(feature) key = params.pop(CONF_FEATURE) if key in feature_list: raise vol.Invalid(f"A feature can be added only once for {entity}") feature_list[key] = params config[CONF_FEATURE_LIST] = feature_list elif domain == "switch": config = SWITCH_TYPE_SCHEMA(config) else: config = BASIC_INFO_SCHEMA(config) entities[entity] = config return entities
def validate_entity_config(values): """Validate config entry for CONF_ENTITY.""" entities = {} for entity_id, config in values.items(): entity = cv.entity_id(entity_id) domain, _ = split_entity_id(entity) if not isinstance(config, dict): raise vol.Invalid('The configuration for {} must be ' ' a dictionary.'.format(entity)) if domain in ('alarm_control_panel', 'lock'): config = CODE_SCHEMA(config) elif domain == media_player.DOMAIN: config = FEATURE_SCHEMA(config) feature_list = {} for feature in config[CONF_FEATURE_LIST]: params = MEDIA_PLAYER_SCHEMA(feature) key = params.pop(CONF_FEATURE) if key in feature_list: raise vol.Invalid('A feature can be added only once for {}' .format(entity)) feature_list[key] = params config[CONF_FEATURE_LIST] = feature_list elif domain == 'switch': config = SWITCH_TYPE_SCHEMA(config) else: config = BASIC_INFO_SCHEMA(config) entities[entity] = config return entities
def validate_entity_config(values): """Validate config entry for CONF_ENTITY.""" entity_config_schema = vol.Schema({vol.Optional(CONF_REVERSE): cv.boolean}) if not isinstance(values, dict): raise vol.Invalid("expected a dictionary") entities = {} for entity_id, config in values.items(): entity = cv.entity_id(entity_id) config = entity_config_schema(config) entities[entity] = config return entities
def check_data(user_input, hass): """Check validity of the provided date.""" ret = {} try: if (not (entity_id(user_input[CONF_NET]))): _LOGGER.error("ICS: ID not unique") ret["base"] = ERROR_ID_NOT_UNIQUE return ret url = "http://" + user_input[CONF_energy_calc_URL] + "/realtime.csv" ret = requests.get(url).content return {} except Exception: ret["base"] = ERROR_URL return ret
def validate_entity_config(values): """Validate config entry for CONF_ENTITY.""" entities = {} for key, config in values.items(): entity = cv.entity_id(key) params = {} if not isinstance(config, dict): raise vol.Invalid('The configuration for "{}" must be ' ' an dictionary.'.format(entity)) domain, _ = split_entity_id(entity) if domain == 'alarm_control_panel': code = config.get(ATTR_CODE) params[ATTR_CODE] = cv.string(code) if code else None entities[entity] = params return entities
async def async_step_configure(self, user_input=None): """Step when user configures a calibrated sensor.""" errors = {} if user_input and user_input.get(CONF_ENTITY_ID): await self.async_set_unique_id(user_input.get(CONF_ENTITY_ID)) self._abort_if_unique_id_configured() try: # Validate the user provided entity_id. cv.entity_id(user_input[CONF_ENTITY_ID]) return self.async_create_entry( title=f"{ self.unique_id }", data={ CONF_ENTITY_ID: self.unique_id, CONF_TRACKED_ENTITY_ID: self.entity_id_from_user_step, CONF_NAME: user_input[CONF_NAME], CONF_ATTRIBUTE: user_input.get(CONF_ATTRIBUTE, None), CONF_PRECISION: user_input[CONF_PRECISION], CONF_MQTT_TOPIC: user_input[CONF_MQTT_TOPIC], CONF_DEGREE: user_input[CONF_DEGREE], CONF_UNIT_OF_MEASUREMENT: user_input[CONF_UNIT_OF_MEASUREMENT], }, ) except vol.Invalid as e: errors[CONF_ENTITY_ID] = "entity_id_not_valid" tracked_entity_id = self.entity_id_from_user_step if tracked_entity_id.endswith("_raw"): default_entity_id = tracked_entity_id.replace( "_raw", "_calibrated") default_name = self.hass.states.get( tracked_entity_id).name.replace(" Raw", " Calibrated") else: default_entity_id = f"{ tracked_entity_id }_calibrated" default_name = f"{ self.hass.states.get(tracked_entity_id).name } Calibrated" calibration_entity_id = default_entity_id.replace( '_calibrated', '_calibration') if self.hass.states.get(tracked_entity_id): device_name = re.sub("(.*\d+).*", "\\1", calibration_entity_id.replace("sensor.", "")) default_topic = f"{ user_input.get(CONF_MQTT_PREFIX, '') if user_input else self._get_prefix(None) }{ device_name }/{ calibration_entity_id.replace(f'sensor.{ device_name }_', '') }" else: default_topic = "" return self.async_show_form( step_id="configure", data_schema=vol.Schema({ vol.Required(CONF_ENTITY_ID, default=default_entity_id): cv.string, vol.Optional(CONF_NAME, default=default_name): cv.string, vol.Optional(CONF_ATTRIBUTE): vol.In( list( self.hass.states.get( tracked_entity_id).attributes.keys())), vol.Optional(CONF_PRECISION, default=DEFAULT_PRECISION): cv.positive_int, vol.Optional(CONF_MQTT_TOPIC, default=default_topic): cv.string, vol.Optional(CONF_DEGREE, default=DEFAULT_DEGREE): vol.All( vol.Coerce(int), vol.Range(min=1, max=7), ), vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=f"{ self.hass.states.get(tracked_entity_id).attributes.get(CONF_UNIT_OF_MEASUREMENT) }"): cv.string, }), errors=errors, )