Skip to content

Commit

Permalink
fix for unknown events
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Oct 23, 2023
1 parent 26af076 commit 822105a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions py_vista_turbo_serial/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
##################################################################################
"""

from typing import Union

class UnknownEventException(Exception):
pass
EventTypes: type = Union['SystemEvent', 'UnknownEvent']


class SystemEvent:
Expand All @@ -55,11 +55,22 @@ def __repr__(self) -> str:
@classmethod
def event_for_code(
cls, event_code: int, zone_or_user: int
) -> 'SystemEvent':
) -> EventTypes:
for klass in cls.__subclasses__():
if klass.CODE == event_code:
return klass(zone_or_user)
raise UnknownEventException(f'Unknown event code: {event_code}')
return UnknownEvent(event_code, zone_or_user)


class UnknownEvent:

def __init__(self, code: int, zone_or_user: int):
self.zone_or_user: int = zone_or_user
self.code: int = code

def __repr__(self) -> str:
return (f'<UnknownEvent(code={self.code}, code_hex=0x{self.code:02x},'
f' zone_or_user={self.zone_or_user})>')


class AlarmEvent(SystemEvent):
Expand Down

0 comments on commit 822105a

Please sign in to comment.