Fix file sensor reading the whole file to get the last line (#73986)
This commit is contained in:
parent
ba64d9db64
commit
6ec6f0a835
8 changed files with 40 additions and 28 deletions
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||
import logging
|
||||
import os
|
||||
|
||||
from file_read_backwards import FileReadBackwards
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
|
@ -77,9 +78,10 @@ class FileSensor(SensorEntity):
|
|||
def update(self):
|
||||
"""Get the latest entry from a file and updates the state."""
|
||||
try:
|
||||
with open(self._file_path, encoding="utf-8") as file_data:
|
||||
with FileReadBackwards(self._file_path, encoding="utf-8") as file_data:
|
||||
for line in file_data:
|
||||
data = line
|
||||
break
|
||||
data = data.strip()
|
||||
except (IndexError, FileNotFoundError, IsADirectoryError, UnboundLocalError):
|
||||
_LOGGER.warning(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue