@@ -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