mirror of
https://github.com/home-assistant/core.git
synced 2026-05-04 20:04:35 +02:00
Applying the requested fixes
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TypedDict
|
||||
|
||||
from fluss_api import (
|
||||
FlussApiClient,
|
||||
@@ -16,18 +16,23 @@ from homeassistant.const import CONF_API_KEY, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.BUTTON]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Fluss+ from a config entry."""
|
||||
class FlussConfigEntryData(TypedDict):
|
||||
"""Type definition for Fluss+ config entry data."""
|
||||
|
||||
api_key: str
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry[FlussConfigEntryData]
|
||||
) -> bool:
|
||||
"""Set up Fluss+ from a config entry."""
|
||||
try:
|
||||
api = FlussApiClient(entry.data[CONF_API_KEY])
|
||||
except FlussApiClientAuthenticationError as e:
|
||||
raise ConfigEntryError from e
|
||||
raise ConfigEntryAuthFailed from e
|
||||
except (FlussApiClientCommunicationError, FlussApiClientError) as e:
|
||||
raise ConfigEntryNotReady from e
|
||||
|
||||
|
||||
@@ -1,53 +1,87 @@
|
||||
"""Support for Fluss Devices."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from fluss_api.main import FlussApiClient
|
||||
|
||||
from homeassistant.components.button import ButtonEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
from homeassistant.util import slugify
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
_LOGGER = logging.getLogger(__package__)
|
||||
DEFAULT_NAME = "Fluss +"
|
||||
UPDATE_INTERVAL = 60
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Fluss Devices."""
|
||||
api: FlussApiClient = entry.runtime_data
|
||||
api_key: str = entry.data[CONF_API_KEY]
|
||||
|
||||
api = entry.runtime_data
|
||||
|
||||
devices_data = await api.async_get_devices()
|
||||
devices = devices_data["devices"]
|
||||
coordinator = FlussDataUpdateCoordinator(hass, api, api_key)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
async_add_entities(
|
||||
FlussButton(api, device) for device in devices if isinstance(device, dict)
|
||||
FlussButton(coordinator, device)
|
||||
for device in coordinator.data["devices"]
|
||||
if isinstance(device, dict)
|
||||
)
|
||||
|
||||
|
||||
class FlussButton(ButtonEntity):
|
||||
"""Representation of a Fluss cover device."""
|
||||
class FlussDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Class to manage fetching Fluss device data."""
|
||||
|
||||
def __init__(self, api: FlussApiClient, device: dict) -> None:
|
||||
"""Initialize the cover."""
|
||||
def __init__(self, hass: HomeAssistant, api: FlussApiClient, api_key: str) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
self.api = api
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=f"Fluss+ ({slugify(api_key[:8])})",
|
||||
update_interval=timedelta(seconds=UPDATE_INTERVAL),
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> dict[str, Any]:
|
||||
"""Fetch data from the Fluss API."""
|
||||
try:
|
||||
return await self.api.async_get_devices()
|
||||
except Exception as err:
|
||||
raise UpdateFailed(f"Error fetching Fluss data: {err}") from err
|
||||
|
||||
|
||||
class FlussButton(ButtonEntity):
|
||||
"""Representation of a Fluss button device."""
|
||||
|
||||
def __init__(self, coordinator: FlussDataUpdateCoordinator, device: dict) -> None:
|
||||
"""Initialize the button."""
|
||||
if "deviceId" not in device:
|
||||
raise ValueError("Device missing required 'deviceId' attribute.")
|
||||
|
||||
self.api = api
|
||||
self.coordinator = coordinator
|
||||
self.device = device
|
||||
self._name = device.get("deviceName", "Unknown Device")
|
||||
self._attr_unique_id = f"fluss_{device['deviceId']}"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return name of the cover."""
|
||||
"""Return name of the button."""
|
||||
return self._name
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Handle the button press."""
|
||||
await self.api.async_trigger_device(self.device["deviceId"])
|
||||
await self.coordinator.api.async_trigger_device(self.device["deviceId"])
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if the entity is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
@@ -26,10 +26,17 @@ STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): cv.string})
|
||||
class FlussConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Fluss+."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the initial step."""
|
||||
await self.async_set_unique_id(DOMAIN)
|
||||
|
||||
if self._async_current_entries():
|
||||
return self.async_abort(reason="single_instance_allowed")
|
||||
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
@@ -43,7 +50,9 @@ class FlussConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
_LOGGER.exception("Unexpected exception occurred")
|
||||
errors["base"] = "unknown"
|
||||
if not errors:
|
||||
return self.async_create_entry(title="Fluss Device", data=user_input)
|
||||
return self.async_create_entry(
|
||||
title="My Fluss+ Devices", data=user_input
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
"""Constants for the Fluss+ integration."""
|
||||
|
||||
import logging
|
||||
|
||||
DOMAIN = "fluss"
|
||||
LOGGER = logging.getLogger(DOMAIN)
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/fluss",
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["fluss-api"],
|
||||
"requirements": ["fluss-api==0.1.9.17"]
|
||||
"requirements": ["fluss-api==0.1.9.17"],
|
||||
"single_config_entry": true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user