def is_sensor_continuous(ent_reg: er.EntityRegistry, entity_id: str) -> bool: """Determine if a sensor is continuous by checking its state class. Sensors with a unit_of_measurement are also considered continuous, but are filtered already by the SQL query generated by _get_events """ if not (entry := ent_reg.async_get(entity_id)): # Entity not registered, so can't have a state class return False
async def _get_entity_and_device(self, ent_reg: EntityRegistry, dev_reg: DeviceRegistry) -> \ tuple[RegistryEntry, DeviceEntry] | tuple[None, None]: """Fetch the entity and device entries.""" entity_entry = ent_reg.async_get(self.entity_id) if not entity_entry: return None, None device_entry = dev_reg.devices.get(entity_entry.device_id) return entity_entry, device_entry
def _exclude_by_entity_registry( ent_reg: entity_registry.EntityRegistry, entity_id: str, include_entity_category: bool, include_hidden: bool, ) -> bool: """Filter out hidden entities and ones with entity category (unless specified).""" return bool( (entry := ent_reg.async_get(entity_id)) and ((not include_hidden and entry.hidden_by is not None) or (not include_entity_category and entry.entity_category is not None)))
async def code_slots_changed( ent_reg: EntityRegistry, platform: entity_platform.EntityPlatform, config_entry: ConfigEntry, old_slots: List[int], new_slots: List[int], ): """Handle code slots changed.""" slots_to_add = list(set(new_slots) - set(old_slots)) slots_to_remove = list(set(old_slots) - set(new_slots)) for slot in slots_to_remove: sensor_name = slugify( f"{config_entry.data[CONF_LOCK_NAME]}_code_slot_{slot}" ) entity_id = f"sensor.{sensor_name}" if ent_reg.async_get(entity_id): await platform.async_remove_entity(entity_id) ent_reg.async_remove(entity_id) async_add_entities( [CodesSensor(hass, entry, x) for x in slots_to_add], True, )
async def devices_serialize(self, entity_reg: EntityRegistry, dev_reg: DeviceRegistry): """Serialize entity for a devices response. https://yandex.ru/dev/dialogs/alice/doc/smart-home/reference/get-devices-docpage/ """ state = self.state # When a state is unavailable, the attributes that describe # capabilities will be stripped. For example, a light entity will miss # the min/max mireds. Therefore they will be excluded from a sync. if state.state == STATE_UNAVAILABLE: return None entity_config = self.config.entity_config.get(state.entity_id, {}) name = (entity_config.get(CONF_NAME) or state.name).strip() domain = state.domain device_class = state.attributes.get(ATTR_DEVICE_CLASS) # If an empty string if not name: return None capabilities = self.capabilities() properties = self.properties() # Found no supported capabilities for this entity if not capabilities and not properties: return None device_type = get_yandex_type(domain, device_class) entry = entity_reg.async_get(state.entity_id) device = dev_reg.async_get(getattr(entry, 'device_id', "")) manufacturer = state.entity_id + ' | ' + getattr( device, "manufacturer", "Yandex Smart Home") model = getattr(device, "model", "") device_info = {'manufacturer': manufacturer, 'model': model} device = { 'id': state.entity_id, 'name': name, 'type': device_type, 'capabilities': [], 'properties': [], 'device_info': device_info, } for cpb in capabilities: description = cpb.description() if description not in device['capabilities']: device['capabilities'].append(description) for ppt in properties: description = ppt.description() if description not in device['properties']: device['properties'].append(description) override_type = entity_config.get(CONF_TYPE) if override_type: device['type'] = override_type room = entity_config.get(CONF_ROOM) if room: device['room'] = room return device dev_reg, ent_reg, area_reg = await gather( self.hass.helpers.device_registry.async_get_registry(), self.hass.helpers.entity_registry.async_get_registry(), self.hass.helpers.area_registry.async_get_registry(), ) entity_entry = ent_reg.async_get(state.entity_id) if not (entity_entry and entity_entry.device_id): return device device_entry = dev_reg.devices.get(entity_entry.device_id) if not (device_entry and device_entry.area_id): return device area_entry = area_reg.areas.get(device_entry.area_id) if area_entry and area_entry.name: device['room'] = area_entry.name return device