mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00
Use runtime data in Poolsense (#117570)
This commit is contained in:
parent
535aa05c65
commit
0335a01fba
@ -341,7 +341,6 @@ homeassistant.components.persistent_notification.*
|
|||||||
homeassistant.components.pi_hole.*
|
homeassistant.components.pi_hole.*
|
||||||
homeassistant.components.ping.*
|
homeassistant.components.ping.*
|
||||||
homeassistant.components.plugwise.*
|
homeassistant.components.plugwise.*
|
||||||
homeassistant.components.poolsense.*
|
|
||||||
homeassistant.components.powerwall.*
|
homeassistant.components.powerwall.*
|
||||||
homeassistant.components.private_ble_device.*
|
homeassistant.components.private_ble_device.*
|
||||||
homeassistant.components.prometheus.*
|
homeassistant.components.prometheus.*
|
||||||
|
@ -9,16 +9,17 @@ from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .coordinator import PoolSenseDataUpdateCoordinator
|
from .coordinator import PoolSenseDataUpdateCoordinator
|
||||||
|
|
||||||
|
PoolSenseConfigEntry = ConfigEntry[PoolSenseDataUpdateCoordinator]
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: PoolSenseConfigEntry) -> bool:
|
||||||
"""Set up PoolSense from a config entry."""
|
"""Set up PoolSense from a config entry."""
|
||||||
|
|
||||||
poolsense = PoolSense(
|
poolsense = PoolSense(
|
||||||
@ -32,21 +33,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
_LOGGER.error("Invalid authentication")
|
_LOGGER.error("Invalid authentication")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
coordinator = PoolSenseDataUpdateCoordinator(hass, entry)
|
coordinator = PoolSenseDataUpdateCoordinator(hass, poolsense)
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
entry.runtime_data = coordinator
|
||||||
hass.data[DOMAIN][entry.entry_id] = coordinator
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: PoolSenseConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -7,12 +7,11 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_EMAIL
|
from homeassistant.const import CONF_EMAIL
|
||||||
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 .const import DOMAIN
|
from . import PoolSenseConfigEntry
|
||||||
from .entity import PoolSenseEntity
|
from .entity import PoolSenseEntity
|
||||||
|
|
||||||
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
||||||
@ -31,11 +30,11 @@ BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: PoolSenseConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Defer sensor setup to the shared sensor module."""
|
"""Defer sensor setup to the shared sensor module."""
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
PoolSenseBinarySensor(coordinator, config_entry.data[CONF_EMAIL], description)
|
PoolSenseBinarySensor(coordinator, config_entry.data[CONF_EMAIL], description)
|
||||||
|
@ -7,10 +7,7 @@ import logging
|
|||||||
from poolsense import PoolSense
|
from poolsense import PoolSense
|
||||||
from poolsense.exceptions import PoolSenseError
|
from poolsense.exceptions import PoolSenseError
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import aiohttp_client
|
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
@ -22,25 +19,16 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
class PoolSenseDataUpdateCoordinator(DataUpdateCoordinator[dict[str, StateType]]):
|
class PoolSenseDataUpdateCoordinator(DataUpdateCoordinator[dict[str, StateType]]):
|
||||||
"""Define an object to hold PoolSense data."""
|
"""Define an object to hold PoolSense data."""
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
|
def __init__(self, hass: HomeAssistant, poolsense: PoolSense) -> None:
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self.poolsense = PoolSense(
|
|
||||||
aiohttp_client.async_get_clientsession(hass),
|
|
||||||
entry.data[CONF_EMAIL],
|
|
||||||
entry.data[CONF_PASSWORD],
|
|
||||||
)
|
|
||||||
self.hass = hass
|
|
||||||
|
|
||||||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=timedelta(hours=1))
|
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=timedelta(hours=1))
|
||||||
|
self.poolsense = poolsense
|
||||||
|
|
||||||
async def _async_update_data(self) -> dict[str, StateType]:
|
async def _async_update_data(self) -> dict[str, StateType]:
|
||||||
"""Update data via library."""
|
"""Update data via library."""
|
||||||
data = {}
|
|
||||||
async with asyncio.timeout(10):
|
async with asyncio.timeout(10):
|
||||||
try:
|
try:
|
||||||
data = await self.poolsense.get_poolsense_data()
|
return await self.poolsense.get_poolsense_data()
|
||||||
except PoolSenseError as error:
|
except PoolSenseError as error:
|
||||||
_LOGGER.error("PoolSense query did not complete")
|
_LOGGER.error("PoolSense query did not complete")
|
||||||
raise UpdateFailed(error) from error
|
raise UpdateFailed(error) from error
|
||||||
|
|
||||||
return data
|
|
||||||
|
@ -7,7 +7,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_EMAIL,
|
CONF_EMAIL,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -18,7 +17,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import PoolSenseConfigEntry
|
||||||
from .entity import PoolSenseEntity
|
from .entity import PoolSenseEntity
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
@ -70,11 +69,11 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: PoolSenseConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Defer sensor setup to the shared sensor module."""
|
"""Defer sensor setup to the shared sensor module."""
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], description)
|
PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], description)
|
||||||
|
10
mypy.ini
10
mypy.ini
@ -3172,16 +3172,6 @@ disallow_untyped_defs = true
|
|||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = true
|
warn_unreachable = true
|
||||||
|
|
||||||
[mypy-homeassistant.components.poolsense.*]
|
|
||||||
check_untyped_defs = true
|
|
||||||
disallow_incomplete_defs = true
|
|
||||||
disallow_subclassing_any = true
|
|
||||||
disallow_untyped_calls = true
|
|
||||||
disallow_untyped_decorators = true
|
|
||||||
disallow_untyped_defs = true
|
|
||||||
warn_return_any = true
|
|
||||||
warn_unreachable = true
|
|
||||||
|
|
||||||
[mypy-homeassistant.components.powerwall.*]
|
[mypy-homeassistant.components.powerwall.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user