mirror of
https://github.com/home-assistant/core.git
synced 2025-12-20 02:48:57 +00:00
Make Google sheets datetime column optional (#155861)
Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
This commit is contained in:
@@ -31,6 +31,7 @@ from .const import DOMAIN
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from . import GoogleSheetsConfigEntry
|
from . import GoogleSheetsConfigEntry
|
||||||
|
|
||||||
|
ADD_CREATED_COLUMN = "add_created_column"
|
||||||
DATA = "data"
|
DATA = "data"
|
||||||
DATA_CONFIG_ENTRY = "config_entry"
|
DATA_CONFIG_ENTRY = "config_entry"
|
||||||
ROWS = "rows"
|
ROWS = "rows"
|
||||||
@@ -43,6 +44,7 @@ SHEET_SERVICE_SCHEMA = vol.All(
|
|||||||
{
|
{
|
||||||
vol.Required(DATA_CONFIG_ENTRY): ConfigEntrySelector({"integration": DOMAIN}),
|
vol.Required(DATA_CONFIG_ENTRY): ConfigEntrySelector({"integration": DOMAIN}),
|
||||||
vol.Optional(WORKSHEET): cv.string,
|
vol.Optional(WORKSHEET): cv.string,
|
||||||
|
vol.Optional(ADD_CREATED_COLUMN, default=True): cv.boolean,
|
||||||
vol.Required(DATA): vol.Any(cv.ensure_list, [dict]),
|
vol.Required(DATA): vol.Any(cv.ensure_list, [dict]),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -69,10 +71,11 @@ def _append_to_sheet(call: ServiceCall, entry: GoogleSheetsConfigEntry) -> None:
|
|||||||
|
|
||||||
worksheet = sheet.worksheet(call.data.get(WORKSHEET, sheet.sheet1.title))
|
worksheet = sheet.worksheet(call.data.get(WORKSHEET, sheet.sheet1.title))
|
||||||
columns: list[str] = next(iter(worksheet.get_values("A1:ZZ1")), [])
|
columns: list[str] = next(iter(worksheet.get_values("A1:ZZ1")), [])
|
||||||
|
add_created_column = call.data[ADD_CREATED_COLUMN]
|
||||||
now = str(datetime.now())
|
now = str(datetime.now())
|
||||||
rows = []
|
rows = []
|
||||||
for d in call.data[DATA]:
|
for d in call.data[DATA]:
|
||||||
row_data = {"created": now} | d
|
row_data = ({"created": now} | d) if add_created_column else d
|
||||||
row = [row_data.get(column, "") for column in columns]
|
row = [row_data.get(column, "") for column in columns]
|
||||||
for key, value in row_data.items():
|
for key, value in row_data.items():
|
||||||
if key not in columns:
|
if key not in columns:
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ append_sheet:
|
|||||||
example: "Sheet1"
|
example: "Sheet1"
|
||||||
selector:
|
selector:
|
||||||
text:
|
text:
|
||||||
|
add_created_column:
|
||||||
|
required: false
|
||||||
|
default: true
|
||||||
|
selector:
|
||||||
|
boolean:
|
||||||
data:
|
data:
|
||||||
required: true
|
required: true
|
||||||
example: '{"hello": world, "cool": True, "count": 5}'
|
example: '{"hello": world, "cool": True, "count": 5}'
|
||||||
|
|||||||
@@ -45,6 +45,10 @@
|
|||||||
"append_sheet": {
|
"append_sheet": {
|
||||||
"description": "Appends data to a worksheet in Google Sheets.",
|
"description": "Appends data to a worksheet in Google Sheets.",
|
||||||
"fields": {
|
"fields": {
|
||||||
|
"add_created_column": {
|
||||||
|
"description": "Add a \"created\" column with the current date-time to the appended data.",
|
||||||
|
"name": "Add created column"
|
||||||
|
},
|
||||||
"config_entry": {
|
"config_entry": {
|
||||||
"description": "The sheet to add data to.",
|
"description": "The sheet to add data to.",
|
||||||
"name": "Sheet"
|
"name": "Sheet"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import time
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
from gspread.exceptions import APIError
|
from gspread.exceptions import APIError
|
||||||
import pytest
|
import pytest
|
||||||
from requests.models import Response
|
from requests.models import Response
|
||||||
@@ -17,8 +18,11 @@ from homeassistant.components.application_credentials import (
|
|||||||
)
|
)
|
||||||
from homeassistant.components.google_sheets.const import DOMAIN
|
from homeassistant.components.google_sheets.const import DOMAIN
|
||||||
from homeassistant.components.google_sheets.services import (
|
from homeassistant.components.google_sheets.services import (
|
||||||
|
ADD_CREATED_COLUMN,
|
||||||
|
DATA,
|
||||||
DATA_CONFIG_ENTRY,
|
DATA_CONFIG_ENTRY,
|
||||||
ROWS,
|
ROWS,
|
||||||
|
SERVICE_APPEND_SHEET,
|
||||||
SERVICE_GET_SHEET,
|
SERVICE_GET_SHEET,
|
||||||
WORKSHEET,
|
WORKSHEET,
|
||||||
)
|
)
|
||||||
@@ -194,12 +198,24 @@ async def test_expired_token_refresh_failure(
|
|||||||
assert entries[0].state is expected_state
|
assert entries[0].state is expected_state
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("add_created_column_param", "expected_row"),
|
||||||
|
[
|
||||||
|
({ADD_CREATED_COLUMN: True}, ["bar", "2024-01-15 12:30:45.123456"]),
|
||||||
|
({ADD_CREATED_COLUMN: False}, ["bar", ""]),
|
||||||
|
({}, ["bar", "2024-01-15 12:30:45.123456"]),
|
||||||
|
],
|
||||||
|
ids=["created_column_true", "created_column_false", "created_column_default"],
|
||||||
|
)
|
||||||
|
@freeze_time("2024-01-15 12:30:45.123456")
|
||||||
async def test_append_sheet(
|
async def test_append_sheet(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
setup_integration: ComponentSetup,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
|
add_created_column_param: dict[str, bool],
|
||||||
|
expected_row: list[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test service call appending to a sheet."""
|
"""Test created column behavior based on add_created_column parameter."""
|
||||||
await setup_integration()
|
await setup_integration()
|
||||||
|
|
||||||
entries = hass.config_entries.async_entries(DOMAIN)
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
@@ -207,17 +223,26 @@ async def test_append_sheet(
|
|||||||
assert entries[0].state is ConfigEntryState.LOADED
|
assert entries[0].state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
with patch("homeassistant.components.google_sheets.services.Client") as mock_client:
|
with patch("homeassistant.components.google_sheets.services.Client") as mock_client:
|
||||||
|
mock_worksheet = (
|
||||||
|
mock_client.return_value.open_by_key.return_value.worksheet.return_value
|
||||||
|
)
|
||||||
|
mock_worksheet.get_values.return_value = [["foo", "created"]]
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
"append_sheet",
|
SERVICE_APPEND_SHEET,
|
||||||
{
|
{
|
||||||
"config_entry": config_entry.entry_id,
|
DATA_CONFIG_ENTRY: config_entry.entry_id,
|
||||||
"worksheet": "Sheet1",
|
WORKSHEET: "Sheet1",
|
||||||
"data": {"foo": "bar"},
|
DATA: {"foo": "bar"},
|
||||||
|
**add_created_column_param,
|
||||||
},
|
},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert len(mock_client.mock_calls) == 8
|
|
||||||
|
mock_worksheet.append_rows.assert_called_once()
|
||||||
|
rows_data = mock_worksheet.append_rows.call_args[0][0]
|
||||||
|
assert rows_data[0] == expected_row
|
||||||
|
|
||||||
|
|
||||||
async def test_get_sheet(
|
async def test_get_sheet(
|
||||||
|
|||||||
Reference in New Issue
Block a user