Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify datamodel #213

Merged
merged 42 commits into from
Feb 22, 2023
Merged
Changes from 3 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
102a98f
simplify parsing logic based on python 3.10
marcelveldt Feb 15, 2023
2fb0689
import chip package for the remaining eval call
marcelveldt Feb 15, 2023
1f4abd6
remove dupe isinstance checks
marcelveldt Feb 20, 2023
82a5437
Merge branch 'main' into simplify-datamodel
marcelveldt Feb 21, 2023
97cf522
simplify datamodel
marcelveldt Feb 21, 2023
6d3879b
Merge branch 'main' into simplify-datamodel
marcelveldt Feb 21, 2023
81e2eb8
Update matter_server/client/models/node.py
marcelveldt Feb 21, 2023
123a8c3
address some comments
marcelveldt Feb 21, 2023
a06f268
Merge branch 'simplify-datamodel' of https://github.com/home-assistan…
marcelveldt Feb 21, 2023
49564b5
fix events logic
marcelveldt Feb 21, 2023
9e24a60
store reference to interview task
marcelveldt Feb 21, 2023
5d36aee
address feedback
marcelveldt Feb 21, 2023
412669f
adjust node logging
marcelveldt Feb 21, 2023
bc3cdba
remaining typo
marcelveldt Feb 21, 2023
9b5bb1e
more typos
marcelveldt Feb 21, 2023
f035e22
some more typos
marcelveldt Feb 21, 2023
b12ca55
bump sdk wheel to 2023.2.2
marcelveldt Feb 21, 2023
00dd35f
fix parse issues for custom SDK objects
marcelveldt Feb 21, 2023
3cc5773
remove redundant code
marcelveldt Feb 21, 2023
94cb99b
simplify a bit more
marcelveldt Feb 21, 2023
60d6054
adjust logging a bit
marcelveldt Feb 21, 2023
979a31a
lint
marcelveldt Feb 21, 2023
80315da
more lint
marcelveldt Feb 21, 2023
d9f435b
lint
marcelveldt Feb 21, 2023
be02d16
fix test
marcelveldt Feb 21, 2023
79b6fbf
lint
marcelveldt Feb 21, 2023
c1fc0c5
extend device type classes a bit for HA
marcelveldt Feb 22, 2023
97368ce
some more extending to help HA
marcelveldt Feb 22, 2023
d9f10b1
more flexibility
marcelveldt Feb 22, 2023
ac20cb8
typo
marcelveldt Feb 22, 2023
8e2a3bd
add has_attribute helper
marcelveldt Feb 22, 2023
7c6b36f
ignore old dumps in init
marcelveldt Feb 22, 2023
ebf5214
fix some more bugs
marcelveldt Feb 22, 2023
c62ab78
fixed bugs and typos
marcelveldt Feb 22, 2023
7da3e76
typo
marcelveldt Feb 22, 2023
a2d3bad
handle custom type conversions in the sdk
marcelveldt Feb 22, 2023
2938436
fix mypy issues
marcelveldt Feb 22, 2023
080ae15
fix test
marcelveldt Feb 22, 2023
d3564e1
ignore strange mypy issue
marcelveldt Feb 22, 2023
bf6f226
bump min schema version
marcelveldt Feb 22, 2023
ff4dd13
fix test
marcelveldt Feb 22, 2023
95457a6
remove unused imports
marcelveldt Feb 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 28 additions & 21 deletions matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
self.wifi_credentials_set: bool = False
self.thread_credentials_set: bool = False
self.compressed_fabric_id: int | None = None
self._interview_task: asyncio.Task | None = None

@property
def fabric_id(self) -> int:
Expand Down Expand Up @@ -83,7 +84,9 @@ async def start(self) -> None:
self._nodes[node_id] = node
# setup subscriptions and (re)interviews as task in the background
# as we do not want it to block our startup
asyncio.create_task(self._check_subscriptions_and_interviews())
self._interview_task = asyncio.create_task(
self._check_subscriptions_and_interviews()
)
LOGGER.debug("Loaded %s nodes", len(self._nodes))

