2024-01-16 12:31:42 +00:00
|
|
|
"""Ecovacs util functions."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2024-01-23 20:17:18 +00:00
|
|
|
from __future__ import annotations
|
2024-01-16 12:31:42 +00:00
|
|
|
|
2024-04-23 11:16:55 +00:00
|
|
|
from enum import Enum
|
2024-01-16 12:31:42 +00:00
|
|
|
import random
|
|
|
|
import string
|
2024-01-23 20:17:18 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2024-04-23 12:33:05 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.util import slugify
|
2024-04-23 11:16:55 +00:00
|
|
|
|
2024-01-23 20:17:18 +00:00
|
|
|
from .entity import (
|
|
|
|
EcovacsCapabilityEntityDescription,
|
|
|
|
EcovacsDescriptionEntity,
|
|
|
|
EcovacsEntity,
|
|
|
|
)
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .controller import EcovacsController
|
2024-01-16 12:31:42 +00:00
|
|
|
|
|
|
|
|
2024-04-23 12:33:05 +00:00
|
|
|
def get_client_device_id(hass: HomeAssistant, self_hosted: bool) -> str:
|
2024-01-16 12:31:42 +00:00
|
|
|
"""Get client device id."""
|
2024-04-23 12:33:05 +00:00
|
|
|
if self_hosted:
|
|
|
|
return f"HA-{slugify(hass.config.location_name)}"
|
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
return "".join(
|
|
|
|
random.choice(string.ascii_uppercase + string.digits) for _ in range(8)
|
|
|
|
)
|
2024-01-23 20:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_supported_entitites(
|
|
|
|
controller: EcovacsController,
|
|
|
|
entity_class: type[EcovacsDescriptionEntity],
|
|
|
|
descriptions: tuple[EcovacsCapabilityEntityDescription, ...],
|
|
|
|
) -> list[EcovacsEntity]:
|
|
|
|
"""Return all supported entities for all devices."""
|
2024-03-12 17:42:43 +00:00
|
|
|
return [
|
|
|
|
entity_class(device, capability, description)
|
2024-06-13 09:49:20 +00:00
|
|
|
for device in controller.devices
|
2024-03-12 17:42:43 +00:00
|
|
|
for description in descriptions
|
|
|
|
if (capability := description.capability_fn(device.capabilities))
|
|
|
|
]
|
2024-04-23 11:16:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def get_name_key(enum: Enum) -> str:
|
|
|
|
"""Return the lower case name of the enum."""
|
|
|
|
return enum.name.lower()
|