Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for sensor value(s) stored in local files."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import os
7 
8 from file_read_backwards import FileReadBackwards
9 
10 from homeassistant.components.sensor import SensorEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import (
13  CONF_FILE_PATH,
14  CONF_NAME,
15  CONF_UNIT_OF_MEASUREMENT,
16  CONF_VALUE_TEMPLATE,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.template import Template
21 
22 from .const import DEFAULT_NAME, FILE_ICON
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
28  hass: HomeAssistant,
29  entry: ConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Set up the file sensor."""
33  config = dict(entry.data)
34  options = dict(entry.options)
35  file_path: str = config[CONF_FILE_PATH]
36  unique_id: str = entry.entry_id
37  name: str = config.get(CONF_NAME, DEFAULT_NAME)
38  unit: str | None = options.get(CONF_UNIT_OF_MEASUREMENT)
39  value_template: Template | None = None
40 
41  if CONF_VALUE_TEMPLATE in options:
42  value_template = Template(options[CONF_VALUE_TEMPLATE], hass)
43 
45  [FileSensor(unique_id, name, file_path, unit, value_template)], True
46  )
47 
48 
50  """Implementation of a file sensor."""
51 
52  _attr_icon = FILE_ICON
53 
54  def __init__(
55  self,
56  unique_id: str,
57  name: str,
58  file_path: str,
59  unit_of_measurement: str | None,
60  value_template: Template | None,
61  ) -> None:
62  """Initialize the file sensor."""
63  self._attr_name_attr_name = name
64  self._file_path_file_path = file_path
65  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = unit_of_measurement
66  self._val_tpl_val_tpl = value_template
67  self._attr_unique_id_attr_unique_id = unique_id
68 
69  def update(self) -> None:
70  """Get the latest entry from a file and updates the state."""
71  try:
72  with FileReadBackwards(self._file_path_file_path, encoding="utf-8") as file_data:
73  for line in file_data:
74  data = line
75  break
76  data = data.strip()
77  except (IndexError, FileNotFoundError, IsADirectoryError, UnboundLocalError):
78  _LOGGER.warning(
79  "File or data not present at the moment: %s",
80  os.path.basename(self._file_path_file_path),
81  )
82  return
83 
84  if self._val_tpl_val_tpl is not None:
85  self._attr_native_value_attr_native_value = (
86  self._val_tpl_val_tpl.async_render_with_possible_json_value(data, None)
87  )
88  else:
89  self._attr_native_value_attr_native_value = data
None __init__(self, str unique_id, str name, str file_path, str|None unit_of_measurement, Template|None value_template)
Definition: sensor.py:61
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:31