rename functions

This commit is contained in:
Julian Pawlowski 2025-04-18 14:12:10 +00:00
parent 8123285489
commit ef38bf75b5
9 changed files with 52 additions and 52 deletions

View file

@ -14,15 +14,15 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.loader import async_get_loaded_integration from homeassistant.loader import async_get_loaded_integration
from .api import IntegrationBlueprintApiClient from .api import TibberPricesApiClient
from .const import DOMAIN, LOGGER from .const import DOMAIN, LOGGER
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import BlueprintDataUpdateCoordinator
from .data import IntegrationBlueprintData from .data import TibberPricesData
if TYPE_CHECKING: if TYPE_CHECKING:
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .data import IntegrationBlueprintConfigEntry from .data import TibberPricesConfigEntry
PLATFORMS: list[Platform] = [ PLATFORMS: list[Platform] = [
Platform.SENSOR, Platform.SENSOR,
@ -34,7 +34,7 @@ PLATFORMS: list[Platform] = [
# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry # https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry, entry: TibberPricesConfigEntry,
) -> bool: ) -> bool:
"""Set up this integration using UI.""" """Set up this integration using UI."""
coordinator = BlueprintDataUpdateCoordinator( coordinator = BlueprintDataUpdateCoordinator(
@ -43,8 +43,8 @@ async def async_setup_entry(
name=DOMAIN, name=DOMAIN,
update_interval=timedelta(hours=1), update_interval=timedelta(hours=1),
) )
entry.runtime_data = IntegrationBlueprintData( entry.runtime_data = TibberPricesData(
client=IntegrationBlueprintApiClient( client=TibberPricesApiClient(
username=entry.data[CONF_USERNAME], username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD], password=entry.data[CONF_PASSWORD],
session=async_get_clientsession(hass), session=async_get_clientsession(hass),
@ -64,7 +64,7 @@ async def async_setup_entry(
async def async_unload_entry( async def async_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry, entry: TibberPricesConfigEntry,
) -> bool: ) -> bool:
"""Handle removal of an entry.""" """Handle removal of an entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
@ -72,7 +72,7 @@ async def async_unload_entry(
async def async_reload_entry( async def async_reload_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry, entry: TibberPricesConfigEntry,
) -> None: ) -> None:
"""Reload config entry.""" """Reload config entry."""
await async_unload_entry(hass, entry) await async_unload_entry(hass, entry)

View file

@ -9,18 +9,18 @@ import aiohttp
import async_timeout import async_timeout
class IntegrationBlueprintApiClientError(Exception): class TibberPricesApiClientError(Exception):
"""Exception to indicate a general API error.""" """Exception to indicate a general API error."""
class IntegrationBlueprintApiClientCommunicationError( class TibberPricesApiClientCommunicationError(
IntegrationBlueprintApiClientError, TibberPricesApiClientError,
): ):
"""Exception to indicate a communication error.""" """Exception to indicate a communication error."""
class IntegrationBlueprintApiClientAuthenticationError( class TibberPricesApiClientAuthenticationError(
IntegrationBlueprintApiClientError, TibberPricesApiClientError,
): ):
"""Exception to indicate an authentication error.""" """Exception to indicate an authentication error."""
@ -29,13 +29,13 @@ def _verify_response_or_raise(response: aiohttp.ClientResponse) -> None:
"""Verify that the response is valid.""" """Verify that the response is valid."""
if response.status in (401, 403): if response.status in (401, 403):
msg = "Invalid credentials" msg = "Invalid credentials"
raise IntegrationBlueprintApiClientAuthenticationError( raise TibberPricesApiClientAuthenticationError(
msg, msg,
) )
response.raise_for_status() response.raise_for_status()
class IntegrationBlueprintApiClient: class TibberPricesApiClient:
"""Sample API Client.""" """Sample API Client."""
def __init__( def __init__(
@ -86,16 +86,16 @@ class IntegrationBlueprintApiClient:
except TimeoutError as exception: except TimeoutError as exception:
msg = f"Timeout error fetching information - {exception}" msg = f"Timeout error fetching information - {exception}"
raise IntegrationBlueprintApiClientCommunicationError( raise TibberPricesApiClientCommunicationError(
msg, msg,
) from exception ) from exception
except (aiohttp.ClientError, socket.gaierror) as exception: except (aiohttp.ClientError, socket.gaierror) as exception:
msg = f"Error fetching information - {exception}" msg = f"Error fetching information - {exception}"
raise IntegrationBlueprintApiClientCommunicationError( raise TibberPricesApiClientCommunicationError(
msg, msg,
) from exception ) from exception
except Exception as exception: # pylint: disable=broad-except except Exception as exception: # pylint: disable=broad-except
msg = f"Something really wrong happened! - {exception}" msg = f"Something really wrong happened! - {exception}"
raise IntegrationBlueprintApiClientError( raise TibberPricesApiClientError(
msg, msg,
) from exception ) from exception

View file

@ -10,14 +10,14 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from .entity import IntegrationBlueprintEntity from .entity import TibberPricesEntity
if TYPE_CHECKING: if TYPE_CHECKING:
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import BlueprintDataUpdateCoordinator
from .data import IntegrationBlueprintConfigEntry from .data import TibberPricesConfigEntry
ENTITY_DESCRIPTIONS = ( ENTITY_DESCRIPTIONS = (
BinarySensorEntityDescription( BinarySensorEntityDescription(
@ -30,12 +30,12 @@ ENTITY_DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, # noqa: ARG001 Unused function argument: `hass` hass: HomeAssistant, # noqa: ARG001 Unused function argument: `hass`
entry: IntegrationBlueprintConfigEntry, entry: TibberPricesConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the binary_sensor platform.""" """Set up the binary_sensor platform."""
async_add_entities( async_add_entities(
IntegrationBlueprintBinarySensor( TibberPricesBinarySensor(
coordinator=entry.runtime_data.coordinator, coordinator=entry.runtime_data.coordinator,
entity_description=entity_description, entity_description=entity_description,
) )
@ -43,7 +43,7 @@ async def async_setup_entry(
) )
class IntegrationBlueprintBinarySensor(IntegrationBlueprintEntity, BinarySensorEntity): class TibberPricesBinarySensor(TibberPricesEntity, BinarySensorEntity):
"""tibber_prices binary_sensor class.""" """tibber_prices binary_sensor class."""
def __init__( def __init__(

View file

@ -10,10 +10,10 @@ from homeassistant.helpers.aiohttp_client import async_create_clientsession
from slugify import slugify from slugify import slugify
from .api import ( from .api import (
IntegrationBlueprintApiClient, TibberPricesApiClient,
IntegrationBlueprintApiClientAuthenticationError, TibberPricesApiClientAuthenticationError,
IntegrationBlueprintApiClientCommunicationError, TibberPricesApiClientCommunicationError,
IntegrationBlueprintApiClientError, TibberPricesApiClientError,
) )
from .const import DOMAIN, LOGGER from .const import DOMAIN, LOGGER
@ -35,13 +35,13 @@ class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
username=user_input[CONF_USERNAME], username=user_input[CONF_USERNAME],
password=user_input[CONF_PASSWORD], password=user_input[CONF_PASSWORD],
) )
except IntegrationBlueprintApiClientAuthenticationError as exception: except TibberPricesApiClientAuthenticationError as exception:
LOGGER.warning(exception) LOGGER.warning(exception)
_errors["base"] = "auth" _errors["base"] = "auth"
except IntegrationBlueprintApiClientCommunicationError as exception: except TibberPricesApiClientCommunicationError as exception:
LOGGER.error(exception) LOGGER.error(exception)
_errors["base"] = "connection" _errors["base"] = "connection"
except IntegrationBlueprintApiClientError as exception: except TibberPricesApiClientError as exception:
LOGGER.exception(exception) LOGGER.exception(exception)
_errors["base"] = "unknown" _errors["base"] = "unknown"
else: else:
@ -81,7 +81,7 @@ class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def _test_credentials(self, username: str, password: str) -> None: async def _test_credentials(self, username: str, password: str) -> None:
"""Validate credentials.""" """Validate credentials."""
client = IntegrationBlueprintApiClient( client = TibberPricesApiClient(
username=username, username=username,
password=password, password=password,
session=async_create_clientsession(self.hass), session=async_create_clientsession(self.hass),

View file

@ -8,25 +8,25 @@ from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .api import ( from .api import (
IntegrationBlueprintApiClientAuthenticationError, TibberPricesApiClientAuthenticationError,
IntegrationBlueprintApiClientError, TibberPricesApiClientError,
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from .data import IntegrationBlueprintConfigEntry from .data import TibberPricesConfigEntry
# https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities # https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities
class BlueprintDataUpdateCoordinator(DataUpdateCoordinator): class BlueprintDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API.""" """Class to manage fetching data from the API."""
config_entry: IntegrationBlueprintConfigEntry config_entry: TibberPricesConfigEntry
async def _async_update_data(self) -> Any: async def _async_update_data(self) -> Any:
"""Update data via library.""" """Update data via library."""
try: try:
return await self.config_entry.runtime_data.client.async_get_data() return await self.config_entry.runtime_data.client.async_get_data()
except IntegrationBlueprintApiClientAuthenticationError as exception: except TibberPricesApiClientAuthenticationError as exception:
raise ConfigEntryAuthFailed(exception) from exception raise ConfigEntryAuthFailed(exception) from exception
except IntegrationBlueprintApiClientError as exception: except TibberPricesApiClientError as exception:
raise UpdateFailed(exception) from exception raise UpdateFailed(exception) from exception

View file

@ -9,17 +9,17 @@ if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.loader import Integration from homeassistant.loader import Integration
from .api import IntegrationBlueprintApiClient from .api import TibberPricesApiClient
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import BlueprintDataUpdateCoordinator
type IntegrationBlueprintConfigEntry = ConfigEntry[IntegrationBlueprintData] type TibberPricesConfigEntry = ConfigEntry[TibberPricesData]
@dataclass @dataclass
class IntegrationBlueprintData: class TibberPricesData:
"""Data for the Blueprint integration.""" """Data for the Blueprint integration."""
client: IntegrationBlueprintApiClient client: TibberPricesApiClient
coordinator: BlueprintDataUpdateCoordinator coordinator: BlueprintDataUpdateCoordinator
integration: Integration integration: Integration

View file

@ -9,7 +9,7 @@ from .const import ATTRIBUTION
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import BlueprintDataUpdateCoordinator
class IntegrationBlueprintEntity(CoordinatorEntity[BlueprintDataUpdateCoordinator]): class TibberPricesEntity(CoordinatorEntity[BlueprintDataUpdateCoordinator]):
"""BlueprintEntity class.""" """BlueprintEntity class."""
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION

View file

@ -6,14 +6,14 @@ from typing import TYPE_CHECKING
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from .entity import IntegrationBlueprintEntity from .entity import TibberPricesEntity
if TYPE_CHECKING: if TYPE_CHECKING:
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import BlueprintDataUpdateCoordinator
from .data import IntegrationBlueprintConfigEntry from .data import TibberPricesConfigEntry
ENTITY_DESCRIPTIONS = ( ENTITY_DESCRIPTIONS = (
SensorEntityDescription( SensorEntityDescription(
@ -26,12 +26,12 @@ ENTITY_DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, # noqa: ARG001 Unused function argument: `hass` hass: HomeAssistant, # noqa: ARG001 Unused function argument: `hass`
entry: IntegrationBlueprintConfigEntry, entry: TibberPricesConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the sensor platform.""" """Set up the sensor platform."""
async_add_entities( async_add_entities(
IntegrationBlueprintSensor( TibberPricesSensor(
coordinator=entry.runtime_data.coordinator, coordinator=entry.runtime_data.coordinator,
entity_description=entity_description, entity_description=entity_description,
) )
@ -39,7 +39,7 @@ async def async_setup_entry(
) )
class IntegrationBlueprintSensor(IntegrationBlueprintEntity, SensorEntity): class TibberPricesSensor(TibberPricesEntity, SensorEntity):
"""tibber_prices Sensor class.""" """tibber_prices Sensor class."""
def __init__( def __init__(

View file

@ -6,14 +6,14 @@ from typing import TYPE_CHECKING, Any
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from .entity import IntegrationBlueprintEntity from .entity import TibberPricesEntity
if TYPE_CHECKING: if TYPE_CHECKING:
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import BlueprintDataUpdateCoordinator from .coordinator import BlueprintDataUpdateCoordinator
from .data import IntegrationBlueprintConfigEntry from .data import TibberPricesConfigEntry
ENTITY_DESCRIPTIONS = ( ENTITY_DESCRIPTIONS = (
SwitchEntityDescription( SwitchEntityDescription(
@ -26,12 +26,12 @@ ENTITY_DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, # noqa: ARG001 Unused function argument: `hass` hass: HomeAssistant, # noqa: ARG001 Unused function argument: `hass`
entry: IntegrationBlueprintConfigEntry, entry: TibberPricesConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the switch platform.""" """Set up the switch platform."""
async_add_entities( async_add_entities(
IntegrationBlueprintSwitch( TibberPricesSwitch(
coordinator=entry.runtime_data.coordinator, coordinator=entry.runtime_data.coordinator,
entity_description=entity_description, entity_description=entity_description,
) )
@ -39,7 +39,7 @@ async def async_setup_entry(
) )
class IntegrationBlueprintSwitch(IntegrationBlueprintEntity, SwitchEntity): class TibberPricesSwitch(TibberPricesEntity, SwitchEntity):
"""tibber_prices switch class.""" """tibber_prices switch class."""
def __init__( def __init__(