|
5 | 5 | import logging |
6 | 6 | import os |
7 | 7 |
|
8 | | -# from file_read_backwards import FileReadBackwards |
| 8 | +from file_read_backwards import FileReadBackwards |
9 | 9 | import voluptuous as vol |
10 | 10 |
|
11 | 11 | from homeassistant.components.sensor import ( |
@@ -98,50 +98,21 @@ def __init__( |
98 | 98 | def update(self) -> None: |
99 | 99 | """Get the latest entry from a file and updates the state.""" |
100 | 100 | try: |
101 | | - # with FileReadBackwards(self._file_path, encoding="utf-8") as file_data: |
102 | | - # for line in file_data: |
103 | | - # data = line |
104 | | - # break |
105 | | - # data = data.strip() |
106 | | - with open(self._file_path, encoding="utf-8") as f: |
107 | | - data = f.read() |
108 | | - except (IndexError, FileNotFoundError, IsADirectoryError, UnboundLocalError) as e: |
109 | | - _LOGGER.warning( |
110 | | - "File or data not present at the moment: %s", |
111 | | - os.path.basename(self._file_path), |
112 | | - ) |
113 | | - return |
114 | | - |
115 | | - self._attr_native_value = "Ok" |
116 | | - |
117 | | - # if self._val_tpl is not None: |
118 | | - # self._attr_native_value = ( |
119 | | - # self._val_tpl.async_render_with_possible_json_value(data, None) |
120 | | - # ) |
121 | | - # else: |
122 | | - # self._attr_native_value = data |
123 | | - |
124 | | - @property |
125 | | - def extra_state_attributes(self): |
126 | | - """Return device state attributes.""" |
127 | | - |
128 | | - try: |
129 | | - with open(self._file_path, encoding="utf-8") as f: |
130 | | - data = f.read() |
131 | | - except (IndexError, FileNotFoundError, IsADirectoryError, UnboundLocalError) as e: |
| 101 | + with FileReadBackwards(self._file_path, encoding="utf-8") as file_data: |
| 102 | + for line in file_data: |
| 103 | + data = line |
| 104 | + break |
| 105 | + data = data.strip() |
| 106 | + except (IndexError, FileNotFoundError, IsADirectoryError, UnboundLocalError): |
132 | 107 | _LOGGER.warning( |
133 | 108 | "File or data not present at the moment: %s", |
134 | 109 | os.path.basename(self._file_path), |
135 | 110 | ) |
136 | 111 | return |
137 | 112 |
|
138 | 113 | if self._val_tpl is not None: |
139 | | - content = ( |
| 114 | + self._attr_native_value = ( |
140 | 115 | self._val_tpl.async_render_with_possible_json_value(data, None) |
141 | 116 | ) |
142 | 117 | else: |
143 | | - content = data |
144 | | - |
145 | | - return { |
146 | | - "content": content, |
147 | | - } |
| 118 | + self._attr_native_value = data |
0 commit comments