Skip to content

Commit

Permalink
- replace dict with defaultdict where nested dict assignments are used
Browse files Browse the repository at this point in the history
- replace some dict .update with assignments
  • Loading branch information
kormax committed Oct 8, 2023
1 parent 2daa32b commit ada75e9
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions pyhap/accessory_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""
import asyncio
import base64
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor
import hashlib
import logging
Expand Down Expand Up @@ -865,8 +866,8 @@ def set_characteristics(self, chars_query, client_addr):

self._notify(queries, client_addr)

updates_by_accessories_services = {}
results = {}
updates_by_accessories_services = defaultdict(lambda: defaultdict(lambda: {}))
results = defaultdict(lambda: defaultdict(lambda: {}))
char_to_iid = {}

expired = False
Expand Down Expand Up @@ -905,28 +906,21 @@ def set_characteristics(self, chars_query, client_addr):
set_result_value is not None and write_response_requested
)

if aid not in results:
results[aid] = {}
if iid not in results[aid]:
results[aid][iid] = {}

results[aid][iid] = {HAP_REPR_STATUS: set_result}
aid_results = results[aid]

aid_results[iid] = {HAP_REPR_STATUS: set_result}
if return_value_in_response:
results[aid][iid][HAP_REPR_VALUE] = set_result_value
aid_results[iid][HAP_REPR_VALUE] = set_result_value

char_to_iid[char] = iid
service = char.service

if acc not in updates_by_accessories_services:
updates_by_accessories_services[acc] = {}
if service not in updates_by_accessories_services[acc]:
updates_by_accessories_services[acc][service] = {}
updates_by_accessories_services[acc][service][char] = value

# Proccess accessory and service level setter callbacks
for acc, updates_by_service in updates_by_accessories_services.items():
aid = acc.aid
aid_results = results[aid]

# Accessory level setter callbacks
acc_set_result = None
Expand All @@ -940,11 +934,11 @@ def set_characteristics(self, chars_query, client_addr):
char_set_result = _wrap_service_setter(service, chars, client_addr)
set_result = char_set_result or acc_set_result

if set_result:
for char in chars:
results[aid][char_to_iid[char]].update(
{HAP_REPR_STATUS: set_result}
)
if not set_result:
continue

for char in chars:
aid_results[char_to_iid[char]][HAP_REPR_STATUS] = set_result

characteristics = []
nonempty_results_exist = False
Expand Down

0 comments on commit ada75e9

Please sign in to comment.