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

Use attrs for classes #177

Merged
merged 7 commits into from
Sep 22, 2021
Merged

Use attrs for classes #177

merged 7 commits into from
Sep 22, 2021

Conversation

tetienne
Copy link
Collaborator

@tetienne tetienne commented Sep 15, 2021

attrs is dataclass on steroids. I use it to enforce our classes typing and to automatically generate str, repr and slots.

I have ofuscated some ids by default.

Exemple of output when logging devices, events and history:

2021-09-20 10:19:17 DEBUG (MainThread) [custom_components.tahoma] Unsupported device detected (Device(attributes=States(_states=[State(name='core:ModelName', type=<DataType.STRING: 3>, value='Sonos Connect'), State(name='core:PortNumber', type=<DataType.STRING: 3>, value='1400'), State(name='core:IPAddress', type=<DataType.STRING: 3>, value='192.168.2.173'), State(name='upnpcontrol:SonosZoneName', type=<DataType.STRING: 3>, value='Salon'), State(name='upnpcontrol:Coordinator', type=<DataType.STRING: 3>, value='true'), State(name='core:GroupId', type=<DataType.STRING: 3>, value='RINCON_000E582D045801400:3452204091')]), available=True, enabled=True, label='Salon', deviceurl=upnpcontrol://xxx-xxx-8767/uuid:RINCON_000E582D045801400, controllable_name='upnpcontrol:SonosConnectComponent', definition=Definition(commands=CommandDefinitions(_commands=[CommandDefinition(command_name='getMute', nparams=0), CommandDefinition(command_name='getVolume', nparams=0), CommandDefinition(command_name='mute', nparams=0), CommandDefinition(command_name='next', nparams=0), CommandDefinition(command_name='pause', nparams=0), CommandDefinition(command_name='play', nparams=0), CommandDefinition(command_name='previous', nparams=0), CommandDefinition(command_name='rewind', nparams=0), CommandDefinition(command_name='setVolume', nparams=1), CommandDefinition(command_name='stop', nparams=0), CommandDefinition(command_name='unmute', nparams=0), CommandDefinition(command_name='getAllPlayingInfo', nparams=0), CommandDefinition(command_name='getCurrentTransportActions', nparams=0), CommandDefinition(command_name='getGroupMute', nparams=0), CommandDefinition(command_name='getGroupVolume', nparams=0), CommandDefinition(command_name='getMediaInfo', nparams=0), CommandDefinition(command_name='getPositionInfo', nparams=0), CommandDefinition(command_name='getSonosFavorites', nparams=0), CommandDefinition(command_name='getSonosPlaylist', nparams=0), CommandDefinition(command_name='getTransportInfo', nparams=0), CommandDefinition(command_name='muteGroup', nparams=0), CommandDefinition(command_name='playURI', nparams=2), CommandDefinition(command_name='setGroupVolume', nparams=1), CommandDefinition(command_name='unmuteGroup', nparams=0)]), states=[StateDefinition(qualified_name='core:PlayState', type='DiscreteState', values=['forward', 'pause', 'playing', 'rewind', 'stop'])], widget_name='MediaRenderer', ui_class='MusicPlayer', qualified_name='upnpcontrol:SonosConnectComponent'), data_properties=None, widget='MediaRenderer', ui_class='MusicPlayer', qualified_name=None, states=States(_states=[]), type=<ProductType.ACTUATOR: 1>, placeoid='c1ac4702-e9ae-4dfe-bb41-c50072dfddc2'))
2021-09-20 10:20:12 DEBUG (MainThread) [custom_components.tahoma.coordinator] Event(timestamp=1632133212066, name=<EventName.EXECUTION_REGISTERED: 'ExecutionRegisteredEvent'>, setupoid=741ca89f-a47b-4adxxx-894d-d225c06922b1, owner_key=None, type=1, sub_type=1, time_to_next_state=None, failed_commands=None, failure_type_code=None, failure_type=None, condition_groupoid=None, placeoid=None, label='Home Assistant', metadata='', camera_id=None, deleted_raw_devices_count=None, protocol_type=None, gateway_id=None, exec_id='02b8679e-ac10-3e01-7473-4d35fb47d059', deviceurl=None, device_states=[], old_state=None, new_state=None)
2021-09-20 10:20:12 DEBUG (MainThread) [custom_components.tahoma.coordinator] Event(timestamp=1632133212066, name=<EventName.EXECUTION_STATE_CHANGED: 'ExecutionStateChangedEvent'>, setupoid=741ca89f-a47b-4adxxx-894d-d225c06922b1, owner_key=741ca89f-a47b-4adxxx-894d-d225c06922b1, type=1, sub_type=1, time_to_next_state=0, failed_commands=None, failure_type_code=None, failure_type=None, condition_groupoid=None, placeoid=None, label=None, metadata=None, camera_id=None, deleted_raw_devices_count=None, protocol_type=None, gateway_id=None, exec_id='02b8679e-ac10-3e01-7473-4d35fb47d059', deviceurl=None, device_states=[], old_state=<ExecutionState.INITIALIZED: 'INITIALIZED'>, new_state=<ExecutionState.NOT_TRANSMITTED: 'NOT_TRANSMITTED'>)
2021-09-20 10:20:12 DEBUG (MainThread) [custom_components.tahoma.coordinator] Event(timestamp=1632133212066, name=<EventName.GATEWAY_SYNCHRONIZATION_STARTED: 'GatewaySynchronizationStartedEvent'>, setupoid=_CountingAttr(counter=243, _default=None, repr=<function offuscate_id at 0x7f25b9c1dc10>, eq=True, order=True, hash=None, init=True, on_setattr=None, metadata={}), owner_key=None, type=None, sub_type=None, time_to_next_state=None, failed_commands=None, failure_type_code=None, failure_type=None, condition_groupoid=None, placeoid=None, label=None, metadata=None, camera_id=None, deleted_raw_devices_count=None, protocol_type=None, gateway_id=xxx-xxx-8767, exec_id=None, deviceurl=None, device_states=[], old_state=None, new_state=None)
2021-09-20 10:20:12 DEBUG (MainThread) [custom_components.tahoma.coordinator] Event(timestamp=1632133212068, name=<EventName.EXECUTION_STATE_CHANGED: 'ExecutionStateChangedEvent'>, setupoid=741ca89f-a47b-4adxxx-894d-d225c06922b1, owner_key=741ca89f-a47b-4adxxx-894d-d225c06922b1, type=1, sub_type=1, time_to_next_state=0, failed_commands=None, failure_type_code=None, failure_type=None, condition_groupoid=None, placeoid=None, label=None, metadata=None, camera_id=None, deleted_raw_devices_count=None, protocol_type=None, gateway_id=None, exec_id='02b8679e-ac10-3e01-7473-4d35fb47d059', deviceurl=None, device_states=[], old_state=<ExecutionState.NOT_TRANSMITTED: 'NOT_TRANSMITTED'>, new_state=<ExecutionState.TRANSMITTED: 'TRANSMITTED'>)
2021-09-20 10:24:15 INFO (MainThread) [custom_components.tahoma] HistoryExecution(id='02b8679e-ac10-3e01-7473-4d35fb47d059', event_time=1632133212066, owner='xxxx@protonmail.ch', source='mobile:tool', end_time=1632133215846, effective_start_time=1632133212066, duration=3780, label='Home Assistant', type='Immediate execution - MANUAL_CONTROL', state=<ExecutionState.FAILED: 'FAILED'>, failure_type='CMDCANCELLED', commands=[HistoryExecutionCommand(deviceurl=io://xxx-xxx-8767/5978000, command='close', rank=0, dynamic=False, state=<ExecutionState.FAILED: 'FAILED'>, failure_type='CMDCANCELLED', parameters=[])], execution_type=<ExecutionType.IMMEDIATE_EXECUTION: 'Immediate execution'>, execution_sub_type=<ExecutionSubType.MANUAL_CONTROL: 'MANUAL_CONTROL'>)
2021-09-20 10:24:15 INFO (MainThread) [custom_components.tahoma] HistoryExecution(id='029ef8c7-ac10-3e01-7473-4d35b29d5115', event_time=1632131545289, owner='xxxx@protonmail.ch', source='mobile:tool', end_time=1632131548079, effective_start_time=1632131545290, duration=2789, label='Home Assistant', type='Immediate execution - MANUAL_CONTROL', state=<ExecutionState.FAILED: 'FAILED'>, failure_type='CMDCANCELLED', commands=[HistoryExecutionCommand(deviceurl=io://xxx-xxx-8767/5978000, command='open', rank=0, dynamic=False, state=<ExecutionState.FAILED: 'FAILED'>, failure_type='CMDCANCELLED', parameters=[])], execution_type=<ExecutionType.IMMEDIATE_EXECUTION: 'Immediate execution'>, execution_sub_type=<ExecutionSubType.MANUAL_CONTROL: 'MANUAL_CONTROL'>)
2021-09-20 10:24:15 INFO (MainThread) [custom_components.tahoma] HistoryExecution(id='029ee9c7-ac10-3e01-7473-4d35b5a5c548', event_time=1632131541449, owner='xxxx@protonmail.ch', source='mobile:tool', end_time=1632131543301, effective_start_time=1632131541450, duration=1851, label='Home Assistant', type='Immediate execution - MANUAL_CONTROL', state=<ExecutionState.FAILED: 'FAILED'>, failure_type='CMDCANCELLED', commands=[HistoryExecutionCommand(deviceurl=io://xxx-xxx-8767/5978000, command='close', rank=0, dynamic=False, state=<ExecutionState.FAILED: 'FAILED'>, failure_type='CMDCANCELLED', parameters=[])], execution_type=<ExecutionType.IMMEDIATE_EXECUTION: 'Immediate execution'>, execution_sub_type=<ExecutionSubType.MANUAL_CONTROL: 'MANUAL_CONTROL'>)

It would be really helpful to log event within HA.

Tested with HA:

"requirements": [
    "git+https://github.com/imicknl/python-tahoma-api.git@enhancement/attrs#pyhoma==9.9.9"
  ],

@github-actions github-actions bot added the enhancement New feature or request label Sep 15, 2021
@tetienne tetienne force-pushed the enhancement/attrs branch 9 times, most recently from a1cbcc9 to 08c4ca7 Compare September 20, 2021 14:44
@iMicknl
Copy link
Owner

iMicknl commented Sep 22, 2021

@tetienne can you please resolve conflicts?

pyhoma/models.py Outdated Show resolved Hide resolved
pyhoma/models.py Outdated Show resolved Hide resolved
pyhoma/models.py Outdated Show resolved Hide resolved
Copy link
Owner

@iMicknl iMicknl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvement! Some minor remarks.

Copy link
Owner

@iMicknl iMicknl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to merge if you rebase and fix the two small spelling mistakes.

@iMicknl
Copy link
Owner

iMicknl commented Sep 22, 2021

@tetienne could you perhaps fix the typing of Command (https://github.com/iMicknl/python-tahoma-api/blob/e370e772f659c772587667952b193bf3adf60658/pyhoma/models.py#L184-L192) as well? We pass a list ([]) and we type it as a string...

@tetienne
Copy link
Collaborator Author

@iMicknl I fixed the typing, and the stupid English mistakes I did :D

@tetienne tetienne merged commit c019e32 into master Sep 22, 2021
@tetienne tetienne deleted the enhancement/attrs branch September 22, 2021 12:32
tetienne added a commit that referenced this pull request Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants