55import logging
66import os
77
8- # from file_read_backwards import FileReadBackwards
98import voluptuous as vol
109
1110from homeassistant .components .sensor import (
@@ -98,157 +97,7 @@ def __init__(
9897 def update (self ) -> None :
9998 """Get the latest entry from a file and updates the state."""
10099 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
114100
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 :
132- _LOGGER .warning (
133- "File or data not present at the moment: %s" ,
134- os .path .basename (self ._file_path ),
135- )
136- return
137-
138- if self ._val_tpl is not None :
139- content = (
140- self ._val_tpl .async_render_with_possible_json_value (data , None )
141- )
142- else :
143- content = data
144-
145- return {
146- "content" : content ,
147- }"""Support for sensor value(s) stored in local files."""
148-
149- from __future__ import annotations
150-
151- import logging
152- import os
153-
154- # from file_read_backwards import FileReadBackwards
155- import voluptuous as vol
156-
157- from homeassistant .components .sensor import (
158- PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA ,
159- SensorEntity ,
160- )
161- from homeassistant .config_entries import ConfigEntry
162- from homeassistant .const import (
163- CONF_FILE_PATH ,
164- CONF_NAME ,
165- CONF_UNIT_OF_MEASUREMENT ,
166- CONF_VALUE_TEMPLATE ,
167- )
168- from homeassistant .core import HomeAssistant
169- from homeassistant .helpers import config_validation as cv
170- from homeassistant .helpers .entity_platform import AddEntitiesCallback
171- from homeassistant .helpers .template import Template
172- from homeassistant .helpers .typing import ConfigType , DiscoveryInfoType
173-
174- from .const import DEFAULT_NAME , FILE_ICON
175-
176- _LOGGER = logging .getLogger (__name__ )
177-
178- PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA .extend (
179- {
180- vol .Required (CONF_FILE_PATH ): cv .isfile ,
181- vol .Optional (CONF_NAME , default = DEFAULT_NAME ): cv .string ,
182- vol .Optional (CONF_VALUE_TEMPLATE ): cv .string ,
183- vol .Optional (CONF_UNIT_OF_MEASUREMENT ): cv .string ,
184- }
185- )
186-
187-
188- async def async_setup_platform (
189- hass : HomeAssistant ,
190- config : ConfigType ,
191- async_add_entities : AddEntitiesCallback ,
192- discovery_info : DiscoveryInfoType | None = None ,
193- ) -> None :
194- """Set up the file sensor from YAML.
195-
196- The YAML platform config is automatically
197- imported to a config entry, this method can be removed
198- when YAML support is removed.
199- """
200-
201-
202- async def async_setup_entry (
203- hass : HomeAssistant ,
204- entry : ConfigEntry ,
205- async_add_entities : AddEntitiesCallback ,
206- ) -> None :
207- """Set up the file sensor."""
208- config = dict (entry .data )
209- options = dict (entry .options )
210- file_path : str = config [CONF_FILE_PATH ]
211- unique_id : str = entry .entry_id
212- name : str = config .get (CONF_NAME , DEFAULT_NAME )
213- unit : str | None = options .get (CONF_UNIT_OF_MEASUREMENT )
214- value_template : Template | None = None
215-
216- if CONF_VALUE_TEMPLATE in options :
217- value_template = Template (options [CONF_VALUE_TEMPLATE ], hass )
218-
219- async_add_entities (
220- [FileSensor (unique_id , name , file_path , unit , value_template )], True
221- )
222-
223-
224- class FileSensor (SensorEntity ):
225- """Implementation of a file sensor."""
226-
227- _attr_icon = FILE_ICON
228-
229- def __init__ (
230- self ,
231- unique_id : str ,
232- name : str ,
233- file_path : str ,
234- unit_of_measurement : str | None ,
235- value_template : Template | None ,
236- ) -> None :
237- """Initialize the file sensor."""
238- self ._attr_name = name
239- self ._file_path = file_path
240- self ._attr_native_unit_of_measurement = unit_of_measurement
241- self ._val_tpl = value_template
242- self ._attr_unique_id = unique_id
243-
244- def update (self ) -> None :
245- """Get the latest entry from a file and updates the state."""
246- try :
247- # with FileReadBackwards(self._file_path, encoding="utf-8") as file_data:
248- # for line in file_data:
249- # data = line
250- # break
251- # data = data.strip()
252101 with open (self ._file_path , encoding = "utf-8" ) as f :
253102 data = f .read ()
254103 except (IndexError , FileNotFoundError , IsADirectoryError , UnboundLocalError ) as e :
@@ -260,13 +109,6 @@ def update(self) -> None:
260109
261110 self ._attr_native_value = "Ok"
262111
263- # if self._val_tpl is not None:
264- # self._attr_native_value = (
265- # self._val_tpl.async_render_with_possible_json_value(data, None)
266- # )
267- # else:
268- # self._attr_native_value = data
269-
270112 @property
271113 def extra_state_attributes (self ):
272114 """Return device state attributes."""
0 commit comments