Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn3400 committed Jun 10, 2024
1 parent 574319e commit c62052a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 135 deletions.
254 changes: 136 additions & 118 deletions custom_components/carousel/base_carousel_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from abc import abstractmethod
from datetime import datetime, timedelta

import voluptuous as vol
Expand Down Expand Up @@ -151,20 +150,6 @@ def register_entity_services(self) -> None:
self.async_remove_entity_dispatcher,
)

# ------------------------------------------------------------------
async def async_handle_timer_finished(self, error: bool) -> None:
"""Handle timer finished."""

if error:
self.refresh_type = RefreshType.NORMAL
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: BaseCarouselEntity, service_data: ServiceCall
Expand Down Expand Up @@ -228,15 +213,9 @@ async def async_show_prev_dispatcher(
async def async_show_prev(self, service_data: ServiceCall) -> None:
"""Show prev."""

if len(self.entities_list) == 0:
self.current_entity = None
if not self.prev_entity_pos():
return

if self.current_entity_pos <= 1:
self.current_entity_pos = len(self.entities_list) - 1
else:
self.current_entity_pos -= 2

await self.coordinator.async_refresh()

# ------------------------------------------------------------------
Expand All @@ -245,7 +224,7 @@ async def async_remove_entity_dispatcher(
) -> None:
"""Remove entity."""

await entity.async_show_prev(service_data)
await entity.async_remove_entity(service_data)

# ------------------------------------------------------------------
async def async_remove_entity(self, service_data: ServiceCall) -> None:
Expand All @@ -260,6 +239,20 @@ async def async_remove_entity(self, service_data: ServiceCall) -> None:
self.stay_at_current_pos = True
await self.coordinator.async_refresh()

# ------------------------------------------------------------------
async def async_handle_timer_finished(self, error: bool) -> None:
"""Handle timer finished."""

if error:
self.refresh_type = RefreshType.NORMAL
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()

# ------------------------------------------------------------------
def find_entity_pos(self, entity_id: str) -> int:
"""Find entity pos."""
Expand All @@ -270,7 +263,7 @@ def find_entity_pos(self, entity_id: str) -> int:
return -1

# ------------------------------------------------------------------
async def async_remove_expired_entities(self) -> None:
def remove_expired_entities(self) -> None:
"""Remove expired entites."""

if (
Expand All @@ -291,92 +284,49 @@ 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."""

# ------------------------------------------------------------------
async def async_refresh_entity():
await self.async_refresh_common_first_part()

self.current_entity = self.entities_list[
self.current_entity_pos
] = await self.async_get_entity_info(self.current_entity)

self.device_class = self.current_entity.device_class

await async_refresh_entity()

tmp_res: str = ""
def next_entity_pos(self) -> bool:
"""Next entity."""

if len(self.entities_list) > 0 and self.entry.options.get(
CONF_SHOW_IF_TEMPLATE, ""
):
tmp_pos: int = 1

while tmp_res != "True" and tmp_pos <= len(self.entities_list):
tmp_state: State = self.hass.states.get(self.current_entity.entity_id)
template_values: dict = {
"state": tmp_state.state,
"state_attributes": tmp_state.attributes.copy(),
}

try:
value_template: Template | None = Template(
str(self.entry.options.get(CONF_SHOW_IF_TEMPLATE)), self.hass
)
if len(self.entities_list) == 0:
self.current_entity = None
return False

tmp_res = str(value_template.async_render(template_values))
if self.stay_at_current_pos:
self.stay_at_current_pos = False
else:
self.current_entity_pos += 1

except (TypeError, TemplateError) as e:
await self.async_create_issue_template(
str(e), TRANSLATION_KEY_TEMPLATE_ERROR
)
tmp_res = ""
break
if (self.current_entity_pos + 1) > len(self.entities_list):
self.current_entity_pos = 0

if tmp_res == "True":
self.entities_list[self.current_entity_pos].is_visible = True
return True

else:
self.entities_list[self.current_entity_pos].is_visible = False
await async_refresh_entity()
# ------------------------------------------------------------------
def prev_entity_pos(self) -> bool:
"""Prev entity."""

tmp_pos += 1
if len(self.entities_list) == 0:
self.current_entity = None
return False

if tmp_res != "True":
self.current_entity = None
return
if self.current_entity_pos <= 1:
self.current_entity_pos = len(self.entities_list) - 1
else:
self.current_entity_pos -= 2

await self.async_refresh_common_last_part()
return True

# ------------------------------------------------------------------
async def async_refresh_common_first_part(self) -> None:
"""Refresh common first part."""
async def async_get_next_entity(self) -> None:
"""Get next entity."""

if self.cancel_state_listener is not None:
self.cancel_state_listener()
self.cancel_state_listener = None

await self.async_remove_expired_entities()

if len(self.entities_list) == 0:
self.current_entity = None
if not self.next_entity_pos():
return

if self.stay_at_current_pos:
self.stay_at_current_pos = False
else:
self.current_entity_pos += 1

if (self.current_entity_pos + 1) > len(self.entities_list):
self.current_entity_pos = 0

self.current_entity = self.entities_list[self.current_entity_pos]
self.current_entity.state = self.hass.states.get(self.current_entity.entity_id)

Expand All @@ -385,16 +335,91 @@ async def async_refresh_common_first_part(self) -> None:
self.current_entity.entity_id, None
)
else:
await self.async_create_issue_entity(
self.current_entity.entity_id, TRANSLATION_KEY_MISSING_ENTITY
self.create_issue(
TRANSLATION_KEY_MISSING_ENTITY,
{
"entity": self.current_entity.entity_id,
"carousel_helper": self.entity_id,
},
)
self.entities_list.pop(self.current_entity_pos)
await self.async_refresh_common_first_part()
return

# ------------------------------------------------------------------
async def async_refresh_common_last_part(self) -> None:
"""Refresh common last part."""
async def async_find_entity_template_ok(self) -> bool:
"""Find entity template ok."""

tmp_pos: int = 1
tmp_res: str = ""
str_true: str = str(True)
# xx: str = datetime.now().strftime("%c")
# tt: str = await self.hass.async_add_executor_job(
# format_datetime,
# datetime.now(),
# "medium",
# None,
# self.hass.config.language,
# )

while tmp_res != str_true and tmp_pos <= len(self.entities_list):
try:
tmp_state: State = self.hass.states.get(self.current_entity.entity_id)

if tmp_state is not None:
template_values: dict = {
"state": tmp_state.state,
"state_attributes": tmp_state.attributes.copy(),
}
value_template: Template | None = Template(
str(self.entry.options.get(CONF_SHOW_IF_TEMPLATE)), self.hass
)

tmp_res = str(value_template.async_render(template_values))

except (TypeError, TemplateError) as e:
self.create_issue_template(str(e))
return False

if tmp_res == str_true:
self.entities_list[self.current_entity_pos].is_visible = True

else:
self.entities_list[self.current_entity_pos].is_visible = False
await self.async_get_next_entity()

tmp_pos += 1

if tmp_res != str_true:
self.current_entity = None
return False

return True

# ------------------------------------------------------------------
async def async_refresh(self) -> None:
"""Refresh."""
await self.async_refresh_common()

# ------------------------------------------------------------------
async def async_refresh_common(self) -> None:
"""Refresh common."""

self.remove_expired_entities()

await self.async_get_next_entity()

if len(self.entities_list) > 0 and self.entry.options.get(
CONF_SHOW_IF_TEMPLATE, ""
):
if not await self.async_find_entity_template_ok():
return

self.current_entity = self.entities_list[
self.current_entity_pos
] = await self.async_get_entity_info(self.current_entity)

self.device_class = self.current_entity.device_class

if self.current_entity is not None:
self.cancel_state_listener = async_track_state_change_event(
Expand All @@ -416,12 +441,12 @@ async def sensor_state_listener(
await self.coordinator.async_refresh()

# ------------------------------------------------------------------
async def async_create_issue_entity(
def create_issue(
self,
entity_id: str,
translation_key: str,
translation_placeholders: dict,
) -> None:
"""Create issue on entity."""
"""Create issue on."""

ir.async_create_issue(
self.hass,
Expand All @@ -431,17 +456,13 @@ async def async_create_issue_entity(
is_fixable=False,
severity=ir.IssueSeverity.WARNING,
translation_key=translation_key,
translation_placeholders={
"entity": entity_id,
"carousel_helper": self.entity_id,
},
translation_placeholders=translation_placeholders,
)

# ------------------------------------------------------------------
async def async_create_issue_template(
def create_issue_template(
self,
error_txt: str,
translation_key: str,
) -> None:
"""Create issue on template."""

Expand All @@ -450,22 +471,15 @@ async def async_create_issue_template(
!= self.entry.options.get(CONF_SHOW_IF_TEMPLATE, "")
or error_txt != self.last_error_txt_template
):
LOGGER.warning(error_txt)

ir.async_create_issue(
self.hass,
DOMAIN,
DOMAIN_NAME + datetime.now().isoformat(),
issue_domain=DOMAIN,
is_fixable=False,
severity=ir.IssueSeverity.WARNING,
translation_key=translation_key,
translation_placeholders={
self.create_issue(
TRANSLATION_KEY_TEMPLATE_ERROR,
{
"error_txt": error_txt,
"template": self.entry.options.get(CONF_SHOW_IF_TEMPLATE, ""),
"carousel_helper": self.entity_id,
},
)

self.last_error_template = self.entry.options.get(CONF_SHOW_IF_TEMPLATE, "")
self.last_error_txt_template = error_txt

Expand Down Expand Up @@ -500,9 +514,13 @@ async def async_verify_entities_exist(self) -> bool:
state: State | None = self.hass.states.get(entity_info.entity_id)

if state is None:
await self.async_create_issue_entity(
self.create_issue(
entity_info.entity_id,
TRANSLATION_KEY_MISSING_ENTITY,
{
"entity": entity_info.entity_id,
"carousel_helper": self.entity_id,
},
)
del self.entities_list[index]
res = False
Expand Down
6 changes: 0 additions & 6 deletions custom_components/carousel/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ def __init__(

self.translation_key = TRANSLATION_KEY

# ------------------------------------------------------------------
async def async_refresh(self) -> None:
"""Refresh."""

await self.async_refresh_common()

# ------------------------------------------------------
@property
def is_on(self) -> bool:
Expand Down
Loading

0 comments on commit c62052a

Please sign in to comment.