-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Fix and optimize digitalloggers platform #9203
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
Merged
+33
−33
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c67fe4b
Fix and optimize digitalloggers platform
dale3h 33dc99d
Fix line length
dale3h b1d3304
Fix hanging indentation
dale3h 989cdc0
Add missing docstring
dale3h fcc2cfd
Add period to end of docstring
dale3h c6fb40d
Add second blank line
dale3h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |
| import dlipower | ||
|
|
||
| host = config.get(CONF_HOST) | ||
| controllername = config.get(CONF_NAME) | ||
| controller_name = config.get(CONF_NAME) | ||
| user = config.get(CONF_USERNAME) | ||
| pswd = config.get(CONF_PASSWORD) | ||
| tout = config.get(CONF_TIMEOUT) | ||
|
|
@@ -61,37 +61,42 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |
| _LOGGER.error("Could not connect to DIN III Relay") | ||
| return False | ||
|
|
||
| devices = [] | ||
| outlets = [] | ||
| parent_device = DINRelayDevice(power_switch) | ||
|
|
||
| devices.extend( | ||
| DINRelay(controllername, device.outlet_number, parent_device) | ||
| for device in power_switch | ||
| outlets.extend( | ||
| DINRelay(controller_name, parent_device, outlet) | ||
| for outlet in power_switch[0:] | ||
| ) | ||
|
|
||
| add_devices(devices, True) | ||
| add_devices(outlets) | ||
|
|
||
|
|
||
| class DINRelay(SwitchDevice): | ||
| """Representation of a individual DIN III relay port.""" | ||
|
|
||
| def __init__(self, name, outletnumber, parent_device): | ||
| def __init__(self, controller_name, parent_device, outlet): | ||
| """Initialize the DIN III Relay switch.""" | ||
| self._controller_name = controller_name | ||
| self._parent_device = parent_device | ||
| self.controllername = name | ||
| self.outletnumber = outletnumber | ||
| self._outletname = '' | ||
| self._is_on = False | ||
| self._outlet = outlet | ||
|
|
||
| self._outlet_number = self._outlet.outlet_number | ||
| self._name = self._outlet.description | ||
| self._state = self._outlet.state == 'ON' | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the display name of this relay.""" | ||
| return self._outletname | ||
| return '{}_{}'.format( | ||
| self._controller_name, | ||
| self._name | ||
| ) | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return true if relay is on.""" | ||
| return self._is_on | ||
| return self._state | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
|
|
@@ -100,41 +105,30 @@ def should_poll(self): | |
|
|
||
| def turn_on(self, **kwargs): | ||
| """Instruct the relay to turn on.""" | ||
| self._parent_device.turn_on(outlet=self.outletnumber) | ||
| self._outlet.on() | ||
|
|
||
| def turn_off(self, **kwargs): | ||
| """Instruct the relay to turn off.""" | ||
| self._parent_device.turn_off(outlet=self.outletnumber) | ||
| self._outlet.off() | ||
|
|
||
| def update(self): | ||
| """Trigger update for all switches on the parent device.""" | ||
| self._parent_device.update() | ||
| self._is_on = ( | ||
| self._parent_device.statuslocal[self.outletnumber - 1][2] == 'ON' | ||
| ) | ||
| self._outletname = '{}_{}'.format( | ||
| self.controllername, | ||
| self._parent_device.statuslocal[self.outletnumber - 1][1] | ||
| ) | ||
|
|
||
| outlet_status = self._parent_device._statuslist[self._outlet_number - 1] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (80 > 79 characters) |
||
|
|
||
| self._name = outlet_status[1] | ||
| self._state = outlet_status[2] == 'ON' | ||
|
|
||
| class DINRelayDevice(object): | ||
| """Device representation for per device throttling.""" | ||
|
|
||
| def __init__(self, device): | ||
| def __init__(self, power_switch): | ||
| """Initialize the DINRelay device.""" | ||
| self._device = device | ||
| self.statuslocal = None | ||
|
|
||
| def turn_on(self, **kwargs): | ||
| """Instruct the relay to turn on.""" | ||
| self._device.on(**kwargs) | ||
|
|
||
| def turn_off(self, **kwargs): | ||
| """Instruct the relay to turn off.""" | ||
| self._device.off(**kwargs) | ||
| self._power_switch = power_switch | ||
| self._statuslist = None | ||
|
|
||
| @Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
| def update(self): | ||
| """Fetch new state data for this device.""" | ||
| self.statuslocal = self._device.statuslist() | ||
| self._statuslist = self._power_switch.statuslist() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without True as the second argument here, update will not be called before adding each device. I don't think that is intended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I intentionally change this because I was able to get all of the necessary data from the iteration of
power_switch(line 69).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks for the clarification!