Skip to content

WIP: program added/removed events#48

Open
travisghansen wants to merge 2 commits intomasterfrom
program_events
Open

WIP: program added/removed events#48
travisghansen wants to merge 2 commits intomasterfrom
program_events

Conversation

@travisghansen
Copy link
Collaborator

This allows registering callbacks for 2 different scenarios:

  1. added/removed callback for programs directly on the controller
  2. removed callback fired directly on the program

Example usage:

import time
from pyopensprinkler import Controller as OpenSprinklerController

def premoved(program):
    print("program object callback")
    print(program.index)

def pobserver(controller, action, index):
    print(f"station {index} {action}")
    if action == "added":
        print(controller.programs[index])
        controller.programs[index].add_on_removed_observer(premoved)

controller = OpenSprinklerController(...)
controller.add_program_observer(pobserver)

controller.refresh()
# go change program name
# add or remove programs
# etc
time.sleep(10)

# should trigger callbacks
controller.refresh()

exit(0)

The callback directly on the program will make it easier to bind an entity to the removal process and deregister the entity with hass in a more self-contained manner.

@travisghansen travisghansen requested a review from vinteo July 4, 2020 16:50
@vinteo
Copy link
Owner

vinteo commented Jul 4, 2020

For the sake of completeness should we have a de-register callback too?

Should there be a different event for "rename"?

Can you open a proof on concept on the HA side too?

def _set_data_flag_bit(self, index, value):
print("foo")

def add_on_removed_observer(self, observer):
Copy link
Owner

Choose a reason for hiding this comment

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

technically it is both add and remove so the name is a bit off

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is actually just remove. Chicken/egg for added.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Meaning if you’re adding a callback for added on a program...the program is already added (in the context of a program instance).

Copy link
Owner

Choose a reason for hiding this comment

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

yea but you have both "added" and "removed" "events". Maybe just add_observer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The event on the program directly is only removed..

@travisghansen
Copy link
Collaborator Author

The controller callback serves both added and removed. For simplicity I think it would be difficult try to manage a rename/modify event and would probably stick with a modify is a remove and subsequent add.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 9, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants