def async_request_config(self, name, callback, description, submit_caption, fields, entity_picture): """Set up a request for configuration.""" entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, opp=self.opp) 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, ATTR_ENTITY_PICTURE: entity_picture, } data.update({ key: value for key, value in [ (ATTR_DESCRIPTION, description), (ATTR_SUBMIT_CAPTION, submit_caption), ] if value is not None }) self.opp.states.async_set(entity_id, STATE_CONFIGURE, data) return request_id
def create_entity(number): """Create entity helper.""" entity = MockEntity(unique_id=f"unique{number}") entity.entity_id = async_generate_entity_id(DOMAIN + ".{}", "Number", opp=opp) return entity
def __init__( self, opp, device_id, friendly_name, state_template, icon_template, entity_picture_template, availability_template, on_action, off_action, unique_id, ): """Initialize the Template switch.""" super().__init__( availability_template=availability_template, icon_template=icon_template, entity_picture_template=entity_picture_template, ) self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id, opp=opp) self._name = friendly_name self._template = state_template domain = __name__.split(".")[-2] self._on_script = Script(opp, on_action, friendly_name, domain) self._off_script = Script(opp, off_action, friendly_name, domain) self._state = False self._unique_id = unique_id
def __init__(self, gw_dev, var, device_class, friendly_name_format): """Initialize the binary sensor.""" self.entity_id = async_generate_entity_id( ENTITY_ID_FORMAT, f"{var}_{gw_dev.gw_id}", opp=gw_dev.opp ) self._gateway = gw_dev self._var = var self._source = DEPRECATED_BINARY_SENSOR_SOURCE_LOOKUP[var] self._state = None self._device_class = device_class self._friendly_name = friendly_name_format.format(gw_dev.name) self._unsub_updates = None
async def async_setup_entry(opp, config_entry, async_add_entities): """Set up the OpenTherm Gateway sensors.""" sensors = [] deprecated_sensors = [] gw_dev = opp.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][ config_entry.data[CONF_ID]] ent_reg = await async_get_registry(opp) for var, info in SENSOR_INFO.items(): device_class = info[0] unit = info[1] friendly_name_format = info[2] status_sources = info[3] for source in status_sources: sensors.append( OpenThermSensor( gw_dev, var, source, device_class, unit, friendly_name_format, )) old_style_entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, f"{var}_{gw_dev.gw_id}", opp=gw_dev.opp) old_ent = ent_reg.async_get(old_style_entity_id) if old_ent and old_ent.config_entry_id == config_entry.entry_id: if old_ent.disabled: ent_reg.async_remove(old_style_entity_id) else: deprecated_sensors.append( DeprecatedOpenThermSensor( gw_dev, var, device_class, unit, friendly_name_format, )) sensors.extend(deprecated_sensors) if deprecated_sensors: _LOGGER.warning( "The following sensor entities are deprecated and may no " "longer behave as expected. They will be removed in a future " "version. You can force removal of these entities by disabling " "them and restarting Open Peer Power.\n%s", pformat([s.entity_id for s in deprecated_sensors]), ) async_add_entities(sensors)
async def async_setup_platform(opp, config, async_add_entities, discovery_info=None): """Set up the CityBikes platform.""" if PLATFORM not in opp.data: opp.data[PLATFORM] = {MONITORED_NETWORKS: {}} latitude = config.get(CONF_LATITUDE, opp.config.latitude) longitude = config.get(CONF_LONGITUDE, opp.config.longitude) network_id = config.get(CONF_NETWORK) stations_list = set(config.get(CONF_STATIONS_LIST, [])) radius = config.get(CONF_RADIUS, 0) name = config[CONF_NAME] if not opp.config.units.is_metric: radius = distance.convert(radius, LENGTH_FEET, LENGTH_METERS) # Create a single instance of CityBikesNetworks. networks = opp.data.setdefault(CITYBIKES_NETWORKS, CityBikesNetworks(opp)) if not network_id: network_id = await networks.get_closest_network_id(latitude, longitude) if network_id not in opp.data[PLATFORM][MONITORED_NETWORKS]: network = CityBikesNetwork(opp, network_id) opp.data[PLATFORM][MONITORED_NETWORKS][network_id] = network opp.async_create_task(network.async_refresh()) async_track_time_interval(opp, network.async_refresh, SCAN_INTERVAL) else: network = opp.data[PLATFORM][MONITORED_NETWORKS][network_id] await network.ready.wait() devices = [] for station in network.stations: dist = location.distance(latitude, longitude, station[ATTR_LATITUDE], station[ATTR_LONGITUDE]) station_id = station[ATTR_ID] station_uid = str(station.get(ATTR_EXTRA, {}).get(ATTR_UID, "")) if radius > dist or stations_list.intersection( (station_id, station_uid)): if name: uid = "_".join([network.network_id, name, station_id]) else: uid = "_".join([network.network_id, station_id]) entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, uid, opp=opp) devices.append(CityBikesStation(network, station_id, entity_id)) async_add_entities(devices, True)
def create_service(call): """Handle a create notification service call.""" title = call.data.get(ATTR_TITLE) message = call.data.get(ATTR_MESSAGE) notification_id = call.data.get(ATTR_NOTIFICATION_ID) if notification_id is not None: entity_id = ENTITY_ID_FORMAT.format(slugify(notification_id)) else: entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, DEFAULT_OBJECT_ID, opp=opp) notification_id = entity_id.split(".")[1] attr = {} if title is not None: if isinstance(title, Template): try: title.opp = opp title = title.async_render(parse_result=False) except TemplateError as ex: _LOGGER.error("Error rendering title %s: %s", title, ex) title = title.template attr[ATTR_TITLE] = title attr[ATTR_FRIENDLY_NAME] = title if isinstance(message, Template): try: message.opp = opp message = message.async_render(parse_result=False) except TemplateError as ex: _LOGGER.error("Error rendering message %s: %s", message, ex) message = message.template attr[ATTR_MESSAGE] = message opp.states.async_set(entity_id, STATE, attr) # Store notification and fire event # This will eventually replace state machine storage persistent_notifications[entity_id] = { ATTR_MESSAGE: message, ATTR_NOTIFICATION_ID: notification_id, ATTR_STATUS: STATUS_UNREAD, ATTR_TITLE: title, ATTR_CREATED_AT: dt_util.utcnow(), } opp.bus.async_fire(EVENT_PERSISTENT_NOTIFICATIONS_UPDATED)
def __init__(self, gw_dev, var, source, device_class, unit, friendly_name_format): """Initialize the OpenTherm Gateway sensor.""" self.entity_id = async_generate_entity_id( ENTITY_ID_FORMAT, f"{var}_{source}_{gw_dev.gw_id}", opp=gw_dev.opp) self._gateway = gw_dev self._var = var self._source = source self._value = None self._device_class = device_class self._unit = unit if TRANSLATE_SOURCE[source] is not None: friendly_name_format = ( f"{friendly_name_format} ({TRANSLATE_SOURCE[source]})") self._friendly_name = friendly_name_format.format(gw_dev.name) self._unsub_updates = None
def __init__( self, opp: OpenPeerPower, object_id: str | None, friendly_name: template.Template | None, device_class: str, value_template: template.Template, icon_template: template.Template | None, entity_picture_template: template.Template | None, availability_template: template.Template | None, delay_on_raw, delay_off_raw, attribute_templates: dict[str, template.Template], unique_id: str | None, ): """Initialize the Template binary sensor.""" super().__init__( attribute_templates=attribute_templates, availability_template=availability_template, icon_template=icon_template, entity_picture_template=entity_picture_template, ) if object_id is not None: self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, object_id, opp=opp) self._name: str | None = None self._friendly_name_template: template.Template | None = friendly_name # Try to render the name as it can influence the entity ID if friendly_name: friendly_name.opp = opp try: self._name = friendly_name.async_render(parse_result=False) except template.TemplateError: pass self._device_class = device_class self._template = value_template self._state = None self._delay_cancel = None self._delay_on = None self._delay_on_raw = delay_on_raw self._delay_off = None self._delay_off_raw = delay_off_raw self._unique_id = unique_id
def __init__( self, opp, name, condition_template, temperature_template, humidity_template, attribution_template, pressure_template, wind_speed_template, wind_bearing_template, ozone_template, visibility_template, forecast_template, unique_id, ): """Initialize the Demo weather.""" super().__init__() self._name = name self._condition_template = condition_template self._temperature_template = temperature_template self._humidity_template = humidity_template self._attribution_template = attribution_template self._pressure_template = pressure_template self._wind_speed_template = wind_speed_template self._wind_bearing_template = wind_bearing_template self._ozone_template = ozone_template self._visibility_template = visibility_template self._forecast_template = forecast_template self._unique_id = unique_id self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, opp=opp) self._condition = None self._temperature = None self._humidity = None self._attribution = None self._pressure = None self._wind_speed = None self._wind_bearing = None self._ozone = None self._visibility = None self._forecast = []
async def async_create_group( opp, name, entity_ids=None, user_defined=True, icon=None, object_id=None, mode=None, order=None, ): """Initialize a group. This method must be run in the event loop. """ if order is None: opp.data.setdefault(GROUP_ORDER, 0) order = opp.data[GROUP_ORDER] # Keep track of the group order without iterating # every state in the state machine every time # we setup a new group opp.data[GROUP_ORDER] += 1 group = Group( opp, name, order=order, icon=icon, user_defined=user_defined, entity_ids=entity_ids, mode=mode, ) group.entity_id = async_generate_entity_id( ENTITY_ID_FORMAT, object_id or name, opp=opp ) # If called before the platform async_setup is called (test cases) component = opp.data.get(DOMAIN) if component is None: component = opp.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, opp) await component.async_add_entities([group]) return group
async def async_create_group( opp, name, entity_ids=None, user_defined=True, visible=True, icon=None, view=False, control=None, object_id=None, mode=None, ): """Initialize a group. This method must be run in the event loop. """ group = Group( opp, name, order=len(opp.states.async_entity_ids(DOMAIN)), visible=visible, icon=icon, view=view, control=control, user_defined=user_defined, entity_ids=entity_ids, mode=mode, ) group.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, object_id or name, opp=opp) # If called before the platform async_setup is called (test cases) component = opp.data.get(DOMAIN) if component is None: component = opp.data[DOMAIN] = EntityComponent( _LOGGER, DOMAIN, opp) await component.async_add_entities([group], True) return group
def __init__( self, opp, device_id, name, state_template, disarm_action, arm_away_action, arm_home_action, arm_night_action, code_arm_required, unique_id, ): """Initialize the panel.""" super().__init__() self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id, opp=opp) self._name = name self._template = state_template self._disarm_script = None self._code_arm_required = code_arm_required domain = __name__.split(".")[-2] if disarm_action is not None: self._disarm_script = Script(opp, disarm_action, name, domain) self._arm_away_script = None if arm_away_action is not None: self._arm_away_script = Script(opp, arm_away_action, name, domain) self._arm_home_script = None if arm_home_action is not None: self._arm_home_script = Script(opp, arm_home_action, name, domain) self._arm_night_script = None if arm_night_action is not None: self._arm_night_script = Script(opp, arm_night_action, name, domain) self._state = None self._unique_id = unique_id
def __init__(self, gw_dev, options): """Initialize the device.""" self._gateway = gw_dev self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, gw_dev.gw_id, opp=gw_dev.opp) self.friendly_name = gw_dev.name self.floor_temp = options.get(CONF_FLOOR_TEMP, DEFAULT_FLOOR_TEMP) self.temp_read_precision = options.get(CONF_READ_PRECISION) self.temp_set_precision = options.get(CONF_SET_PRECISION) self.temporary_ovrd_mode = options.get(CONF_TEMPORARY_OVRD_MODE, True) self._available = False self._current_operation = None self._current_temperature = None self._hvac_mode = HVAC_MODE_HEAT self._new_target_temperature = None self._target_temperature = None self._away_mode_a = None self._away_mode_b = None self._away_state_a = False self._away_state_b = False self._unsub_options = None self._unsub_updates = None
def __init__( self, opp, device_id, friendly_name, state_template, icon_template, entity_picture_template, availability_template, on_action, off_action, level_action, level_template, temperature_action, temperature_template, color_action, color_template, white_value_action, white_value_template, effect_action, effect_list_template, effect_template, max_mireds_template, min_mireds_template, supports_transition_template, unique_id, ): """Initialize the light.""" super().__init__( availability_template=availability_template, icon_template=icon_template, entity_picture_template=entity_picture_template, ) self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id, opp=opp) self._name = friendly_name self._template = state_template domain = __name__.split(".")[-2] self._on_script = Script(opp, on_action, friendly_name, domain) self._off_script = Script(opp, off_action, friendly_name, domain) self._level_script = None if level_action is not None: self._level_script = Script(opp, level_action, friendly_name, domain) self._level_template = level_template self._temperature_script = None if temperature_action is not None: self._temperature_script = Script( opp, temperature_action, friendly_name, domain ) self._temperature_template = temperature_template self._color_script = None if color_action is not None: self._color_script = Script(opp, color_action, friendly_name, domain) self._color_template = color_template self._white_value_script = None if white_value_action is not None: self._white_value_script = Script( opp, white_value_action, friendly_name, domain ) self._white_value_template = white_value_template self._effect_script = None if effect_action is not None: self._effect_script = Script(opp, effect_action, friendly_name, domain) self._effect_list_template = effect_list_template self._effect_template = effect_template self._max_mireds_template = max_mireds_template self._min_mireds_template = min_mireds_template self._supports_transition_template = supports_transition_template self._state = False self._brightness = None self._temperature = None self._color = None self._white_value = None self._effect = None self._effect_list = None self._max_mireds = None self._min_mireds = None self._supports_transition = False self._unique_id = unique_id
def __init__( self, opp, device_id, friendly_name, state_template, battery_level_template, fan_speed_template, availability_template, start_action, pause_action, stop_action, return_to_base_action, clean_spot_action, locate_action, set_fan_speed_action, fan_speed_list, attribute_templates, unique_id, ): """Initialize the vacuum.""" super().__init__( attribute_templates=attribute_templates, availability_template=availability_template, ) self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id, opp=opp) self._name = friendly_name self._template = state_template self._battery_level_template = battery_level_template self._fan_speed_template = fan_speed_template self._supported_features = SUPPORT_START domain = __name__.split(".")[-2] self._start_script = Script(opp, start_action, friendly_name, domain) self._pause_script = None if pause_action: self._pause_script = Script(opp, pause_action, friendly_name, domain) self._supported_features |= SUPPORT_PAUSE self._stop_script = None if stop_action: self._stop_script = Script(opp, stop_action, friendly_name, domain) self._supported_features |= SUPPORT_STOP self._return_to_base_script = None if return_to_base_action: self._return_to_base_script = Script( opp, return_to_base_action, friendly_name, domain ) self._supported_features |= SUPPORT_RETURN_HOME self._clean_spot_script = None if clean_spot_action: self._clean_spot_script = Script( opp, clean_spot_action, friendly_name, domain ) self._supported_features |= SUPPORT_CLEAN_SPOT self._locate_script = None if locate_action: self._locate_script = Script(opp, locate_action, friendly_name, domain) self._supported_features |= SUPPORT_LOCATE self._set_fan_speed_script = None if set_fan_speed_action: self._set_fan_speed_script = Script( opp, set_fan_speed_action, friendly_name, domain ) self._supported_features |= SUPPORT_FAN_SPEED self._state = None self._battery_level = None self._fan_speed = None if self._template: self._supported_features |= SUPPORT_STATE if self._battery_level_template: self._supported_features |= SUPPORT_BATTERY self._unique_id = unique_id # List of valid fan speeds self._fan_speed_list = fan_speed_list
def __init__( self, opp, device_id, friendly_name, device_class, state_template, position_template, tilt_template, icon_template, entity_picture_template, availability_template, open_action, close_action, stop_action, position_action, tilt_action, optimistic, tilt_optimistic, unique_id, ): """Initialize the Template cover.""" super().__init__( availability_template=availability_template, icon_template=icon_template, entity_picture_template=entity_picture_template, ) self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id, opp=opp) self._name = friendly_name self._template = state_template self._position_template = position_template self._tilt_template = tilt_template self._device_class = device_class self._open_script = None domain = __name__.split(".")[-2] if open_action is not None: self._open_script = Script(opp, open_action, friendly_name, domain) self._close_script = None if close_action is not None: self._close_script = Script(opp, close_action, friendly_name, domain) self._stop_script = None if stop_action is not None: self._stop_script = Script(opp, stop_action, friendly_name, domain) self._position_script = None if position_action is not None: self._position_script = Script(opp, position_action, friendly_name, domain) self._tilt_script = None if tilt_action is not None: self._tilt_script = Script(opp, tilt_action, friendly_name, domain) self._optimistic = optimistic or (not state_template and not position_template) self._tilt_optimistic = tilt_optimistic or not tilt_template self._position = None self._is_opening = False self._is_closing = False self._tilt_value = None self._unique_id = unique_id
def __init__( self, opp, device_id, friendly_name, state_template, speed_template, percentage_template, preset_mode_template, oscillating_template, direction_template, availability_template, on_action, off_action, set_speed_action, set_percentage_action, set_preset_mode_action, set_oscillating_action, set_direction_action, speed_count, speed_list, preset_modes, unique_id, ): """Initialize the fan.""" super().__init__(availability_template=availability_template) self.opp = opp self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id, opp=opp) self._name = friendly_name self._template = state_template self._speed_template = speed_template self._percentage_template = percentage_template self._preset_mode_template = preset_mode_template self._oscillating_template = oscillating_template self._direction_template = direction_template self._supported_features = 0 domain = __name__.split(".")[-2] self._on_script = Script(opp, on_action, friendly_name, domain) self._off_script = Script(opp, off_action, friendly_name, domain) self._set_speed_script = None if set_speed_action: self._set_speed_script = Script(opp, set_speed_action, friendly_name, domain) self._set_percentage_script = None if set_percentage_action: self._set_percentage_script = Script(opp, set_percentage_action, friendly_name, domain) self._set_preset_mode_script = None if set_preset_mode_action: self._set_preset_mode_script = Script(opp, set_preset_mode_action, friendly_name, domain) self._set_oscillating_script = None if set_oscillating_action: self._set_oscillating_script = Script(opp, set_oscillating_action, friendly_name, domain) self._set_direction_script = None if set_direction_action: self._set_direction_script = Script(opp, set_direction_action, friendly_name, domain) self._state = STATE_OFF self._speed = None self._percentage = None self._preset_mode = None self._oscillating = None self._direction = None if (self._speed_template or self._percentage_template or self._preset_mode_template): self._supported_features |= SUPPORT_SET_SPEED if self._oscillating_template: self._supported_features |= SUPPORT_OSCILLATE if self._direction_template: self._supported_features |= SUPPORT_DIRECTION self._unique_id = unique_id # Number of valid speeds self._speed_count = speed_count # List of valid speeds self._speed_list = speed_list # List of valid preset modes self._preset_modes = preset_modes