From a5044227a8b90fb6f3adb02222f54a323d80f2af Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Sat, 29 Apr 2023 20:20:43 -0400 Subject: [PATCH] Fix Google Mail Sensor key error (#92262) Fix Google Mail key error --- homeassistant/components/google_mail/sensor.py | 6 +++--- .../fixtures/get_vacation_no_dates.json | 6 ++++++ tests/components/google_mail/test_sensor.py | 17 ++++++++++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 tests/components/google_mail/fixtures/get_vacation_no_dates.json diff --git a/homeassistant/components/google_mail/sensor.py b/homeassistant/components/google_mail/sensor.py index c30ea1c0a65..8023b9222a0 100644 --- a/homeassistant/components/google_mail/sensor.py +++ b/homeassistant/components/google_mail/sensor.py @@ -43,10 +43,10 @@ class GoogleMailSensor(GoogleMailEntity, SensorEntity): """Get the vacation data.""" service = await self.auth.get_resource() settings: HttpRequest = service.users().settings().getVacation(userId="me") - data = await self.hass.async_add_executor_job(settings.execute) + data: dict = await self.hass.async_add_executor_job(settings.execute) - if data["enableAutoReply"]: - value = datetime.fromtimestamp(int(data["endTime"]) / 1000, tz=timezone.utc) + if data["enableAutoReply"] and (end := data.get("endTime")): + value = datetime.fromtimestamp(int(end) / 1000, tz=timezone.utc) else: value = None self._attr_native_value = value diff --git a/tests/components/google_mail/fixtures/get_vacation_no_dates.json b/tests/components/google_mail/fixtures/get_vacation_no_dates.json new file mode 100644 index 00000000000..05abae4c705 --- /dev/null +++ b/tests/components/google_mail/fixtures/get_vacation_no_dates.json @@ -0,0 +1,6 @@ +{ + "enableAutoReply": true, + "responseSubject": "Vacation", + "responseBodyPlainText": "I am on vacation.", + "restrictToContacts": false +} diff --git a/tests/components/google_mail/test_sensor.py b/tests/components/google_mail/test_sensor.py index 369557ad3e9..248622d3157 100644 --- a/tests/components/google_mail/test_sensor.py +++ b/tests/components/google_mail/test_sensor.py @@ -4,6 +4,7 @@ from unittest.mock import patch from google.auth.exceptions import RefreshError from httplib2 import Response +import pytest from homeassistant import config_entries from homeassistant.components.google_mail.const import DOMAIN @@ -17,7 +18,17 @@ from .conftest import SENSOR, TOKEN, ComponentSetup from tests.common import async_fire_time_changed, load_fixture -async def test_sensors(hass: HomeAssistant, setup_integration: ComponentSetup) -> None: +@pytest.mark.parametrize( + ("fixture", "result"), + [ + ("get_vacation", "2022-11-18T05:00:00+00:00"), + ("get_vacation_no_dates", STATE_UNKNOWN), + ("get_vacation_off", STATE_UNKNOWN), + ], +) +async def test_sensors( + hass: HomeAssistant, setup_integration: ComponentSetup, fixture: str, result: str +) -> None: """Test we get sensor data.""" await setup_integration() @@ -29,7 +40,7 @@ async def test_sensors(hass: HomeAssistant, setup_integration: ComponentSetup) - "httplib2.Http.request", return_value=( Response({}), - bytes(load_fixture("google_mail/get_vacation_off.json"), encoding="UTF-8"), + bytes(load_fixture(f"google_mail/{fixture}.json"), encoding="UTF-8"), ), ): next_update = dt_util.utcnow() + timedelta(minutes=15) @@ -37,7 +48,7 @@ async def test_sensors(hass: HomeAssistant, setup_integration: ComponentSetup) - await hass.async_block_till_done() state = hass.states.get(SENSOR) - assert state.state == STATE_UNKNOWN + assert state.state == result async def test_sensor_reauth_trigger(