Skip to content

Commit

Permalink
Updated readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn3400 committed Jun 4, 2024
1 parent 843db4f commit 574319e
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 355 deletions.
2 changes: 0 additions & 2 deletions custom_components/carousel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from .const import CONF_PLATFORM_TYPE, DOMAIN

# PLATFORMS: list[Platform] = [Platform.SENSOR]


# ------------------------------------------------------------------
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Base classes."""
"""Base carousel class."""

from __future__ import annotations

from abc import abstractmethod
from datetime import datetime, timedelta

import voluptuous as vol
Expand Down Expand Up @@ -39,6 +40,7 @@
from homeassistant.helpers.template import Template
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .base_entity_info import BaseEntityInfo
from .const import (
CONF_LISTEN_TO_TIMER_TRIGGER,
CONF_RESTART_TIMER,
Expand All @@ -61,39 +63,8 @@

# ------------------------------------------------------
# ------------------------------------------------------
class BaseEntityInfo:
"""Base entity info class."""

def __init__(
self,
entity_id: str,
friendly_name: str | None = None,
icon: str | None = None,
unit_of_measurement: str | None = None,
show_x_times: int | None = None,
remove_at_timedelta: timedelta | None = None,
) -> None:
"""Entity info base."""
self.entity_id: str = entity_id
self.friendly_name: str | None = friendly_name
self.icon: str | None = icon
self.unit_of_measurement: str | None = unit_of_measurement
self.state: State = None
self.show_x_times: int = show_x_times
self.remove_at: datetime = None
self.is_visible: bool = True

if remove_at_timedelta is not None:
self.remove_at = datetime.now() + remove_at_timedelta

self.entity_obj: Entity
self.device_class: str


# ------------------------------------------------------
# ------------------------------------------------------
class BaseCarousel(Entity):
"""Carousel base."""
class BaseCarouselEntity(Entity):
"""Base Carousel entity."""

restart_timer: bool = True

Expand All @@ -103,7 +74,7 @@ def __init__(
hass: HomeAssistant,
entry: ConfigEntry,
) -> None:
"""Carousel sensor."""
"""Carousel entity base."""
self.hass: HomeAssistant = hass
self.entry: ConfigEntry = entry

Expand All @@ -129,6 +100,22 @@ def __init__(
)

self.platform: EntityPlatform = entity_platform.async_get_current_platform()
self.register_entity_services()

if self.entry.options.get(CONF_LISTEN_TO_TIMER_TRIGGER, ""):
self.refresh_type = RefreshType.LISTEN_TO_TIMER_TRIGGER

self.timer_trigger = TimerTrigger(
self,
self.entry.options.get(CONF_LISTEN_TO_TIMER_TRIGGER, ""),
self.async_handle_timer_finished,
self.entry.options.get(CONF_RESTART_TIMER, False),
)
self.coordinator.update_interval = None

# ------------------------------------------------------------------
def register_entity_services(self) -> None:
"""Register entity services."""

self.platform.async_register_entity_service(
self.platform.domain + "_add",
Expand Down Expand Up @@ -164,17 +151,6 @@ def __init__(
self.async_remove_entity_dispatcher,
)

if self.entry.options.get(CONF_LISTEN_TO_TIMER_TRIGGER, ""):
self.refresh_type = RefreshType.LISTEN_TO_TIMER_TRIGGER

self.timer_trigger = TimerTrigger(
self,
self.entry.options.get(CONF_LISTEN_TO_TIMER_TRIGGER, ""),
self.async_handle_timer_finished,
self.entry.options.get(CONF_RESTART_TIMER, ""),
)
self.coordinator.update_interval = None

# ------------------------------------------------------------------
async def async_handle_timer_finished(self, error: bool) -> None:
"""Handle timer finished."""
Expand All @@ -184,13 +160,14 @@ async def async_handle_timer_finished(self, error: bool) -> None:
self.coordinator.update_interval = timedelta(
minutes=self.entry.options.get(CONF_ROTATE_EVERY_MINUTES, 1)
)
return

if self.refresh_type == RefreshType.LISTEN_TO_TIMER_TRIGGER:
await self.coordinator.async_refresh()

# ------------------------------------------------------------------
async def async_add_entity_dispatcher(
self, entity: BaseCarousel, service_data: ServiceCall
self, entity: BaseCarouselEntity, service_data: ServiceCall
) -> None:
"""Add entity."""

Expand All @@ -206,7 +183,7 @@ async def async_add_entity(self, service_data: ServiceCall) -> None:

# ------------------------------------------------------------------
async def async_show_entity_dispatcher(
self, entity: BaseCarousel, service_data: ServiceCall
self, entity: BaseCarouselEntity, service_data: ServiceCall
) -> None:
"""Show entity."""

Expand All @@ -227,7 +204,7 @@ async def async_show_entity(self, service_data: ServiceCall) -> None:

# ------------------------------------------------------------------
async def async_show_next_dispatcher(
self, entity: BaseCarousel, service_data: ServiceCall
self, entity: BaseCarouselEntity, service_data: ServiceCall
) -> None:
"""Show next."""

Expand All @@ -241,7 +218,7 @@ async def async_show_next(self, service_data: ServiceCall) -> None:

# ------------------------------------------------------------------
async def async_show_prev_dispatcher(
self, entity: BaseCarousel, service_data: ServiceCall
self, entity: BaseCarouselEntity, service_data: ServiceCall
) -> None:
"""Show prev."""

Expand All @@ -264,7 +241,7 @@ async def async_show_prev(self, service_data: ServiceCall) -> None:

# ------------------------------------------------------------------
async def async_remove_entity_dispatcher(
self, entity: BaseCarousel, service_data: ServiceCall
self, entity: BaseCarouselEntity, service_data: ServiceCall
) -> None:
"""Remove entity."""

Expand Down Expand Up @@ -313,6 +290,12 @@ async def async_remove_expired_entities(self) -> None:
):
del self.entities_list[self.current_entity_pos]

# ------------------------------------------------------------------
@abstractmethod
async def async_refresh(self) -> None:
"""Refresh - Abstract method."""
return

# ------------------------------------------------------------------
async def async_refresh_common(self) -> None:
"""Refresh common."""
Expand Down Expand Up @@ -567,8 +550,6 @@ async def async_get_icon(self, entity_id: str) -> str:
self.hass,
"entity",
integrations=[source_entity.platform],
# "entity_component",
# integrations=["sensor"],
)

if (
Expand Down Expand Up @@ -610,3 +591,64 @@ async def async_get_entity_info(
entity_info.icon = await self.async_get_icon(entity_info.entity_id)

return entity_info

# ------------------------------------------------------
@property
def name(self) -> str:
"""Name.
Returns:
str: Name
"""

if self.current_entity is not None:
return self.current_entity.friendly_name

return self.entry.title

# ------------------------------------------------------
@property
def unique_id(self) -> str:
"""Unique id.
Returns:
str: Unique id
"""
return self.entry.entry_id

# ------------------------------------------------------
@property
def icon(self) -> str:
"""Icon.
Returns:
str: Icon
"""
if self.current_entity is not None:
return self.current_entity.icon

return None

# ------------------------------------------------------
@property
def extra_state_attributes(self) -> dict:
"""Extra state attributes.
Returns:
dict: Extra state attributes
"""

attr: dict = {}

if self.current_entity is not None and self.current_entity.state is not None:
attr = self.current_entity.state.attributes.copy()

attr["carousel entities visible"] = len(
[p for p in self.entities_list if p.is_visible]
)

return attr
37 changes: 37 additions & 0 deletions custom_components/carousel/base_entity_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Base entity info class."""

# ------------------------------------------------------
# ------------------------------------------------------
from datetime import datetime, timedelta

from homeassistant.core import State
from homeassistant.helpers.entity import Entity


class BaseEntityInfo:
"""Base entity info class."""

def __init__(
self,
entity_id: str,
friendly_name: str | None = None,
icon: str | None = None,
unit_of_measurement: str | None = None,
show_x_times: int | None = None,
remove_at_timedelta: timedelta | None = None,
) -> None:
"""Entity info base."""
self.entity_id: str = entity_id
self.friendly_name: str | None = friendly_name
self.icon: str | None = icon
self.unit_of_measurement: str | None = unit_of_measurement
self.state: State = None
self.show_x_times: int = show_x_times
self.remove_at: datetime = None
self.is_visible: bool = True

if remove_at_timedelta is not None:
self.remove_at = datetime.now() + remove_at_timedelta

self.entity_obj: Entity
self.device_class: str
Loading

0 comments on commit 574319e

Please sign in to comment.