added custom config and started developing roon trigger

This commit is contained in:
2024-12-16 22:52:07 +01:00
parent ccabbea9c6
commit 35c86ffbd3
6 changed files with 100 additions and 10 deletions
+73 -1
View File
@@ -1,11 +1,83 @@
from trigger import Trigger
from config import Config
from roonapi import RoonApi, RoonDiscovery # type: ignore
from functools import cached_property
from typing import Any, NamedTuple
from pathlib import Path
class RoonServer(NamedTuple):
ip: str
port: str
def __repr__(self) -> str:
return f"RoonServer: ip: {self.ip}, port: {self.port}"
class RoonTrigger(Trigger):
def __init__(self, _config: Config) -> None:
super().__init__(_config)
@property
def playing(self) -> bool:
return False
@cached_property
def appinfo(self) -> dict[str, Any]:
_appinfo = {
"extension_id": self.config.custom_config['extensionId'],
"display_name": self.config.custom_config['displayName'],
"display_version": self.config.custom_config['displayVersion'],
"publisher": self.config.custom_config['publisher'],
"email": self.config.custom_config['email'],
}
self.log.debug("appinfo: %s", _appinfo)
return _appinfo
@cached_property
def core_id(self) -> str:
_core_id_path = Path(self.config.custom_config['coreIdFilePath'])
if not _core_id_path.exists():
raise FileExistsError(f'unable to open {_core_id_path}')
_core_id = _core_id_path.read_text()
self.log.debug('roon core id: %s', _core_id)
return _core_id
@cached_property
def token(self) -> str:
_token_path = Path(self.config.custom_config['tokenFilePath'])
if not _token_path.exists():
raise FileExistsError(f'unable to open {_token_path}')
_token = _token_path.read_text()
self.log.debug('roon token: %s', _token)
return _token
@cached_property
def discover(self) -> RoonDiscovery:
return RoonDiscovery(self.core_id)
@cached_property
def server(self) -> RoonServer:
_server = self.discover.first()
assert isinstance(_server, tuple) and len(_server) == 2, "failed to discover roon server"
self.discover.stop()
roon_server = RoonServer(_server[0], _server[1])
self.log.info("found %s", roon_server)
return roon_server
@cached_property
def roonapi(self) -> RoonApi:
_roonapi = RoonApi(self.appinfo, self.token, self.server.ip, self.server.port, True)
return _roonapi
def my_state_callback(self, event, changed_ids):
"""Call when something changes in roon."""
self.log.info("my_state_callback event:%s changed_ids: %s" % (event, changed_ids))
for zone_id in changed_ids:
zone = self.roonapi.zones[zone_id]
self.log.info("zone_id:%s zone_info: %s" % (zone_id, zone))
def run(self) -> bool:
self.log.info("run check code")
self.roonapi.register_state_callback(self.my_state_callback)
return False