-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metadata support for genericmiot
- Loading branch information
Showing
7 changed files
with
388 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
from typing import Dict, Optional | ||
|
||
import yaml | ||
from pydantic import BaseModel | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Loader(yaml.SafeLoader): | ||
"""Loader to implement !include command. | ||
From https://stackoverflow.com/a/9577670 | ||
""" | ||
|
||
def __init__(self, stream): | ||
self._root = os.path.split(stream.name)[0] | ||
super().__init__(stream) | ||
|
||
def include(self, node): | ||
filename = os.path.join(self._root, self.construct_scalar(node)) | ||
|
||
with open(filename) as f: | ||
return yaml.load(f, Loader) # nosec | ||
|
||
|
||
Loader.add_constructor("!include", Loader.include) | ||
|
||
|
||
class MetaBase(BaseModel): | ||
"""Base class for metadata definitions.""" | ||
|
||
description: str | ||
icon: Optional[str] = None | ||
device_class: Optional[str] = None # homeassistant only | ||
|
||
class Config: | ||
extra = "forbid" | ||
|
||
|
||
class ActionMeta(MetaBase): | ||
"""Metadata for actions.""" | ||
|
||
|
||
class PropertyMeta(MetaBase): | ||
"""Metadata for properties.""" | ||
|
||
|
||
class ServiceMeta(MetaBase): | ||
"""Describes a service.""" | ||
|
||
action: Optional[Dict[str, ActionMeta]] | ||
property: Optional[Dict[str, PropertyMeta]] | ||
event: Optional[Dict] | ||
|
||
class Config: | ||
extra = "forbid" | ||
|
||
|
||
class Namespace(MetaBase): | ||
fallback: Optional["Namespace"] = None # fallback | ||
services: Optional[Dict[str, ServiceMeta]] | ||
|
||
|
||
class Metadata(BaseModel): | ||
namespaces: Dict[str, Namespace] | ||
|
||
@classmethod | ||
def load(cls, file: Path = None): | ||
if file is None: | ||
datadir = Path(__file__).resolve().parent | ||
file = datadir / "metadata" / "extras.yaml" | ||
|
||
_LOGGER.debug("Loading metadata file %s", file) | ||
data = yaml.load(file.open(), Loader) # nosec | ||
definitions = cls(**data) | ||
|
||
return definitions | ||
|
||
def get_metadata(self, desc): | ||
extras = {} | ||
urn = desc.extras["urn"] | ||
ns_name = urn.namespace | ||
service = desc.service.name | ||
type_ = urn.type | ||
ns = self.namespaces.get(ns_name) | ||
full_name = f"{ns_name}:{service}:{type_}:{urn.name}" | ||
_LOGGER.debug("Looking metadata for %s", full_name) | ||
if ns is not None: | ||
serv = ns.services.get(service) | ||
if serv is None: | ||
_LOGGER.warning("Unable to find service: %s", service) | ||
return extras | ||
|
||
type_dict = getattr(serv, urn.type, None) | ||
if type_dict is None: | ||
_LOGGER.warning( | ||
"Unable to find type for service %s: %s", service, urn.type | ||
) | ||
return extras | ||
|
||
# TODO: implement fallback to parent? | ||
extras = type_dict.get(urn.name) | ||
if extras is None: | ||
_LOGGER.warning( | ||
"Unable to find extras for %s (%s)", urn.name, full_name | ||
) | ||
else: | ||
if extras.icon is None: | ||
_LOGGER.warning("Icon missing for %s", full_name) | ||
if extras.description is None: | ||
_LOGGER.warning("Description missing for %s", full_name) | ||
else: | ||
_LOGGER.warning("Namespace not found: %s", ns_name) | ||
# TODO: implement fallback? | ||
|
||
return extras |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
description: Metadata for dreame-specific services | ||
services: | ||
vacuum-extend: | ||
description: Extended vacuum services for dreame | ||
action: | ||
stop-clean: | ||
description: Stop cleaning | ||
icon: mdi:stop | ||
position: | ||
description: Locate robot | ||
property: | ||
work-mode: | ||
description: Work mode | ||
mop-mode: | ||
description: Mop mode | ||
waterbox-status: | ||
description: Water box attached | ||
icon: mdi:cup-water | ||
cleaning-mode: | ||
description: Cleaning mode | ||
cleaning-time: | ||
description: Cleaned time | ||
icon: mdi:timer-sand | ||
cleaning-area: | ||
description: Cleaned area | ||
icon: mdi:texture-box | ||
serial-number: | ||
description: Serial number | ||
faults: | ||
description: Error status | ||
icon: mdi:alert | ||
|
||
do-not-disturb: | ||
description: DnD for dreame | ||
icon: mdi:minus-circle-off | ||
property: | ||
enable: | ||
description: DnD enabled | ||
icon: mdi:minus-circle-off | ||
start-time: | ||
description: DnD start | ||
icon: mdi:minus-circle-off | ||
end-time: | ||
description: DnD end | ||
icon: mdi:minus-circle-off | ||
|
||
audio: | ||
description: Audio service for dreame | ||
action: | ||
position: | ||
description: Find device | ||
icon: mdi:target | ||
play-sound: | ||
description: Test sound level | ||
icon: mdi:volume-medium | ||
property: | ||
volume: | ||
description: Volume | ||
icon: mdi:volume-medium | ||
voice-packet-id: | ||
description: Voice package id | ||
icon: mdi:volume-medium | ||
|
||
clean-logs: | ||
description: Cleaning logs for dreame | ||
property: | ||
first-clean-time: | ||
description: First cleaned | ||
total-clean-time: | ||
description: Total cleaning time | ||
icon: mdi:timer-sand | ||
total-clean-times: | ||
description: Total cleaning count | ||
icon: mdi:counter | ||
total-clean-area: | ||
description: Total cleaned area | ||
icon: mdi:texture-box | ||
|
||
time: | ||
description: Time information for dreame | ||
property: | ||
time-zone: | ||
description: Timezone |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
generic: | ||
property: | ||
cleaning-time: | ||
description: Time cleaned | ||
icon: mdi:timer-sand | ||
cleaning-area: | ||
description: Area cleaned | ||
icon: mdi:texture-box | ||
brightness: | ||
description: Brightness | ||
icon: mdi:brightness-6 | ||
battery: | ||
device_class: battery | ||
icon: mdi:battery | ||
namespaces: | ||
miot-spec-v2: !include miotspec.yaml | ||
dreame-spec: !include dreamespec.yaml |
Oops, something went wrong.