def setup(hass, config): """Setup zone.""" entities = set() for key in extract_domain_configs(config, DOMAIN): entries = config[key] if not isinstance(entries, list): entries = entries, for entry in entries: name = entry.get(CONF_NAME, DEFAULT_NAME) latitude = convert(entry.get(ATTR_LATITUDE), float) longitude = convert(entry.get(ATTR_LONGITUDE), float) radius = convert(entry.get(ATTR_RADIUS, DEFAULT_RADIUS), float) icon = entry.get(ATTR_ICON) passive = entry.get(ATTR_PASSIVE, DEFAULT_PASSIVE) if None in (latitude, longitude): logging.getLogger(__name__).error( 'Each zone needs a latitude and longitude.') continue zone = Zone(hass, name, latitude, longitude, radius, icon, passive) zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, entities) zone.update_ha_state() entities.add(zone.entity_id) if ENTITY_ID_HOME not in entities: zone = Zone(hass, hass.config.location_name, hass.config.latitude, hass.config.longitude, DEFAULT_RADIUS, ICON_HOME, False) zone.entity_id = ENTITY_ID_HOME zone.update_ha_state() return True
def request_config(self, name, callback, description, description_image, submit_caption, fields, link_name, link_url): """Setup a request for configuration.""" entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, hass=self.hass) if fields is None: fields = [] request_id = self._generate_unique_id() self._requests[request_id] = (entity_id, fields, callback) data = { ATTR_CONFIGURE_ID: request_id, ATTR_FIELDS: fields, ATTR_FRIENDLY_NAME: name, } data.update({ key: value for key, value in [ (ATTR_DESCRIPTION, description), (ATTR_DESCRIPTION_IMAGE, description_image), (ATTR_SUBMIT_CAPTION, submit_caption), (ATTR_LINK_NAME, link_name), (ATTR_LINK_URL, link_url), ] if value is not None }) self.hass.states.set(entity_id, STATE_CONFIGURE, data) return request_id
def request_config( self, name, callback, description, description_image, submit_caption, fields, link_name, link_url): """Setup a request for configuration.""" entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, hass=self.hass) if fields is None: fields = [] request_id = self._generate_unique_id() self._requests[request_id] = (entity_id, fields, callback) data = { ATTR_CONFIGURE_ID: request_id, ATTR_FIELDS: fields, ATTR_FRIENDLY_NAME: name, } data.update({ key: value for key, value in [ (ATTR_DESCRIPTION, description), (ATTR_DESCRIPTION_IMAGE, description_image), (ATTR_SUBMIT_CAPTION, submit_caption), (ATTR_LINK_NAME, link_name), (ATTR_LINK_URL, link_url), ] if value is not None }) self.hass.states.set(entity_id, STATE_CONFIGURE, data) return request_id
def __init__(self, hass, scene_data, room_data, pv_instance): """Initialize the scene.""" self.pv_instance = pv_instance self.hass = hass self.scene_data = scene_data self._sync_room_data(room_data) self.entity_id_format = DOMAIN + '.{}' self.entity_id = generate_entity_id(self.entity_id_format, str(self.scene_data["id"]), hass=hass)
def __init__(self, hass, scene_data, room_data, pv_instance): """Initialize the scene.""" self.pv_instance = pv_instance self.hass = hass self.scene_data = scene_data self._sync_room_data(room_data) self.entity_id_format = DOMAIN + '.{}' self.entity_id = generate_entity_id(self.entity_id_format, str(self.scene_data["id"]), hass=hass)
def __init__(self, hass, device, friendly_name, sensor_class, value_template, entity_ids): """Initialize the Template binary sensor.""" self.hass = hass self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device, hass=hass) self._name = friendly_name self._sensor_class = sensor_class self._template = value_template self._state = None self.update() def template_bsensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) track_state_change(hass, entity_ids, template_bsensor_state_listener)
def __init__(self, hass, device_id, friendly_name, unit_of_measurement, state_template, entity_ids): """Initialize the sensor.""" self.hass = hass self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass) self._name = friendly_name self._unit_of_measurement = unit_of_measurement self._template = state_template self._state = None self.update() def template_sensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) track_state_change(hass, entity_ids, template_sensor_state_listener)
def __init__(self, hass, device, friendly_name, sensor_class, value_template, entity_ids): """Initialize the Template binary sensor.""" self.hass = hass self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device, hass=hass) self._name = friendly_name self._sensor_class = sensor_class self._template = value_template self._state = None self.update() def template_bsensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) track_state_change(hass, entity_ids, template_bsensor_state_listener)
def __init__(self, hass, device_id, friendly_name, unit_of_measurement, state_template, entity_ids): """Initialize the sensor.""" self.hass = hass self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass) self._name = friendly_name self._unit_of_measurement = unit_of_measurement self._template = state_template self._state = None self.update() def template_sensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) track_state_change(hass, entity_ids, template_sensor_state_listener)
def __init__(self, hass, device_id, friendly_name, state_template, on_action, off_action, entity_ids): """Initialize the Template switch.""" self.hass = hass self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass) self._name = friendly_name self._template = state_template self._on_script = Script(hass, on_action) self._off_script = Script(hass, off_action) self._state = False self.update() def template_switch_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) track_state_change(hass, entity_ids, template_switch_state_listener)
def __init__(self, hass, name, entity_ids=None, user_defined=True, icon=None, view=False, object_id=None): """Initialize a group.""" self.hass = hass self._name = name self._state = STATE_UNKNOWN self._order = len(hass.states.entity_ids(DOMAIN)) self._user_defined = user_defined self._icon = icon self._view = view self.entity_id = generate_entity_id( ENTITY_ID_FORMAT, object_id or name, hass=hass) self.tracking = [] self.group_on = None self.group_off = None self._assumed_state = False self._lock = threading.Lock() if entity_ids is not None: self.update_tracked_entity_ids(entity_ids) else: self.update_ha_state(True)
def add_entity(self, entity, platform=None): """Add entity to component.""" if entity is None or entity in self.entities.values(): return False entity.hass = self.hass if getattr(entity, 'entity_id', None) is None: object_id = entity.name or DEVICE_DEFAULT_NAME if platform is not None and platform.entity_namespace is not None: object_id = '{} {}'.format(platform.entity_namespace, object_id) entity.entity_id = generate_entity_id( self.entity_id_format, object_id, self.entities.keys()) self.entities[entity.entity_id] = entity entity.update_ha_state() return True
def add_entity(self, entity, platform=None): """Add entity to component.""" if entity is None or entity in self.entities.values(): return False entity.hass = self.hass if getattr(entity, 'entity_id', None) is None: object_id = entity.name or DEVICE_DEFAULT_NAME if platform is not None and platform.entity_namespace is not None: object_id = '{} {}'.format(platform.entity_namespace, object_id) entity.entity_id = generate_entity_id(self.entity_id_format, object_id, self.entities.keys()) self.entities[entity.entity_id] = entity entity.update_ha_state() return True