From 8cdd5de75ca2007dfa9311f6132427ba19209277 Mon Sep 17 00:00:00 2001 From: functionpointer Date: Tue, 29 Oct 2024 20:15:08 +0100 Subject: [PATCH] Change Tibber get_prices action to return datetimes as str (#123901) --- homeassistant/components/tibber/services.py | 6 +++-- tests/components/tibber/test_services.py | 29 +++++++++------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/tibber/services.py b/homeassistant/components/tibber/services.py index 35facbcd545..87268186285 100644 --- a/homeassistant/components/tibber/services.py +++ b/homeassistant/components/tibber/services.py @@ -52,7 +52,7 @@ async def __get_prices(call: ServiceCall, *, hass: HomeAssistant) -> ServiceResp ] price_data = [ { - "start_time": dt.datetime.fromisoformat(price["startsAt"]), + "start_time": price["startsAt"], "price": price["total"], "level": price["level"], } @@ -61,7 +61,9 @@ async def __get_prices(call: ServiceCall, *, hass: HomeAssistant) -> ServiceResp ] selected_data = [ - price for price in price_data if start <= price["start_time"] < end + price + for price in price_data + if start <= dt.datetime.fromisoformat(price["start_time"]) < end ] tibber_prices[home_nickname] = selected_data diff --git a/tests/components/tibber/test_services.py b/tests/components/tibber/test_services.py index 33dba9a0e8f..49f9e5e451b 100644 --- a/tests/components/tibber/test_services.py +++ b/tests/components/tibber/test_services.py @@ -138,29 +138,24 @@ async def test_get_prices( "prices": { "first_home": [ { - "start_time": dt.datetime.fromisoformat(START_TIME.isoformat()), - # back and forth conversion to deal with HAFakeDatetime vs real datetime being different types + "start_time": START_TIME.isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, { - "start_time": dt.datetime.fromisoformat( - (START_TIME + dt.timedelta(hours=1)).isoformat() - ), + "start_time": (START_TIME + dt.timedelta(hours=1)).isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, ], "second_home": [ { - "start_time": dt.datetime.fromisoformat(START_TIME.isoformat()), + "start_time": START_TIME.isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, { - "start_time": dt.datetime.fromisoformat( - (START_TIME + dt.timedelta(hours=1)).isoformat() - ), + "start_time": (START_TIME + dt.timedelta(hours=1)).isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, @@ -193,24 +188,24 @@ async def test_get_prices_start_tomorrow( "prices": { "first_home": [ { - "start_time": tomorrow, + "start_time": tomorrow.isoformat(), "price": 0.46914, "level": "VERY_EXPENSIVE", }, { - "start_time": (tomorrow + dt.timedelta(hours=1)), + "start_time": (tomorrow + dt.timedelta(hours=1)).isoformat(), "price": 0.46914, "level": "VERY_EXPENSIVE", }, ], "second_home": [ { - "start_time": tomorrow, + "start_time": tomorrow.isoformat(), "price": 0.46914, "level": "VERY_EXPENSIVE", }, { - "start_time": (tomorrow + dt.timedelta(hours=1)), + "start_time": (tomorrow + dt.timedelta(hours=1)).isoformat(), "price": 0.46914, "level": "VERY_EXPENSIVE", }, @@ -252,24 +247,24 @@ async def test_get_prices_with_timezones( "prices": { "first_home": [ { - "start_time": START_TIME, + "start_time": START_TIME.isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, { - "start_time": START_TIME + dt.timedelta(hours=1), + "start_time": (START_TIME + dt.timedelta(hours=1)).isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, ], "second_home": [ { - "start_time": START_TIME, + "start_time": START_TIME.isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", }, { - "start_time": START_TIME + dt.timedelta(hours=1), + "start_time": (START_TIME + dt.timedelta(hours=1)).isoformat(), "price": 0.36914, "level": "VERY_EXPENSIVE", },