Add Home Assistant integration scaffold
This commit is contained in:
@@ -1 +1,75 @@
|
||||
"""Paquet de l'intégration Arkteos."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .client import ArkteosClient
|
||||
from .const import CONF_HOST, CONF_PORT, DOMAIN, PLATFORMS
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
def _get_client(hass: HomeAssistant, entry: ConfigEntry) -> ArkteosClient | None:
|
||||
"""Retourne le client partagé, depuis runtime_data ou le repli structuré."""
|
||||
|
||||
client = getattr(entry, "runtime_data", None)
|
||||
if isinstance(client, ArkteosClient):
|
||||
return client
|
||||
return hass.data.get(DOMAIN, {}).get(entry.entry_id)
|
||||
|
||||
|
||||
def _store_client(hass: HomeAssistant, entry: ConfigEntry, client: ArkteosClient) -> None:
|
||||
"""Stocke le client une fois par config entry."""
|
||||
|
||||
if hasattr(entry, "runtime_data"):
|
||||
entry.runtime_data = client
|
||||
return
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = client
|
||||
|
||||
|
||||
def _remove_client(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Supprime les données d'exécution de la config entry."""
|
||||
|
||||
if hasattr(entry, "runtime_data"):
|
||||
entry.runtime_data = None
|
||||
domain_data = hass.data.get(DOMAIN)
|
||||
if domain_data is not None:
|
||||
domain_data.pop(entry.entry_id, None)
|
||||
if not domain_data:
|
||||
hass.data.pop(DOMAIN, None)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Crée et partage le client Arkteos pour cette config entry."""
|
||||
|
||||
if _get_client(hass, entry) is not None:
|
||||
return True
|
||||
|
||||
client = ArkteosClient(entry.data[CONF_HOST], entry.data[CONF_PORT])
|
||||
_store_client(hass, entry, client)
|
||||
await client.start()
|
||||
try:
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
except Exception:
|
||||
await client.stop()
|
||||
_remove_client(hass, entry)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Décharge les plateformes et arrête le client partagé."""
|
||||
|
||||
client = _get_client(hass, entry)
|
||||
if client is None:
|
||||
return True
|
||||
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if not unload_ok:
|
||||
return False
|
||||
await client.stop()
|
||||
_remove_client(hass, entry)
|
||||
return True
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Binary sensor de disponibilité du proxy Arkteos."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorDeviceClass, BinarySensorEntity
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import _get_client
|
||||
from .const import DOMAIN
|
||||
from .entity import ArkteosEntity
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback) -> None:
|
||||
"""Ajoute l'unique entité de connexion de cette config entry."""
|
||||
|
||||
client = _get_client(hass, entry)
|
||||
if client is None:
|
||||
return
|
||||
async_add_entities([ArkteosConnectionBinarySensor(client)])
|
||||
|
||||
|
||||
class ArkteosConnectionBinarySensor(ArkteosEntity, BinarySensorEntity):
|
||||
"""Expose la disponibilité du flux lu depuis le proxy."""
|
||||
|
||||
_attr_translation_key = "connection"
|
||||
_attr_unique_id = "arkteos_zuran4_connection"
|
||||
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
||||
_attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, "arkteos_zuran4")},
|
||||
manufacturer="Arkteos",
|
||||
model="Zuran 4",
|
||||
name="PAC Arkteos Zuran 4",
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Reflète la disponibilité du client sans effectuer de lecture réseau."""
|
||||
|
||||
return self._client.available
|
||||
@@ -0,0 +1,145 @@
|
||||
"""Flux de configuration par interface utilisateur de l'intégration Arkteos."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
|
||||
from .client import ArkteosClient
|
||||
from .const import CONF_HOST, CONF_PORT, DEFAULT_PORT, DOMAIN
|
||||
|
||||
|
||||
VALIDATION_TIMEOUT = 10.0
|
||||
|
||||
|
||||
class CannotConnect(Exception):
|
||||
"""La connexion au proxy a échoué."""
|
||||
|
||||
|
||||
class ValidationTimeout(Exception):
|
||||
"""Aucune trame valide n'a été reçue dans le délai imparti."""
|
||||
|
||||
|
||||
class ArkteosConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Gère la création et la reconfiguration des config entries Arkteos."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> FlowResult:
|
||||
"""Affiche et traite le formulaire initial."""
|
||||
|
||||
if user_input is not None:
|
||||
data, errors = await self._async_validate_input(user_input)
|
||||
if not errors:
|
||||
if self._entry_exists(data[CONF_HOST], data[CONF_PORT]):
|
||||
return self.async_abort(reason="already_configured")
|
||||
await self.async_set_unique_id(self._unique_id(data))
|
||||
self._abort_if_unique_id_configured()
|
||||
try:
|
||||
await self._async_validate_proxy(data)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except ValidationTimeout:
|
||||
errors["base"] = "timeout"
|
||||
except Exception:
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
return self.async_create_entry(title=self._title(data), data=data)
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=self._schema(data if "data" in locals() else None), errors=errors
|
||||
)
|
||||
|
||||
return self.async_show_form(step_id="user", data_schema=self._schema())
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Permet la modification de l'hôte et du port d'une entrée existante."""
|
||||
|
||||
entry = self._get_reconfigure_entry()
|
||||
if user_input is not None:
|
||||
data, errors = await self._async_validate_input(user_input)
|
||||
if not errors:
|
||||
if self._entry_exists(data[CONF_HOST], data[CONF_PORT], exclude_entry_id=entry.entry_id):
|
||||
errors["base"] = "already_configured"
|
||||
else:
|
||||
try:
|
||||
await self._async_validate_proxy(data)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except ValidationTimeout:
|
||||
errors["base"] = "timeout"
|
||||
except Exception:
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
return self.async_update_reload_and_abort(
|
||||
entry, data_updates=data, title=self._title(data)
|
||||
)
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure", data_schema=self._schema(data if "data" in locals() else entry.data), errors=errors
|
||||
)
|
||||
|
||||
return self.async_show_form(step_id="reconfigure", data_schema=self._schema(entry.data))
|
||||
|
||||
async def _async_validate_input(
|
||||
self, user_input: dict[str, Any]
|
||||
) -> tuple[dict[str, Any], dict[str, str]]:
|
||||
errors: dict[str, str] = {}
|
||||
host = str(user_input.get(CONF_HOST, "")).strip()
|
||||
if not host:
|
||||
errors[CONF_HOST] = "invalid_host"
|
||||
try:
|
||||
port = int(user_input.get(CONF_PORT, DEFAULT_PORT))
|
||||
except (TypeError, ValueError):
|
||||
errors[CONF_PORT] = "invalid_port"
|
||||
port = DEFAULT_PORT
|
||||
if not 1 <= port <= 65535:
|
||||
errors[CONF_PORT] = "invalid_port"
|
||||
return {CONF_HOST: host, CONF_PORT: port}, errors
|
||||
|
||||
async def _async_validate_proxy(self, data: dict[str, Any]) -> None:
|
||||
client = ArkteosClient(data[CONF_HOST], data[CONF_PORT])
|
||||
try:
|
||||
await client.start()
|
||||
if not await client.wait_until_connected(VALIDATION_TIMEOUT):
|
||||
raise CannotConnect
|
||||
deadline = asyncio.get_running_loop().time() + VALIDATION_TIMEOUT
|
||||
while client.last_valid_frame is None:
|
||||
if asyncio.get_running_loop().time() >= deadline:
|
||||
raise ValidationTimeout
|
||||
if client.last_error is not None and not client.connected:
|
||||
raise CannotConnect
|
||||
await asyncio.sleep(0.05)
|
||||
finally:
|
||||
await client.stop()
|
||||
|
||||
def _entry_exists(self, host: str, port: int, exclude_entry_id: str | None = None) -> bool:
|
||||
return any(
|
||||
entry.entry_id != exclude_entry_id
|
||||
and entry.data.get(CONF_HOST) == host
|
||||
and entry.data.get(CONF_PORT) == port
|
||||
for entry in self._async_current_entries()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _title(data: dict[str, Any]) -> str:
|
||||
return f"Arkteos {data[CONF_HOST]}:{data[CONF_PORT]}"
|
||||
|
||||
@staticmethod
|
||||
def _unique_id(data: dict[str, Any]) -> str:
|
||||
return f"{data[CONF_HOST]}:{data[CONF_PORT]}"
|
||||
|
||||
@staticmethod
|
||||
def _schema(defaults: dict[str, Any] | None = None) -> vol.Schema:
|
||||
defaults = defaults or {}
|
||||
return vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_HOST, default=defaults.get(CONF_HOST, "")): str,
|
||||
vol.Required(CONF_PORT, default=defaults.get(CONF_PORT, DEFAULT_PORT)): vol.Coerce(int),
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Constantes de l'intégration Arkteos."""
|
||||
|
||||
from homeassistant.const import Platform
|
||||
|
||||
|
||||
DOMAIN = "arkteos"
|
||||
DEFAULT_PORT = 9641
|
||||
CONF_HOST = "host"
|
||||
CONF_PORT = "port"
|
||||
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR]
|
||||
RUNTIME_DATA = "runtime_data"
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Entités Home Assistant partageant le client Arkteos."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from .client import ArkteosClient
|
||||
|
||||
|
||||
class ArkteosEntity(Entity):
|
||||
"""Base d'entité sans lecture réseau dans ses propriétés."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(self, client: ArkteosClient) -> None:
|
||||
self._client = client
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Reflète la disponibilité déterminée par le client partagé."""
|
||||
|
||||
return self._client.available
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Abonne l'entité aux changements de disponibilité."""
|
||||
|
||||
await super().async_added_to_hass()
|
||||
self._client.add_availability_callback(self._handle_availability)
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Retire le callback avant la destruction de l'entité."""
|
||||
|
||||
self._client.remove_availability_callback(self._handle_availability)
|
||||
await super().async_will_remove_from_hass()
|
||||
|
||||
def _handle_availability(self, _available: bool) -> None:
|
||||
self.async_write_ha_state()
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"domain": "arkteos",
|
||||
"name": "Arkteos",
|
||||
"config_flow": true,
|
||||
"iot_class": "local_push",
|
||||
"version": "0.1.0",
|
||||
"requirements": [],
|
||||
"documentation": "REPLACE_WITH_GITEA_REPOSITORY_URL",
|
||||
"codeowners": []
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"title": "Arkteos",
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "Configurer Arkteos",
|
||||
"description": "Renseignez le proxy TCP Arkteos.",
|
||||
"data": {
|
||||
"host": "Hôte du proxy",
|
||||
"port": "Port du proxy"
|
||||
}
|
||||
},
|
||||
"reconfigure": {
|
||||
"title": "Reconfigurer Arkteos",
|
||||
"description": "Modifiez l'hôte ou le port du proxy.",
|
||||
"data": {
|
||||
"host": "Hôte du proxy",
|
||||
"port": "Port du proxy"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Impossible de se connecter au proxy.",
|
||||
"invalid_host": "L'hôte est invalide.",
|
||||
"invalid_port": "Le port doit être compris entre 1 et 65535.",
|
||||
"timeout": "Aucune trame valide n'a été reçue avant expiration du délai.",
|
||||
"unknown": "Erreur inattendue."
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "Ce proxy est déjà configuré.",
|
||||
"reconfigure_successful": "Arkteos a été reconfiguré."
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"connection": {
|
||||
"name": "PAC connectée"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"title": "Arkteos",
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "Configurer Arkteos",
|
||||
"description": "Renseignez le proxy TCP Arkteos.",
|
||||
"data": {
|
||||
"host": "Hôte du proxy",
|
||||
"port": "Port du proxy"
|
||||
}
|
||||
},
|
||||
"reconfigure": {
|
||||
"title": "Reconfigurer Arkteos",
|
||||
"description": "Modifiez l'hôte ou le port du proxy.",
|
||||
"data": {
|
||||
"host": "Hôte du proxy",
|
||||
"port": "Port du proxy"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Impossible de se connecter au proxy.",
|
||||
"invalid_host": "L'hôte est invalide.",
|
||||
"invalid_port": "Le port doit être compris entre 1 et 65535.",
|
||||
"timeout": "Aucune trame valide n'a été reçue avant expiration du délai.",
|
||||
"unknown": "Erreur inattendue."
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "Ce proxy est déjà configuré.",
|
||||
"reconfigure_successful": "Arkteos a été reconfiguré."
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"connection": {
|
||||
"name": "PAC connectée"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user