Fix file sensor reading the whole file to get the last line (#73986)

This commit is contained in:
J. Nick Koston 2022-06-25 21:02:50 -05:00 committed by GitHub
parent ba64d9db64
commit 6ec6f0a835
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 28 deletions

View file

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