Skip to content

Commit 27c62d9

Browse files
authored
Async fix (#19)
* Make extra_state_attributes async * Async notify * Only read on change * Remove print
1 parent 55ba660 commit 27c62d9

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

custom_components/file_plusplus/notify.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import asyncio
56
from pathlib import Path
67
from typing import Any, TextIO
78

@@ -40,8 +41,8 @@ def __init__(self, unique_id: str, config: dict[str, Any]) -> None:
4041
self._attr_name = config.get(CONF_NAME, DEFAULT_NAME)
4142
self._attr_unique_id = unique_id
4243

43-
def send_message(self, message: str, title: str | None = None) -> None:
44-
"""Send a message to a file."""
44+
async def write_file(self, message: str, title: str | None = None) -> None:
45+
"""Async write a message to a file."""
4546
file: TextIO
4647
filepath = self._file_path
4748
try:
@@ -59,3 +60,7 @@ def send_message(self, message: str, title: str | None = None) -> None:
5960
translation_key="write_access_failed",
6061
translation_placeholders={"filename": filepath, "exc": f"{exc!r}"},
6162
) from exc
63+
64+
def send_message(self, message: str, title: str | None = None) -> None:
65+
"""Send a message to a file."""
66+
asyncio.run(self.write_file(message, title))

custom_components/file_plusplus/sensor.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,46 @@ def __init__(
6464
self._val_tpl = value_template
6565
self._attr_unique_id = unique_id
6666

67-
def update(self) -> None:
68-
"""Return entity state."""
67+
self._file_last_update = None
68+
self._file_content = None
69+
70+
async def async_update(self):
71+
"""Fetch new state data for the sensor."""
6972
self._attr_native_value = "Ok"
7073

71-
@property
72-
def extra_state_attributes(self):
73-
"""Return entity state attributes.
74-
75-
Get the latest entry from a file and updates the state.
76-
"""
77-
78-
try:
79-
with Path.open(self._file_path, encoding="utf-8") as f:
80-
data = f.read()
81-
except (
82-
IndexError,
83-
FileNotFoundError,
84-
IsADirectoryError,
85-
UnboundLocalError,
86-
):
87-
_LOGGER.warning(
88-
"File or data not present at the moment: %s",
89-
Path(self._file_path).name,
90-
)
91-
data = ""
74+
file_last_update = Path.stat(self._file_path).st_mtime
75+
if self._file_last_update == file_last_update:
76+
return
77+
self._file_last_update = file_last_update
78+
79+
def get_content():
80+
try:
81+
with Path.open(self._file_path, encoding="utf-8") as f:
82+
return f.read()
83+
except (
84+
IndexError,
85+
FileNotFoundError,
86+
IsADirectoryError,
87+
UnboundLocalError,
88+
):
89+
_LOGGER.warning(
90+
"File or data not present at the moment: %s",
91+
Path(self._file_path).name,
92+
)
93+
return ""
94+
95+
data = await self.hass.async_add_executor_job(get_content)
9296

9397
if data and self._val_tpl is not None:
9498
content = self._val_tpl.async_render_with_possible_json_value(data, None)
9599
else:
96100
content = data
97101

102+
self._file_content = content
103+
104+
@property
105+
def extra_state_attributes(self):
106+
"""Return extra attributes."""
98107
return {
99-
"content": content,
108+
"content": self._file_content,
100109
}

0 commit comments

Comments
 (0)