Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions homeassistant/components/tibber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ async def _close(event: Event) -> None:

try:
await tibber_connection.update_info()
if not tibber_connection.name:
raise ConfigEntryNotReady("Could not fetch Tibber data.")

except asyncio.TimeoutError as err:
raise ConfigEntryNotReady from err
except aiohttp.ClientError as err:
_LOGGER.error("Error connecting to Tibber: %s ", err)
return False
except (
asyncio.TimeoutError,
aiohttp.ClientError,
tibber.RetryableHttpException,
) as err:
raise ConfigEntryNotReady("Unable to connect") from err
except tibber.InvalidLogin as exp:
_LOGGER.error("Failed to login. %s", exp)
return False
except tibber.FatalHttpException:
return False

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/tibber/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ async def async_step_user(
await tibber_connection.update_info()
except asyncio.TimeoutError:
errors[CONF_ACCESS_TOKEN] = "timeout"
except aiohttp.ClientError:
errors[CONF_ACCESS_TOKEN] = "cannot_connect"
except tibber.InvalidLogin:
errors[CONF_ACCESS_TOKEN] = "invalid_access_token"
except (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally require 100% coverage on config flows. Could tests be added?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have near to 0 experience on python/unit testing on python and thus i've been strugling with this repo we try to help migrate to the new websocket endpoint. All assistance is welcome! @epenet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look, shall I just create a new PR when done?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please - and add a comment here with the link to the new PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#89088 adds unit tests for the config flow errors

aiohttp.ClientError,
tibber.RetryableHttpException,
tibber.FatalHttpException,
):
errors[CONF_ACCESS_TOKEN] = "cannot_connect"

if errors:
return self.async_show_form(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/tibber/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"loggers": ["tibber"],
"quality_scale": "silver",
"requirements": ["pyTibber==0.26.13"]
"requirements": ["pyTibber==0.27.0"]
}
17 changes: 14 additions & 3 deletions homeassistant/components/tibber/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.util import Throttle, dt as dt_util

Expand Down Expand Up @@ -559,6 +560,8 @@ def get_live_measurement(self) -> Any:
class TibberDataCoordinator(DataUpdateCoordinator[None]):
"""Handle Tibber data and insert statistics."""

config_entry: ConfigEntry

def __init__(self, hass: HomeAssistant, tibber_connection: tibber.Tibber) -> None:
"""Initialize the data handler."""
super().__init__(
Expand All @@ -571,9 +574,17 @@ def __init__(self, hass: HomeAssistant, tibber_connection: tibber.Tibber) -> Non

async def _async_update_data(self) -> None:
"""Update data via API."""
await self._tibber_connection.fetch_consumption_data_active_homes()
await self._tibber_connection.fetch_production_data_active_homes()
await self._insert_statistics()
try:
await self._tibber_connection.fetch_consumption_data_active_homes()
await self._tibber_connection.fetch_production_data_active_homes()
await self._insert_statistics()
except tibber.RetryableHttpException as err:
raise UpdateFailed(f"Error communicating with API ({err.status})") from err
except tibber.FatalHttpException:
# Fatal error. Reload config entry to show correct error.
self.hass.async_create_task(
self.hass.config_entries.async_reload(self.config_entry.entry_id)
)

async def _insert_statistics(self) -> None:
"""Insert Tibber statistics."""
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ pyRFXtrx==0.30.1
pySwitchmate==0.5.1

# homeassistant.components.tibber
pyTibber==0.26.13
pyTibber==0.27.0

# homeassistant.components.dlink
pyW215==0.7.0
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ pyMetno==0.9.0
pyRFXtrx==0.30.1

# homeassistant.components.tibber
pyTibber==0.26.13
pyTibber==0.27.0

# homeassistant.components.dlink
pyW215==0.7.0
Expand Down