Remove global variable from tellstick code (#4700)
* Refactor tellstick code for increased readability. Especially highlight if "device" is a telldus core device or a HA entity. * Refactor Tellstick object model for increased clarity. * Update comments. Unify better with sensors. Fix typo bug. Add debug logging. * Refactor tellstick code for increased readability. Especially highlight if "device" is a telldus core device or a HA entity. * Refactor Tellstick object model for increased clarity. * Update comments. Unify better with sensors. Fix typo bug. Add debug logging. * Fix lint issues. * Remove global variable according to hint from balloob.
This commit is contained in:
parent
c89e6ec915
commit
97cc76b43e
3 changed files with 12 additions and 11 deletions
|
@ -29,16 +29,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
signal_repetitions = discovery_info.get(ATTR_DISCOVER_CONFIG,
|
||||
DEFAULT_SIGNAL_REPETITIONS)
|
||||
|
||||
add_devices(TellstickLight(tellcore_id, signal_repetitions)
|
||||
add_devices(TellstickLight(tellcore_id, hass.data['tellcore_registry'],
|
||||
signal_repetitions)
|
||||
for tellcore_id in discovery_info[ATTR_DISCOVER_DEVICES])
|
||||
|
||||
|
||||
class TellstickLight(TellstickDevice, Light):
|
||||
"""Representation of a Tellstick light."""
|
||||
|
||||
def __init__(self, tellcore_id, signal_repetitions):
|
||||
def __init__(self, tellcore_id, tellcore_registry, signal_repetitions):
|
||||
"""Initialize the light."""
|
||||
super().__init__(tellcore_id, signal_repetitions)
|
||||
super().__init__(tellcore_id, tellcore_registry, signal_repetitions)
|
||||
|
||||
self._brightness = 255
|
||||
|
||||
|
|
|
@ -26,7 +26,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
signal_repetitions = discovery_info.get(ATTR_DISCOVER_CONFIG,
|
||||
DEFAULT_SIGNAL_REPETITIONS)
|
||||
|
||||
add_devices(TellstickSwitch(tellcore_id, signal_repetitions)
|
||||
add_devices(TellstickSwitch(tellcore_id, hass.data['tellcore_registry'],
|
||||
signal_repetitions)
|
||||
for tellcore_id in discovery_info[ATTR_DISCOVER_DEVICES])
|
||||
|
||||
|
||||
|
|
|
@ -62,8 +62,6 @@ def setup(hass, config):
|
|||
from tellcore.library import DirectCallbackDispatcher
|
||||
from tellcore.telldus import TelldusCore
|
||||
|
||||
global TELLCORE_REGISTRY
|
||||
|
||||
try:
|
||||
tellcore_lib = TelldusCore(
|
||||
callback_dispatcher=DirectCallbackDispatcher())
|
||||
|
@ -75,8 +73,9 @@ def setup(hass, config):
|
|||
all_tellcore_devices = tellcore_lib.devices()
|
||||
|
||||
# Register devices
|
||||
TELLCORE_REGISTRY = TellstickRegistry(hass, tellcore_lib)
|
||||
TELLCORE_REGISTRY.register_tellcore_devices(all_tellcore_devices)
|
||||
tellcore_registry = TellstickRegistry(hass, tellcore_lib)
|
||||
tellcore_registry.register_tellcore_devices(all_tellcore_devices)
|
||||
hass.data['tellcore_registry'] = tellcore_registry
|
||||
|
||||
# Discover the switches
|
||||
_discover(hass, config, 'switch',
|
||||
|
@ -153,17 +152,17 @@ class TellstickDevice(Entity):
|
|||
Contains the common logic for all Tellstick devices.
|
||||
"""
|
||||
|
||||
def __init__(self, tellcore_id, signal_repetitions):
|
||||
def __init__(self, tellcore_id, tellcore_registry, signal_repetitions):
|
||||
"""Initalize the Tellstick device."""
|
||||
self._signal_repetitions = signal_repetitions
|
||||
self._state = None
|
||||
# Look up our corresponding tellcore device
|
||||
self._tellcore_device = TELLCORE_REGISTRY.get_tellcore_device(
|
||||
self._tellcore_device = tellcore_registry.get_tellcore_device(
|
||||
tellcore_id)
|
||||
# Query tellcore for the current state
|
||||
self.update()
|
||||
# Add ourselves to the mapping
|
||||
TELLCORE_REGISTRY.register_ha_device(tellcore_id, self)
|
||||
tellcore_registry.register_ha_device(tellcore_id, self)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue