mirror of
https://github.com/home-assistant/core.git
synced 2025-04-28 03:07:50 +00:00

* Create a new homeassistan integration for microBees * black --fast homeassistant tests * Switch platform * rename folder * rename folder * Update owners * aiohttp removed in favor of hass * Update config_flow.py * Update __init__.py * Update const.py * Update manifest.json * Update string.json * Update servicesMicrobees.py * Update switch.py * Update __init__.py * Update it.json * Create a new homeassistan integration for microBees * black --fast homeassistant tests * Switch platform * rename folder * rename folder * Update owners * aiohttp removed in favor of hass * Update config_flow.py * Update __init__.py * Update const.py * Update manifest.json * Update string.json * Update servicesMicrobees.py * Update switch.py * Update __init__.py * Update it.json * fixes review * fixes review * fixes review * pyproject.toml * Update package_constraints.txt * fixes review * bug fixes * bug fixes * delete microbees connector * add other productID in switch * added coordinator and enanchments * added coordinator and enanchments * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * fixes from suggestions * add test * add test * add test * add test * requested commit * requested commit * requested commit * requested commit * reverting .strict-typing and added microbees to .coveragerc * remove log * remove log * remove log * remove log * add test for microbeesExeption and Exeption * add test for microbeesExeption and Exeption * add test for microbeesException and Exception * add test for microbeesException and Exception * add test for microbeesException and Exception --------- Co-authored-by: FedDam <noceracity@gmail.com> Co-authored-by: Federico D'Amico <48856240+FedDam@users.noreply.github.com>
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
"""The microBees Coordinator."""
|
|
|
|
import asyncio
|
|
from dataclasses import dataclass
|
|
from datetime import timedelta
|
|
from http import HTTPStatus
|
|
import logging
|
|
|
|
import aiohttp
|
|
from microBeesPy.microbees import Actuator, Bee, MicroBees, MicroBeesException
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class MicroBeesCoordinatorData:
|
|
"""Microbees data from the Coordinator."""
|
|
|
|
bees: dict[int, Bee]
|
|
actuators: dict[int, Actuator]
|
|
|
|
|
|
class MicroBeesUpdateCoordinator(DataUpdateCoordinator[MicroBeesCoordinatorData]):
|
|
"""MicroBees coordinator."""
|
|
|
|
def __init__(self, hass: HomeAssistant, microbees: MicroBees) -> None:
|
|
"""Initialize microBees coordinator."""
|
|
super().__init__(
|
|
hass,
|
|
_LOGGER,
|
|
name="microBees Coordinator",
|
|
update_interval=timedelta(seconds=30),
|
|
)
|
|
self.microbees = microbees
|
|
|
|
async def _async_update_data(self) -> MicroBeesCoordinatorData:
|
|
"""Fetch data from API endpoint."""
|
|
async with asyncio.timeout(10):
|
|
try:
|
|
bees = await self.microbees.getBees()
|
|
except aiohttp.ClientResponseError as err:
|
|
if err.status is HTTPStatus.UNAUTHORIZED:
|
|
raise ConfigEntryAuthFailed(
|
|
"Token not valid, trigger renewal"
|
|
) from err
|
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
|
|
|
except MicroBeesException as err:
|
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
|
|
|
bees_dict = {}
|
|
actuators_dict = {}
|
|
for bee in bees:
|
|
bees_dict[bee.id] = bee
|
|
for actuator in bee.actuators:
|
|
actuators_dict[actuator.id] = actuator
|
|
return MicroBeesCoordinatorData(bees=bees_dict, actuators=actuators_dict)
|