Skip to content

Commit 5d87e0f

Browse files
Make Google sheets datetime column optional (home-assistant#155861)
Co-authored-by: Abílio Costa <[email protected]>
1 parent acb087f commit 5d87e0f

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

homeassistant/components/google_sheets/services.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
if TYPE_CHECKING:
3232
from . import GoogleSheetsConfigEntry
3333

34+
ADD_CREATED_COLUMN = "add_created_column"
3435
DATA = "data"
3536
DATA_CONFIG_ENTRY = "config_entry"
3637
ROWS = "rows"
@@ -43,6 +44,7 @@
4344
{
4445
vol.Required(DATA_CONFIG_ENTRY): ConfigEntrySelector({"integration": DOMAIN}),
4546
vol.Optional(WORKSHEET): cv.string,
47+
vol.Optional(ADD_CREATED_COLUMN, default=True): cv.boolean,
4648
vol.Required(DATA): vol.Any(cv.ensure_list, [dict]),
4749
},
4850
)
@@ -69,10 +71,11 @@ def _append_to_sheet(call: ServiceCall, entry: GoogleSheetsConfigEntry) -> None:
6971

7072
worksheet = sheet.worksheet(call.data.get(WORKSHEET, sheet.sheet1.title))
7173
columns: list[str] = next(iter(worksheet.get_values("A1:ZZ1")), [])
74+
add_created_column = call.data[ADD_CREATED_COLUMN]
7275
now = str(datetime.now())
7376
rows = []
7477
for d in call.data[DATA]:
75-
row_data = {"created": now} | d
78+
row_data = ({"created": now} | d) if add_created_column else d
7679
row = [row_data.get(column, "") for column in columns]
7780
for key, value in row_data.items():
7881
if key not in columns:

homeassistant/components/google_sheets/services.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ append_sheet:
99
example: "Sheet1"
1010
selector:
1111
text:
12+
add_created_column:
13+
required: false
14+
default: true
15+
selector:
16+
boolean:
1217
data:
1318
required: true
1419
example: '{"hello": world, "cool": True, "count": 5}'

homeassistant/components/google_sheets/strings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
"append_sheet": {
4646
"description": "Appends data to a worksheet in Google Sheets.",
4747
"fields": {
48+
"add_created_column": {
49+
"description": "Add a \"created\" column with the current date-time to the appended data.",
50+
"name": "Add created column"
51+
},
4852
"config_entry": {
4953
"description": "The sheet to add data to.",
5054
"name": "Sheet"

tests/components/google_sheets/test_init.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Any
77
from unittest.mock import patch
88

9+
from freezegun import freeze_time
910
from gspread.exceptions import APIError
1011
import pytest
1112
from requests.models import Response
@@ -17,8 +18,11 @@
1718
)
1819
from homeassistant.components.google_sheets.const import DOMAIN
1920
from homeassistant.components.google_sheets.services import (
21+
ADD_CREATED_COLUMN,
22+
DATA,
2023
DATA_CONFIG_ENTRY,
2124
ROWS,
25+
SERVICE_APPEND_SHEET,
2226
SERVICE_GET_SHEET,
2327
WORKSHEET,
2428
)
@@ -194,30 +198,51 @@ async def test_expired_token_refresh_failure(
194198
assert entries[0].state is expected_state
195199

196200

201+
@pytest.mark.parametrize(
202+
("add_created_column_param", "expected_row"),
203+
[
204+
({ADD_CREATED_COLUMN: True}, ["bar", "2024-01-15 12:30:45.123456"]),
205+
({ADD_CREATED_COLUMN: False}, ["bar", ""]),
206+
({}, ["bar", "2024-01-15 12:30:45.123456"]),
207+
],
208+
ids=["created_column_true", "created_column_false", "created_column_default"],
209+
)
210+
@freeze_time("2024-01-15 12:30:45.123456")
197211
async def test_append_sheet(
198212
hass: HomeAssistant,
199213
setup_integration: ComponentSetup,
200214
config_entry: MockConfigEntry,
215+
add_created_column_param: dict[str, bool],
216+
expected_row: list[str],
201217
) -> None:
202-
"""Test service call appending to a sheet."""
218+
"""Test created column behavior based on add_created_column parameter."""
203219
await setup_integration()
204220

205221
entries = hass.config_entries.async_entries(DOMAIN)
206222
assert len(entries) == 1
207223
assert entries[0].state is ConfigEntryState.LOADED
208224

209225
with patch("homeassistant.components.google_sheets.services.Client") as mock_client:
226+
mock_worksheet = (
227+
mock_client.return_value.open_by_key.return_value.worksheet.return_value
228+
)
229+
mock_worksheet.get_values.return_value = [["foo", "created"]]
230+
210231
await hass.services.async_call(
211232
DOMAIN,
212-
"append_sheet",
233+
SERVICE_APPEND_SHEET,
213234
{
214-
"config_entry": config_entry.entry_id,
215-
"worksheet": "Sheet1",
216-
"data": {"foo": "bar"},
235+
DATA_CONFIG_ENTRY: config_entry.entry_id,
236+
WORKSHEET: "Sheet1",
237+
DATA: {"foo": "bar"},
238+
**add_created_column_param,
217239
},
218240
blocking=True,
219241
)
220-
assert len(mock_client.mock_calls) == 8
242+
243+
mock_worksheet.append_rows.assert_called_once()
244+
rows_data = mock_worksheet.append_rows.call_args[0][0]
245+
assert rows_data[0] == expected_row
221246

222247

223248
async def test_get_sheet(

0 commit comments

Comments
 (0)