Skip to content

Commit f25c735

Browse files
committed
Commit HA Core version of File - Last commit Aug 27, 2024
1 parent 72c4d66 commit f25c735

File tree

6 files changed

+44
-88
lines changed

6 files changed

+44
-88
lines changed

custom_components/file_plusplus/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""The file++ component."""
1+
"""The file component."""
22

33
from copy import deepcopy
44
from typing import Any
@@ -44,7 +44,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
4444
return True
4545
# The use of the legacy notify service was deprecated with HA Core 2024.6.0
4646
# and will be removed with HA Core 2024.12
47-
migrate_notify_issue(hass, DOMAIN, "File (Large)", "2024.12.0")
47+
migrate_notify_issue(hass, DOMAIN, "File", "2024.12.0")
4848
# The YAML config was imported with HA Core 2024.6.0 and will be removed with
4949
# HA Core 2024.12
5050
ir.async_create_issue(
@@ -59,7 +59,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
5959
translation_key="deprecated_yaml",
6060
translation_placeholders={
6161
"domain": DOMAIN,
62-
"integration_title": "File (Large)",
62+
"integration_title": "File",
6363
},
6464
)
6565

custom_components/file_plusplus/config_flow.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
"""Config flow for file++ integration."""
1+
"""Config flow for file integration."""
22

33
from copy import deepcopy
4-
import logging
54
import os
65
from typing import Any
76

@@ -21,9 +20,8 @@
2120
CONF_PLATFORM,
2221
CONF_UNIT_OF_MEASUREMENT,
2322
CONF_VALUE_TEMPLATE,
24-
Platform
23+
Platform,
2524
)
26-
2725
from homeassistant.core import callback
2826
from homeassistant.helpers.selector import (
2927
BooleanSelector,
@@ -35,10 +33,7 @@
3533
TextSelectorType,
3634
)
3735

38-
from .const import (CONF_TIMESTAMP, DEFAULT_NAME, DOMAIN,
39-
CONF_NOTIFY, CONF_SENSOR)
40-
41-
_LOGGER = logging.getLogger(__name__)
36+
from .const import CONF_TIMESTAMP, DEFAULT_NAME, DOMAIN
4237

4338
BOOLEAN_SELECTOR = BooleanSelector(BooleanSelectorConfig())
4439
TEMPLATE_SELECTOR = TemplateSelector(TemplateSelectorConfig())
@@ -73,7 +68,7 @@
7368

7469

7570
class FileConfigFlowHandler(ConfigFlow, domain=DOMAIN):
76-
"""Handle a file++ config flow."""
71+
"""Handle a file config flow."""
7772

7873
VERSION = 2
7974

@@ -116,9 +111,6 @@ async def _async_handle_step(
116111
if key not in (CONF_FILE_PATH, CONF_PLATFORM, CONF_NAME):
117112
data.pop(key)
118113
options[key] = value
119-
_LOGGER.debug("DATA: " + str(data))
120-
_LOGGER.debug("TITLE: " + str(title))
121-
_LOGGER.debug("OPTIONS: " + str(options))
122114
return self.async_create_entry(data=data, title=title, options=options)
123115

124116
return self.async_show_form(
@@ -137,9 +129,7 @@ async def async_step_sensor(
137129
"""Handle file sensor config flow."""
138130
return await self._async_handle_step(Platform.SENSOR.value, user_input)
139131

140-
async def async_step_import(
141-
self, import_data: dict[str, Any]
142-
) -> ConfigFlowResult:
132+
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
143133
"""Import `file`` config from configuration.yaml."""
144134
self._async_abort_entries_match(import_data)
145135
platform = import_data[CONF_PLATFORM]
@@ -162,12 +152,12 @@ async def async_step_import(
162152

163153

164154
class FileOptionsFlowHandler(OptionsFlowWithConfigEntry):
165-
"""Handle File++ options."""
155+
"""Handle File options."""
166156

167157
async def async_step_init(
168158
self, user_input: dict[str, Any] | None = None
169159
) -> ConfigFlowResult:
170-
"""Manage File++ options."""
160+
"""Manage File options."""
171161
if user_input:
172162
return self.async_create_entry(data=user_input)
173163

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
"""Constants for the file++ integration."""
1+
"""Constants for the file integration."""
22

3-
DOMAIN = "file_plusplus"
3+
DOMAIN = "file"
44

55
CONF_TIMESTAMP = "timestamp"
66

7-
DEFAULT_NAME = "file_plusplus"
7+
DEFAULT_NAME = "File"
88
FILE_ICON = "mdi:file"
9-
10-
11-
CONF_NOTIFY = "Set up a notification service"
12-
CONF_SENSOR = "Set up a file based sensor"
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
2-
"domain": "file_plusplus",
3-
"name": "File++",
4-
"codeowners": ["@benjamin-dcs"],
5-
"config_flow": true,
6-
"documentation": "https://github.com/benjamin-dcs/File-plusplus/blob/main/README.md",
7-
"iot_class": "local_polling",
8-
"issue_tracker": "https://github.com/benjamin-dcs/File-plusplus/issues",
9-
"version": "0.1.0"
10-
}
2+
"domain": "file",
3+
"name": "File",
4+
"codeowners": ["@fabaff"],
5+
"config_flow": true,
6+
"documentation": "https://www.home-assistant.io/integrations/file",
7+
"iot_class": "local_polling",
8+
"requirements": ["file-read-backwards==2.0.0"]
9+
}

custom_components/file_plusplus/notify.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Support for file++ notification."""
1+
"""Support for file notification."""
22

33
from __future__ import annotations
44

@@ -57,7 +57,7 @@ async def async_get_service(
5757

5858

5959
class FileNotificationService(BaseNotificationService):
60-
"""Implement the notification service for the File++ service."""
60+
"""Implement the notification service for the File service."""
6161

6262
def __init__(self, file_path: str, add_timestamp: bool) -> None:
6363
"""Initialize the service."""
@@ -81,12 +81,12 @@ def send_message(self, message: str = "", **kwargs: Any) -> None:
8181
filepath = self._file_path
8282
try:
8383
with open(filepath, "a", encoding="utf8") as file:
84-
# if os.stat(filepath).st_size == 0:
85-
# title = (
86-
# f"{kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)} notifications (Log"
87-
# f" started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
88-
# )
89-
# file.write(title)
84+
if os.stat(filepath).st_size == 0:
85+
title = (
86+
f"{kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)} notifications (Log"
87+
f" started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
88+
)
89+
file.write(title)
9090

9191
if self.add_timestamp:
9292
text = f"{dt_util.utcnow().isoformat()} {message}\n"
@@ -131,12 +131,12 @@ def send_message(self, message: str, title: str | None = None) -> None:
131131
filepath = self._file_path
132132
try:
133133
with open(filepath, "a", encoding="utf8") as file:
134-
# if os.stat(filepath).st_size == 0:
135-
# title = (
136-
# f"{title or ATTR_TITLE_DEFAULT} notifications (Log"
137-
# f" started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
138-
# )
139-
# file.write(title)
134+
if os.stat(filepath).st_size == 0:
135+
title = (
136+
f"{title or ATTR_TITLE_DEFAULT} notifications (Log"
137+
f" started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
138+
)
139+
file.write(title)
140140

141141
if self._add_timestamp:
142142
text = f"{dt_util.utcnow().isoformat()} {message}\n"

custom_components/file_plusplus/sensor.py

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import os
77

8-
# from file_read_backwards import FileReadBackwards
8+
from file_read_backwards import FileReadBackwards
99
import voluptuous as vol
1010

1111
from homeassistant.components.sensor import (
@@ -98,50 +98,21 @@ def __init__(
9898
def update(self) -> None:
9999
"""Get the latest entry from a file and updates the state."""
100100
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
114-
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:
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+
except (IndexError, FileNotFoundError, IsADirectoryError, UnboundLocalError):
132107
_LOGGER.warning(
133108
"File or data not present at the moment: %s",
134109
os.path.basename(self._file_path),
135110
)
136111
return
137112

138113
if self._val_tpl is not None:
139-
content = (
114+
self._attr_native_value = (
140115
self._val_tpl.async_render_with_possible_json_value(data, None)
141116
)
142117
else:
143-
content = data
144-
145-
return {
146-
"content": content,
147-
}
118+
self._attr_native_value = data

0 commit comments

Comments
 (0)