async def stop(self) -> None:
Expand Down Expand Up @@ -323,7 +326,8 @@ async def subscribe_node(self, node_id: int) -> None:
f"Node {node_id} does not exist or has not been interviewed."
)
assert node_id not in self._subscriptions, "Already subscribed to node"
LOGGER.debug("Setup subscription for node %s", node_id)
node_logger = LOGGER.getChild(str(node_id))
node_logger.debug("Setting up subscriptions...")

node = self._nodes[node_id]

Expand All @@ -348,7 +352,9 @@ def attribute_updated_callback(
) -> None:
assert self.server.loop is not None
new_value = transaction.GetAttribute(path)
LOGGER.debug("attribute updated -- %s - new value: %s", path, new_value)
node_logger.debug(
"Attribute updated -- %s - new value: %s", path, new_value
marcelveldt marked this conversation as resolved.
Show resolved Hide resolved
)
node = self._nodes[node_id]
attr_path = str(path.Path)
node.attributes[attr_path] = new_value
Expand All @@ -374,7 +380,7 @@ def event_callback(
) -> None:
# pylint: disable=unused-argument
assert self.server.loop is not None
LOGGER.debug("received node event: %s", data)
node_logger.debug("Received node event: %s", data)
self.event_history.append(data)
# TODO: This callback does not seem to fire ever or my test devices do not have events
self.server.loop.call_soon_threadsafe(
Expand All @@ -385,15 +391,15 @@ def error_callback(
chipError: int, transaction: Attribute.SubscriptionTransaction
) -> None:
# pylint: disable=unused-argument, invalid-name
LOGGER.error("Got error from node: %s", chipError)
node_logger.error("Got error from node: %s", chipError)

def resubscription_attempted(
transaction: Attribute.SubscriptionTransaction,
terminationError: int,
nextResubscribeIntervalMsec: int,
) -> None:
# pylint: disable=unused-argument, invalid-name
LOGGER.debug(
node_logger.debug(
"Previous subscription failed with Error: %s - re-subscribing in %s ms...",
terminationError,
nextResubscribeIntervalMsec,
Expand All @@ -407,7 +413,7 @@ def resubscription_succeeded(
transaction: Attribute.SubscriptionTransaction,
) -> None:
# pylint: disable=unused-argument, invalid-name
LOGGER.debug("Subscription succeeded for node %s", node_id)
node_logger.debug("Re-Subscription succeeded")
# mark node as available and signal consumers
if not node.available:
node.available = True
Expand All @@ -422,6 +428,7 @@ def resubscription_succeeded(
# if we reach this point, it means the node could be resolved
# and the initial subscription succeeded, mark the node available.
node.available = True
node_logger.debug("Subscription succeeded")
self.server.signal_event(EventType.NODE_UPDATED, node)

def _get_next_node_id(self) -> int:
Expand Down Expand Up @@ -463,27 +470,27 @@ async def _check_subscriptions_and_interviews(self) -> None:
continue

# setup subscriptions for the node
if node_id not in self._subscriptions:
if node_id in self._subscriptions:
continue
try:
await self.subscribe_node(node_id)
except NodeNotResolving as err:
# If the node is unreachable on the network now,
# it will throw a NodeNotResolving exception, catch this,
# log this and just try to resolve this node in the next run.
try:
await self.subscribe_node(node_id)
except NodeNotResolving as err:
LOGGER.warning(
"Unable to contact Node %s,"
" we will retry later in the background.",
node_id,
exc_info=err,
)
continue
LOGGER.warning(
"Unable to contact Node %s,"
" we will retry later in the background.",
node_id,
exc_info=err,
)

# reschedule self to run every hour
def _schedule():
asyncio.create_task(self._check_subscriptions_and_interviews())
self._interview_task = asyncio.create_task(
self._check_subscriptions_and_interviews()
)

# NOTE: do not do this in one call otherwise asyncio will start
# complaining about non awaited coroutines at shutdown
self.server.loop.call_later(3600, _schedule)

@staticmethod
Expand Down