Skip to content

Commit

Permalink
Creating issue if template rendering fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn3400 committed Jun 1, 2024
1 parent 8a8af5c commit c08ceb6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 32 deletions.
75 changes: 58 additions & 17 deletions custom_components/carousel/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
State,
callback,
)
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import (
config_validation as cv,
entity_platform,
Expand Down Expand Up @@ -52,6 +53,7 @@
SERVICE_SHOW_FOR,
SERVICE_SHOW_X_TIMES,
TRANSLATION_KEY_MISSING_ENTITY,
TRANSLATION_KEY_TEMPLATE_ERROR,
RefreshType,
)
from .timer_trigger import TimerTrigger
Expand Down Expand Up @@ -115,6 +117,9 @@ def __init__(

self.timer_trigger: TimerTrigger

self.last_error_template: str = ""
self.last_error_txt_template: str = ""

self.coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
self.hass,
LOGGER,
Expand Down Expand Up @@ -329,25 +334,28 @@ async def async_refresh_entity():
if len(self.entities_list) > 0 and self.entry.options.get(
CONF_SHOW_IF_TEMPLATE, ""
):
while tmp_res != "True" and any(
item.is_visible for item in self.entities_list
):
# self.async_write_ha_state()
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(),
}

value_template: Template | None = Template(
str(self.entry.options.get(CONF_SHOW_IF_TEMPLATE)), self.hass
)
try:
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))

tmp_res = str(
value_template.async_render_with_possible_json_value(
"", variables=template_values
except (TypeError, TemplateError) as e:
await self.async_create_issue_template(
str(e), TRANSLATION_KEY_TEMPLATE_ERROR
)
)
tmp_res = ""
break

if tmp_res == "True":
self.entities_list[self.current_entity_pos].is_visible = True
Expand All @@ -356,7 +364,9 @@ async def async_refresh_entity():
self.entities_list[self.current_entity_pos].is_visible = False
await async_refresh_entity()

if not any(item.is_visible for item in self.entities_list):
tmp_pos += 1

if tmp_res != "True":
self.current_entity = None
return

Expand Down Expand Up @@ -403,11 +413,12 @@ async def async_refresh_common_first_part(self) -> None:
async def async_refresh_common_last_part(self) -> None:
"""Refresh common last part."""

self.cancel_state_listener = async_track_state_change_event(
self.hass,
self.current_entity.entity_id,
self.sensor_state_listener,
)
if self.current_entity is not None:
self.cancel_state_listener = async_track_state_change_event(
self.hass,
self.current_entity.entity_id,
self.sensor_state_listener,
)

# ------------------------------------------------------
@callback
Expand Down Expand Up @@ -441,6 +452,36 @@ async def async_create_issue_entity(
},
)

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

if (
self.last_error_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={
"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

# ------------------------------------------------------
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
Expand Down
18 changes: 8 additions & 10 deletions custom_components/carousel/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ async def async_refresh(self) -> None:

await self.async_refresh_common()

# 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 self.async_refresh_common_last_part()

# ------------------------------------------------------
@property
def name(self) -> str:
Expand Down Expand Up @@ -156,6 +146,14 @@ def extra_state_attributes(self) -> dict:
if self.current_entity is not None and self.current_entity.state is not None:
attr = self.current_entity.state.attributes.copy()

tmp_count: int = 0

for item in self.entities_list:
if item.is_visible:
tmp_count += 1

attr["entities visible"] = tmp_count

return attr

# ------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions custom_components/carousel/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
TRANSLATION_KEY = DOMAIN
TRANSLATION_KEY_MISSING_ENTITY = "missing_entity"
TRANSLATION_KEY_MISSING__TIMER_ENTITY = "missing_timer_entity"
TRANSLATION_KEY_TEMPLATE_ERROR = "template_error"

CONF_ENTITY_IDS = "entity_ids"
CONF_ROTATE_EVERY_MINUTES = "rotate_every_minutes"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/carousel/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"issue_tracker": "https://github.com/kgn3400/carousel/issues",
"requirements": [],
"ssdp": [],
"version": "1.0.4",
"version": "1.0.5",
"zeroconf": []
}
11 changes: 7 additions & 4 deletions custom_components/carousel/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ def extra_state_attributes(self) -> dict:
if self.current_entity is not None and self.current_entity.state is not None:
attr = self.current_entity.state.attributes.copy()

if any(item.is_visible for item in self.entities_list):
attr["any entities visible"] = True
else:
attr["any entities visible"] = False
tmp_count: int = 0

for item in self.entities_list:
if item.is_visible:
tmp_count += 1

attr["entities visible"] = tmp_count

return attr

Expand Down
4 changes: 4 additions & 0 deletions custom_components/carousel/translations/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
"missing_timer_entity": {
"description": "Det ser ud til, at Timer hjælpefunktionen `{timer_entity}` enten er blevet slettet eller omdøbt som brug i Karrusel hjælpefunktionen `{entity}`.\n\n Venligst ret dette problem.",
"title": "Karrusel hjælper: Timer hjælper slettet eller omdøbt"
},
"template_error": {
"description": "Behandling af vis hvis skabelon `{template}` fejler.\nFejl: `{error_txt}` \n\n Venligst ret dette problem.",
"title": "Karrusel hjælper: Skabelon fejl"
}
},
"services": {
Expand Down
4 changes: 4 additions & 0 deletions custom_components/carousel/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
"missing_timer_entity": {
"description": "It looks like either the Timer helper `{timer_entity}` has been deleted or renamed for use in Carousel helper `{entity}`. \n\n Please fix this problem.",
"title": "Carousel helper: Timer helper deleted or renamed"
},
"template_error": {
"description": "Rendering show if template `{template}` ends in error.\nError: `{error_txt}` \n\n Please fix this problem.",
"title": "Carousel helper: Template error"
}
},
"services": {
Expand Down

0 comments on commit c08ceb6

Please sign in to comment.