Repository: home-assistant/core
File: homeassistant/components/local_calendar/calendar.py (Python)

Code before:
class LocalCalendarEntity(CalendarEntity):
    """A calendar entity backed by a local iCalendar file."""

    _attr_has_entity_name = True
    _attr_supported_features = (
        CalendarEntityFeature.CREATE_EVENT
        | CalendarEntityFeature.DELETE_EVENT
        | CalendarEntityFeature.UPDATE_EVENT
    )

    def __init__(
        self,
        store: LocalCalendarStore,
        calendar: Calendar,
        name: str,
        unique_id: str,
        color: RGBColor | None = None,
    ) -> None:
        """Initialize LocalCalendarEntity."""
        self._store = store
        self._calendar = calendar
        self._calendar_lock = asyncio.Lock()
        self._event: CalendarEvent | None = None
        self._attr_name = name
        self._attr_unique_id = unique_id
        self._attr_color: RGBColor | None = color

    @property
    def event(self) -> CalendarEvent | None:
        """Return the next upcoming event."""
        return self._event

    @property
    def color(self) -> RGBColor | None:
        """Return the color of the calendar entity as RGBColor."""
        return self._attr_color

    @color.setter
    def color(self, value: RGBColor | None) -> None:
        self._attr_color = value

    async def async_get_events(
        self, hass: HomeAssistant, start_date: datetime, end_date: datetime
    ) -> list[CalendarEvent]:
        """Get all events in a specific time frame."""

        def events_in_range() -> list[CalendarEvent]:
            events = self._calendar.timeline_tz(start_date.tzinfo).overlapping(
                start_date,
                end_date,
            )

Review comment (suggestion):
I think we can just write as:
```suggestion
        self._attr_color = color
```

Code after:

    def __init__(
        self,
        store: LocalCalendarStore,
        calendar: Calendar,
        name: str,
        unique_id: str,
    ) -> None:
        """Initialize LocalCalendarEntity."""
        self._store = store
        self._calendar = calendar
        self._calendar_lock = asyncio.Lock()
        self._event: CalendarEvent | None = None
        self._attr_name = name
        self._attr_unique_id = unique_id

    @property
    def event(self) -> CalendarEvent | None:
        """Return the next upcoming event."""
        return self._event

    async def async_get_events(
        self, hass: HomeAssistant, start_date: datetime, end_date: datetime
    ) -> list[CalendarEvent]:
        """Get all events in a specific time frame."""

        def events_in_range() -> list[CalendarEvent]:
            events = self._calendar.timeline_tz(start_date.tzinfo).overlapping(
                start_date,
                end_date,
            )
            return [_get_calendar_event(event) for event in events]

        return await self.hass.async_add_executor_job(events_in_range)

    async def async_update(self) -> None:
        """Update entity state with the next upcoming event."""

        def next_event() -> CalendarEvent | None:
            now = dt_util.now()
            events = self._calendar.timeline_tz(now.tzinfo).active_after(now)
            if event := next(events, None):
                return _get_calendar_event(event)
            return None

        self._event = await self.hass.async_add_executor_job(next_event)

    async def _async_store(self) -> None:
        """Persist the calendar to disk."""
        content = IcsCalendarStream.calendar_to_ics(self._calendar)
        await self._store.async_store(content)