Repository: home-assistant/core
File: homeassistant/components/scrape/sensor.py (Python)

Code before:
        value_template: ValueTemplate | None,
        yaml: bool,
    ) -> None:
        """Initialize a web scrape sensor."""
        CoordinatorEntity.__init__(self, coordinator)
        ManualTriggerSensorEntity.__init__(self, hass, trigger_entity_config)
        self._select = select
        self._attr = attr
        self._index = index
        self._value_template = value_template
        self._attr_native_value = None
        if not yaml and (unique_id := trigger_entity_config.get(CONF_UNIQUE_ID)):
            self._sensor_name = None
            self._attr_has_entity_name = True
            self._attr_device_info = DeviceInfo(
                entry_type=DeviceEntryType.SERVICE,
                identifiers={(DOMAIN, unique_id)},
                manufacturer="Scrape",
                name=self._rendered[CONF_NAME],
            )
        else:
            self._sensor_name = self._rendered.get(CONF_NAME)

    @property
    def name(self) -> str | None:
        """Return the name of the sensor."""
        return self._sensor_name

    def _extract_value(self) -> Any:
        """Parse the html extraction in the executor."""
        raw_data = self.coordinator.data
        value: str | list[str] | None
        try:
            if self._attr is not None:
                value = raw_data.select(self._select)[self._index][self._attr]
            else:
                tag = raw_data.select(self._select)[self._index]
                if tag.name in ("style", "script", "template"):
                    value = tag.string
                else:
                    value = tag.text
        except IndexError:
            _LOGGER.warning("Index '%s' not found in %s", self._index, self.entity_id)
            return _SENTINEL
        except KeyError:
            _LOGGER.warning(
                "Attribute '%s' not found in %s", self._attr, self.entity_id
            )
            return _SENTINEL
        _LOGGER.debug("Parsed value: %s", value)
        return value

Review comment (suggestion):
Please add a comment or a second line to the docstring explaining why we're overriding this instead of setting `self._attr_name`

Code after:
        value_template: ValueTemplate | None,
        yaml: bool,
    ) -> None:
        """Initialize a web scrape sensor."""
        CoordinatorEntity.__init__(self, coordinator)
        ManualTriggerSensorEntity.__init__(self, hass, trigger_entity_config)
        self._select = select
        self._attr = attr
        self._index = index
        self._value_template = value_template
        self._attr_native_value = None
        if not yaml and (unique_id := trigger_entity_config.get(CONF_UNIQUE_ID)):
            self._sensor_name = None
            self._attr_has_entity_name = True
            self._attr_device_info = DeviceInfo(
                entry_type=DeviceEntryType.SERVICE,
                identifiers={(DOMAIN, unique_id)},
                manufacturer="Scrape",
                name=self._rendered[CONF_NAME],
            )
        else:
            self._sensor_name = self._rendered.get(CONF_NAME)

    @property
    def name(self) -> str | None:
        """Return the name of the sensor.

        Override needed because TriggerBaseEntity.name always returns the
        rendered name, ignoring _attr_name. When has_entity_name is True,
        we need name to return None to use the device name instead.
        """
        return self._sensor_name

    def _extract_value(self) -> Any:
        """Parse the html extraction in the executor."""
        raw_data = self.coordinator.data
        value: str | list[str] | None
        try:
            if self._attr is not None:
                value = raw_data.select(self._select)[self._index][self._attr]
            else:
                tag = raw_data.select(self._select)[self._index]
                if tag.name in ("style", "script", "template"):
                    value = tag.string
                else:
                    value = tag.text
        except IndexError:
            _LOGGER.warning("Index '%s' not found in %s", self._index, self.entity_id)
            return _SENTINEL
        except KeyError:
            _LOGGER.warning(