33from __future__ import annotations
44
55import logging
6- import os
6+ from pathlib import Path
77
88from homeassistant .components .sensor import SensorEntity
99from homeassistant .config_entries import ConfigEntry
1414 CONF_VALUE_TEMPLATE ,
1515)
1616from homeassistant .core import HomeAssistant
17- from homeassistant .helpers .entity_platform import AddEntitiesCallback
17+ from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
1818from homeassistant .helpers .template import Template
1919
2020from .const import DEFAULT_NAME , FILE_ICON
2525async def async_setup_entry (
2626 hass : HomeAssistant ,
2727 entry : ConfigEntry ,
28- async_add_entities : AddEntitiesCallback ,
28+ async_add_entities : AddConfigEntryEntitiesCallback ,
2929) -> None :
3030 """Set up the File++ sensor."""
3131 config = dict (entry .data )
@@ -70,28 +70,31 @@ def update(self) -> None:
7070
7171 @property
7272 def extra_state_attributes (self ):
73- """
74- Return entity state attributes.
73+ """Return entity state attributes.
74+
7575 Get the latest entry from a file and updates the state.
7676 """
7777
7878 try :
79- with open (self ._file_path , encoding = "utf-8" ) as f :
79+ with Path . open (self ._file_path , encoding = "utf-8" ) as f :
8080 data = f .read ()
81- except (IndexError , FileNotFoundError , IsADirectoryError , UnboundLocalError ) as e :
81+ except (
82+ IndexError ,
83+ FileNotFoundError ,
84+ IsADirectoryError ,
85+ UnboundLocalError ,
86+ ):
8287 _LOGGER .warning (
8388 "File or data not present at the moment: %s" ,
84- os . path . basename (self ._file_path ),
89+ Path (self ._file_path ). name ,
8590 )
8691 data = ""
8792
8893 if data and self ._val_tpl is not None :
89- content = (
90- self ._val_tpl .async_render_with_possible_json_value (data , None )
91- )
94+ content = self ._val_tpl .async_render_with_possible_json_value (data , None )
9295 else :
9396 content = data
9497
9598 return {
9699 "content" : content ,
97- }
100+ }
0 commit comments