1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
|
#
# Copyright 2019 aiohomekit team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations
import argparse
from argparse import ArgumentParser, Namespace
import asyncio
from collections.abc import AsyncIterator
import contextlib
import locale
import logging
import pathlib
import re
import sys
from zeroconf.asyncio import AsyncServiceBrowser, AsyncZeroconf
from aiohomekit.characteristic_cache import CharacteristicCacheFile
import aiohomekit.hkjson as hkjson
from .controller import Controller
from .exceptions import HomeKitException
from .zeroconf import ZeroconfServiceListener
logger = logging.getLogger(__name__)
XDG_DATA_HOME = pathlib.Path.home() / ".local" / "share"
DEFAULT_PAIRING_FILE = XDG_DATA_HOME / "aiohomekit" / "pairing.json"
@contextlib.asynccontextmanager
async def get_controller(args: argparse.Namespace) -> AsyncIterator[Controller]:
charmap_path = pathlib.Path(args.file).parent / "charmap.json"
zeroconf = AsyncZeroconf()
controller = Controller(
async_zeroconf_instance=zeroconf,
char_cache=CharacteristicCacheFile(charmap_path),
)
try:
controller.load_data(args.file)
except Exception:
logger.exception(f"Error while loading {args.file}")
raise SystemExit
async with zeroconf:
listener = ZeroconfServiceListener()
browser = AsyncServiceBrowser(
zeroconf.zeroconf,
[
"_hap._tcp.local.",
"_hap._udp.local.",
],
listener=listener,
)
async with controller:
yield controller
await browser.async_cancel()
def pin_from_keyboard():
read_pin = ""
while re.match(r"^\d{3}-\d{2}-\d{3}$", read_pin) is None:
read_pin = input("Enter device pin (XXX-YY-ZZZ): ")
return read_pin
def setup_logging(level: None) -> None:
"""
Set up the logging to use a decent format and the log level given as parameter.
:param level: the log level used for the root logger
"""
logging.basicConfig(
format="%(asctime)s %(filename)s:%(lineno)04d %(levelname)s %(message)s"
)
if level:
getattr(logging, level.upper())
numeric_level = getattr(logging, level.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: %s" % level)
logging.getLogger().setLevel(numeric_level)
def add_log_arguments(parser: ArgumentParser) -> None:
"""
Adds command line arguments to control logging behaviour.
:param parser: The argparse.ArgumentParser object to add to.
"""
parser.add_argument("--log", action="store", dest="loglevel")
def prepare_string(input_string):
"""
Make a string save for printing in a terminal. The string get recoded using the terminals preferred locale and
replacing the characters that cannot be encoded.
:param input_string: the input string
:return: the output string which is save for printing
"""
return "{t}".format(
t=input_string.encode(locale.getpreferredencoding(), errors="replace").decode()
)
async def discover(args):
async with get_controller(args) as controller:
await asyncio.sleep(30)
async for discovery in controller.async_discover(args.timeout):
if args.unpaired_only and not discovery.paired:
continue
desc = discovery.description
print(f"Name: {desc.name}")
print(f"Device ID (id): {desc.id}")
if hasattr(desc, "model"):
print(f"Model Name (md): {desc.model}")
if hasattr(desc, "feature_flags"):
print(f"Feature Flags (ff): {desc.feature_flags!s}")
if desc.status_flags:
print(f"Status Flags (sf): {desc.status_flags!s}")
print(f"Category (ci): {desc.category!s}")
print(f"Configuration number (c#): {desc.config_num}")
print(f"State Number (s#): {desc.state_num}")
print()
return True
async def pair(args):
async with get_controller(args) as controller:
if args.alias in controller.aliases:
print(f'"{args.alias}" is a already known alias')
return False
discovery = await controller.async_find(args.device)
try:
finish_pairing = await discovery.async_start_pairing(args.alias)
except HomeKitException as e:
print(str(e))
return False
pin = args.pin if args.pin else pin_from_keyboard()
try:
await finish_pairing(pin)
except HomeKitException as e:
print(str(e))
return False
controller.save_data(args.file)
print(f'Pairing for "{args.alias}" was established.')
return True
async def get_accessories(args: Namespace) -> bool:
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
try:
pairing = controller.aliases[args.alias]
data = await pairing.list_accessories_and_characteristics()
controller.save_data(args.file)
except Exception:
logging.exception("Error whilst fetching /accessories")
return False
# prepare output
if args.output == "json":
print(hkjson.dumps_indented(data))
elif args.output == "compact":
for accessory in data:
aid = accessory["aid"]
for service in accessory["services"]:
s_type = service["type"]
s_iid = service["iid"]
print(f"{aid}.{s_iid}: >{s_type}<")
for characteristic in service["characteristics"]:
c_iid = characteristic["iid"]
value = characteristic.get("value", "")
c_type = characteristic["type"]
perms = ",".join(characteristic["perms"])
desc = characteristic.get("description", "")
print(f" {aid}.{c_iid}: {value} ({desc}) >{c_type}< [{perms}]")
return True
async def get_characteristics(args: Namespace) -> bool:
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
pairing = controller.aliases[args.alias]
# convert the command line parameters to the required form
characteristics = [
(int(c.split(".")[0]), int(c.split(".")[1])) for c in args.characteristics
]
# get the data
try:
data = await pairing.get_characteristics(
characteristics,
)
except Exception:
logging.exception("Error whilst getting characteristic values")
return False
# print the data
tmp = {}
for k in data:
nk = str(k[0]) + "." + str(k[1])
tmp[nk] = data[k]
print(hkjson.dumps_indented(tmp))
return True
async def put_characteristics(args: Namespace) -> bool:
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
try:
pairing = controller.aliases[args.alias]
# FIXME use Service.build_update
characteristics = [
(
int(c[0].split(".")[0]), # the first part is the aid, must be int
int(c[0].split(".")[1]), # the second part is the iid, must be int
hkjson.loads(c[1]),
)
for c in args.characteristics
]
results = await pairing.put_characteristics(characteristics)
except Exception:
logging.exception("Unhandled error whilst writing to device")
return False
for key, value in results.items():
aid = key[0]
iid = key[1]
status = value["status"]
desc = value["description"]
# used to be < 0 but bluetooth le errors are > 0 and only success (= 0) needs to be checked
if status != 0:
print(
"put_characteristics failed on {aid}.{iid} because: {reason} ({code})".format(
aid=aid, iid=iid, reason=desc, code=status
)
)
return True
async def identify(args: Namespace) -> bool:
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
try:
pairing = controller.aliases[args.alias]
await pairing.identify()
except Exception:
logging.exception("Unhandled error whilst identifying device")
return False
return True
async def list_pairings(args: Namespace) -> bool:
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
exit(-1)
pairing = controller.aliases[args.alias]
try:
pairings = await pairing.list_pairings()
except Exception as e:
print(e)
logging.debug(e, exc_info=True)
sys.exit(-1)
for pairing in pairings:
print("Pairing Id: {id}".format(id=pairing["pairingId"]))
print("\tPublic Key: 0x{key}".format(key=pairing["publicKey"]))
print(
"\tPermissions: {perm} ({type})".format(
perm=pairing["permissions"], type=pairing["controllerType"]
)
)
return True
async def remove_pairing(args):
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
pairing = controller.aliases[args.alias]
await pairing.remove_pairing(args.controllerPairingId)
controller.save_data(args.file)
print(f'Pairing for "{args.alias}" was removed.')
return True
async def unpair(args):
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
await controller.remove_pairing(args.alias)
controller.save_data(args.file)
print(f"Device {args.alias} was completely unpaired.")
return True
async def get_events(args):
async with get_controller(args) as controller:
if args.alias not in controller.aliases:
print(f'"{args.alias}" is no known alias')
return False
pairing = controller.aliases[args.alias]
# convert the command line parameters to the required form
characteristics = [
(int(c.split(".")[0]), int(c.split(".")[1])) for c in args.characteristics
]
def handler(data):
# print the data
tmp = {}
for k in data:
nk = str(k[0]) + "." + str(k[1])
tmp[nk] = data[k]
print(hkjson.dumps_indented(tmp))
pairing.dispatcher_connect(handler)
results = await pairing.subscribe(characteristics)
if results:
for key, value in results.items():
aid = key[0]
iid = key[1]
status = value["status"]
desc = value["description"]
if status < 0:
print(
"watch failed on {aid}.{iid} because: {reason} ({code})".format(
aid=aid, iid=iid, reason=desc, code=status
)
)
return False
while True:
# get the data
try:
data = await pairing.get_characteristics(
characteristics,
)
handler(data)
except Exception:
logging.exception("Error whilst fetching /accessories")
return False
await asyncio.sleep(10)
return True
def setup_parser_for_pairing(parser: ArgumentParser) -> None:
parser.add_argument(
"-a", action="store", required=True, dest="alias", help="alias for the pairing"
)
async def main(argv: list[str] | None = None) -> None:
argv = argv or sys.argv[1:]
parser = argparse.ArgumentParser(
description="aiohomekitctl",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"--adapter",
action="store",
dest="adapter",
default="hci0",
help="the bluetooth adapter to be used (defaults to hci0)",
)
add_log_arguments(parser)
parser.add_argument(
"-f",
action="store",
required=False,
dest="file",
default=DEFAULT_PAIRING_FILE,
help="File with the pairing data",
)
subparsers = parser.add_subparsers(
title="available commands", metavar="command [options ...]"
)
# discover
discover_parser = subparsers.add_parser(
"discover", help="Find HomeKit devices accessible from this controller"
)
discover_parser.set_defaults(func=discover)
discover_parser.add_argument(
"-t",
action="store",
required=False,
dest="timeout",
type=int,
default=10,
help="Number of seconds to wait",
)
discover_parser.add_argument(
"-u",
action="store_true",
required=False,
dest="unpaired_only",
help="If activated, this option will show only unpaired HomeKit devices",
)
# pair
pair_parser = subparsers.add_parser(
"pair", help="Pair with an unpaired HomeKit device"
)
pair_parser.set_defaults(func=pair)
setup_parser_for_pairing(pair_parser)
pair_parser.add_argument(
"-d",
action="store",
required=True,
dest="device",
help="HomeKit Device ID (use discover to get it)",
)
pair_parser.add_argument(
"-p",
action="store",
required=False,
dest="pin",
help="HomeKit configuration code",
)
# get_accessories - return all characteristics of all services of all accessories.
get_accessories_parser = subparsers.add_parser(
"accessories",
help="List all accessories, services and characteristics for a paired device",
)
get_accessories_parser.set_defaults(func=get_accessories)
setup_parser_for_pairing(get_accessories_parser)
get_accessories_parser.add_argument(
"-o",
action="store",
dest="output",
default="compact",
choices=["json", "compact"],
help="Specify output format",
)
# get_characteristics - get only requested characteristics
get_char_parser = subparsers.add_parser(
"get",
help="Read an up to date value from a characteristic of a paired device",
)
get_char_parser.set_defaults(func=get_characteristics)
setup_parser_for_pairing(get_char_parser)
get_char_parser.add_argument(
"-c",
action="append",
required=True,
dest="characteristics",
help="Read characteristics, multiple characteristics can be given by repeating the option",
)
# put_characteristics - set characteristics values
put_char_parser = subparsers.add_parser(
"put", help="Write to a characteristic of a paired device"
)
put_char_parser.set_defaults(func=put_characteristics)
setup_parser_for_pairing(put_char_parser)
put_char_parser.add_argument(
"-c",
action="append",
required=False,
dest="characteristics",
nargs=2,
help="Use aid.iid value to change the value. Repeat to change multiple characteristics.",
)
identify_parser = subparsers.add_parser("identify", help="Identify a paired device")
identify_parser.set_defaults(func=identify)
setup_parser_for_pairing(identify_parser)
get_events_parser = subparsers.add_parser(
"watch", help="Monitor changes to characteristics on this device"
)
get_events_parser.set_defaults(func=get_events)
setup_parser_for_pairing(get_events_parser)
get_events_parser.add_argument(
"-c",
action="append",
required=True,
dest="characteristics",
help="Read characteristics, multiple characteristics can be given by repeating the option",
)
# list_pairings - list all pairings
list_pairings_parser = subparsers.add_parser(
"list-pairings", help="List all pairings from a paired device"
)
list_pairings_parser.set_defaults(func=list_pairings)
setup_parser_for_pairing(list_pairings_parser)
# remove_pairing - remove sub pairing
remove_pairing_parser = subparsers.add_parser(
"remove-pairing", help="Remove a subpairing from a paired device"
)
remove_pairing_parser.set_defaults(func=remove_pairing)
setup_parser_for_pairing(remove_pairing_parser)
remove_pairing_parser.add_argument(
"-i",
action="store",
required=True,
dest="controllerPairingId",
help="this pairing ID identifies the controller who should be removed from accessory",
)
# unpair - completely unpair the device
unpair_parser = subparsers.add_parser(
"unpair", help="Completely unpair this device"
)
unpair_parser.set_defaults(func=unpair)
setup_parser_for_pairing(unpair_parser)
args = parser.parse_args(argv)
setup_logging(args.loglevel)
if not hasattr(args, "func"):
parser.print_help()
sys.exit(1)
if not await args.func(args):
sys.exit(1)
def sync_main():
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
if __name__ == "__main__":
sync_main()
|