Change Tibber get_prices action to return datetimes as str (#123901)

This commit is contained in:
functionpointer 2024-10-29 20:15:08 +01:00 committed by GitHub
parent a95c232f11
commit 8cdd5de75c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 19 deletions

View file

@ -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

View file

@ -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",
},