Skip to content
Open
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
12 changes: 5 additions & 7 deletions custom_components/gismeteo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
https://github.com/Limych/ha-gismeteo/
"""

import asyncio
import logging
from functools import cached_property

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from aiohttp import ClientConnectorError
from async_timeout import timeout
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
Expand All @@ -33,8 +32,8 @@
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
UpdateFailed,
_DataT,
)
from propcache.api import cached_property

from .api import ApiError, GismeteoApiClient
from .const import (
Expand Down Expand Up @@ -203,12 +202,11 @@ def unique_id(self) -> str | None:
"""Return a unique ID."""
return self._unique_id

async def _async_update_data(self) -> _DataT:
async def _async_update_data(self):
"""Update data via library."""
try:
async with timeout(10):
async with asyncio.timeout(10):
await self.gismeteo.async_update()
except (ApiError, ClientConnectorError) as error:
raise UpdateFailed(error) from error
else:
return self.gismeteo.current_data
return self.gismeteo.current_data
28 changes: 9 additions & 19 deletions custom_components/gismeteo/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
https://github.com/Limych/ha-gismeteo/
"""

import asyncio
import logging
from collections.abc import Mapping
from typing import Any

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from aiohttp import ClientConnectorError, ClientError
from async_timeout import timeout
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand Down Expand Up @@ -75,7 +75,7 @@ async def async_step_user(

if user_input is not None:
try:
async with timeout(10):
async with asyncio.timeout(10):
gismeteo = _get_api_client(self.hass, user_input)
await gismeteo.async_update()
except (TimeoutError, ApiError, ClientConnectorError, ClientError):
Expand Down Expand Up @@ -115,17 +115,12 @@ def _show_config_form(self, config: ConfigType) -> config_entries.ConfigFlowResu
@callback
def async_get_options_flow(config_entry: ConfigEntry) -> config_entries.OptionsFlow:
"""Get component options flow."""
return GismeteoOptionsFlowHandler(config_entry)
return GismeteoOptionsFlowHandler()


class GismeteoOptionsFlowHandler(config_entries.OptionsFlow):
"""Gismeteo config flow options handler."""

def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize HACS options flow."""
self.config_entry = config_entry
self.options = dict(config_entry.options)

async def async_step_init(
self, user_input: ConfigType = None # noqa: ARG002
) -> config_entries.ConfigFlowResult: # pylint: disable=unused-argument
Expand All @@ -140,10 +135,11 @@ async def async_step_user(
) -> config_entries.ConfigFlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
if CONF_FORECAST_DAYS in self.options:
self.options[CONF_FORECAST_DAYS] = None
self.options.update(user_input)
return await self._update_options()
options = dict(self.config_entry.options)
if CONF_FORECAST_DAYS in options:
options[CONF_FORECAST_DAYS] = None
options.update(user_input)
return self.async_create_entry(data=options)

return self.async_show_form(
step_id="user",
Expand All @@ -152,7 +148,7 @@ async def async_step_user(
{
vol.Required(
CONF_SHOW_ON_MAP,
default=self.options.get(CONF_SHOW_ON_MAP, False),
default=self.config_entry.options.get(CONF_SHOW_ON_MAP, False),
): bool,
vol.Required(CONF_ADD_SENSORS, default=False): bool,
vol.Optional(CONF_FORECAST_DAYS): forecast_days_int,
Expand All @@ -161,9 +157,3 @@ async def async_step_user(
self.config_entry.options,
),
)

async def _update_options(self) -> config_entries.ConfigFlowResult:
"""Update config entry options."""
return self.async_create_entry(
title=self.config_entry.data.get(CONF_NAME), data=self.options
)
2 changes: 1 addition & 1 deletion custom_components/gismeteo/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class GismeteoEntity(CoordinatorEntity):
"""Gismeteo entity."""

def __init__(self, coordinator: GismeteoDataUpdateCoordinator, location_name: str):
def __init__(self, coordinator: GismeteoDataUpdateCoordinator, location_name: str) -> None:
"""Class initialization."""
super().__init__(coordinator)
self._location_name = location_name
Expand Down
2 changes: 1 addition & 1 deletion custom_components/gismeteo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(
description: SensorEntityDescription,
location_name: str,
day: int | None = None,
):
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, location_name)

Expand Down