Remove global variable from scsgate (#33719)
* Remove global variable from scsgate * Import CONF_SCS_ID in light.py * Run isort * Remove constant ATTR_STATE
This commit is contained in:
parent
40ce8f8c9c
commit
00e67fb2c7
4 changed files with 58 additions and 49 deletions
|
@ -14,14 +14,10 @@ import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_STATE = "state"
|
|
||||||
|
|
||||||
CONF_SCS_ID = "scs_id"
|
CONF_SCS_ID = "scs_id"
|
||||||
|
|
||||||
DOMAIN = "scsgate"
|
DOMAIN = "scsgate"
|
||||||
|
|
||||||
SCSGATE = None
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
{DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.string})}, extra=vol.ALLOW_EXTRA
|
{DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.string})}, extra=vol.ALLOW_EXTRA
|
||||||
)
|
)
|
||||||
|
@ -34,11 +30,11 @@ SCSGATE_SCHEMA = vol.Schema(
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Set up the SCSGate component."""
|
"""Set up the SCSGate component."""
|
||||||
device = config[DOMAIN][CONF_DEVICE]
|
device = config[DOMAIN][CONF_DEVICE]
|
||||||
global SCSGATE # pylint: disable=global-statement
|
scsgate = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
SCSGATE = SCSGate(device=device, logger=_LOGGER)
|
scsgate = SCSGate(device=device, logger=_LOGGER)
|
||||||
SCSGATE.start()
|
scsgate.start()
|
||||||
except Exception as exception: # pylint: disable=broad-except
|
except Exception as exception: # pylint: disable=broad-except
|
||||||
_LOGGER.error("Cannot setup SCSGate component: %s", exception)
|
_LOGGER.error("Cannot setup SCSGate component: %s", exception)
|
||||||
return False
|
return False
|
||||||
|
@ -46,9 +42,10 @@ def setup(hass, config):
|
||||||
def stop_monitor(event):
|
def stop_monitor(event):
|
||||||
"""Stop the SCSGate."""
|
"""Stop the SCSGate."""
|
||||||
_LOGGER.info("Stopping SCSGate monitor thread")
|
_LOGGER.info("Stopping SCSGate monitor thread")
|
||||||
SCSGATE.stop()
|
scsgate.stop()
|
||||||
|
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_monitor)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_monitor)
|
||||||
|
hass.data[DOMAIN] = scsgate
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -8,15 +8,16 @@ from scsgate.tasks import (
|
||||||
)
|
)
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import scsgate
|
|
||||||
from homeassistant.components.cover import PLATFORM_SCHEMA, CoverDevice
|
from homeassistant.components.cover import PLATFORM_SCHEMA, CoverDevice
|
||||||
from homeassistant.const import CONF_DEVICES, CONF_NAME
|
from homeassistant.const import CONF_DEVICES, CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
from . import CONF_SCS_ID, DOMAIN, SCSGATE_SCHEMA
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(scsgate.SCSGATE_SCHEMA)}
|
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(SCSGATE_SCHEMA)}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,19 +26,22 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
devices = config.get(CONF_DEVICES)
|
devices = config.get(CONF_DEVICES)
|
||||||
covers = []
|
covers = []
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
scsgate = hass.data[DOMAIN]
|
||||||
|
|
||||||
if devices:
|
if devices:
|
||||||
for _, entity_info in devices.items():
|
for _, entity_info in devices.items():
|
||||||
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
|
if entity_info[CONF_SCS_ID] in scsgate.devices:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
name = entity_info[CONF_NAME]
|
name = entity_info[CONF_NAME]
|
||||||
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
scs_id = entity_info[CONF_SCS_ID]
|
||||||
|
|
||||||
logger.info("Adding %s scsgate.cover", name)
|
logger.info("Adding %s scsgate.cover", name)
|
||||||
|
|
||||||
cover = SCSGateCover(name=name, scs_id=scs_id, logger=logger)
|
cover = SCSGateCover(
|
||||||
scsgate.SCSGATE.add_device(cover)
|
name=name, scs_id=scs_id, logger=logger, scsgate=scsgate
|
||||||
|
)
|
||||||
|
scsgate.add_device(cover)
|
||||||
covers.append(cover)
|
covers.append(cover)
|
||||||
|
|
||||||
add_entities(covers)
|
add_entities(covers)
|
||||||
|
@ -46,11 +50,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
class SCSGateCover(CoverDevice):
|
class SCSGateCover(CoverDevice):
|
||||||
"""Representation of SCSGate cover."""
|
"""Representation of SCSGate cover."""
|
||||||
|
|
||||||
def __init__(self, scs_id, name, logger):
|
def __init__(self, scs_id, name, logger, scsgate):
|
||||||
"""Initialize the cover."""
|
"""Initialize the cover."""
|
||||||
self._scs_id = scs_id
|
self._scs_id = scs_id
|
||||||
self._name = name
|
self._name = name
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
self._scsgate = scsgate
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scs_id(self):
|
def scs_id(self):
|
||||||
|
@ -74,15 +79,15 @@ class SCSGateCover(CoverDevice):
|
||||||
|
|
||||||
def open_cover(self, **kwargs):
|
def open_cover(self, **kwargs):
|
||||||
"""Move the cover."""
|
"""Move the cover."""
|
||||||
scsgate.SCSGATE.append_task(RaiseRollerShutterTask(target=self._scs_id))
|
self._scsgate.append_task(RaiseRollerShutterTask(target=self._scs_id))
|
||||||
|
|
||||||
def close_cover(self, **kwargs):
|
def close_cover(self, **kwargs):
|
||||||
"""Move the cover down."""
|
"""Move the cover down."""
|
||||||
scsgate.SCSGATE.append_task(LowerRollerShutterTask(target=self._scs_id))
|
self._scsgate.append_task(LowerRollerShutterTask(target=self._scs_id))
|
||||||
|
|
||||||
def stop_cover(self, **kwargs):
|
def stop_cover(self, **kwargs):
|
||||||
"""Stop the cover."""
|
"""Stop the cover."""
|
||||||
scsgate.SCSGATE.append_task(HaltRollerShutterTask(target=self._scs_id))
|
self._scsgate.append_task(HaltRollerShutterTask(target=self._scs_id))
|
||||||
|
|
||||||
def process_event(self, message):
|
def process_event(self, message):
|
||||||
"""Handle a SCSGate message related with this cover."""
|
"""Handle a SCSGate message related with this cover."""
|
||||||
|
|
|
@ -4,15 +4,16 @@ import logging
|
||||||
from scsgate.tasks import ToggleStatusTask
|
from scsgate.tasks import ToggleStatusTask
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import scsgate
|
|
||||||
from homeassistant.components.light import PLATFORM_SCHEMA, Light
|
from homeassistant.components.light import PLATFORM_SCHEMA, Light
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE, CONF_DEVICES, CONF_NAME
|
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE, CONF_DEVICES, CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
from . import CONF_SCS_ID, DOMAIN, SCSGATE_SCHEMA
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(scsgate.SCSGATE_SCHEMA)}
|
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(SCSGATE_SCHEMA)}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,33 +22,37 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
devices = config.get(CONF_DEVICES)
|
devices = config.get(CONF_DEVICES)
|
||||||
lights = []
|
lights = []
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
scsgate = hass.data[DOMAIN]
|
||||||
|
|
||||||
if devices:
|
if devices:
|
||||||
for _, entity_info in devices.items():
|
for _, entity_info in devices.items():
|
||||||
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
|
if entity_info[CONF_SCS_ID] in scsgate.devices:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
name = entity_info[CONF_NAME]
|
name = entity_info[CONF_NAME]
|
||||||
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
scs_id = entity_info[CONF_SCS_ID]
|
||||||
|
|
||||||
logger.info("Adding %s scsgate.light", name)
|
logger.info("Adding %s scsgate.light", name)
|
||||||
|
|
||||||
light = SCSGateLight(name=name, scs_id=scs_id, logger=logger)
|
light = SCSGateLight(
|
||||||
|
name=name, scs_id=scs_id, logger=logger, scsgate=scsgate
|
||||||
|
)
|
||||||
lights.append(light)
|
lights.append(light)
|
||||||
|
|
||||||
add_entities(lights)
|
add_entities(lights)
|
||||||
scsgate.SCSGATE.add_devices_to_register(lights)
|
scsgate.add_devices_to_register(lights)
|
||||||
|
|
||||||
|
|
||||||
class SCSGateLight(Light):
|
class SCSGateLight(Light):
|
||||||
"""Representation of a SCSGate light."""
|
"""Representation of a SCSGate light."""
|
||||||
|
|
||||||
def __init__(self, scs_id, name, logger):
|
def __init__(self, scs_id, name, logger, scsgate):
|
||||||
"""Initialize the light."""
|
"""Initialize the light."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._scs_id = scs_id
|
self._scs_id = scs_id
|
||||||
self._toggled = False
|
self._toggled = False
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
self._scsgate = scsgate
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scs_id(self):
|
def scs_id(self):
|
||||||
|
@ -72,7 +77,7 @@ class SCSGateLight(Light):
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(ToggleStatusTask(target=self._scs_id, toggled=True))
|
self._scsgate.append_task(ToggleStatusTask(target=self._scs_id, toggled=True))
|
||||||
|
|
||||||
self._toggled = True
|
self._toggled = True
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
@ -80,9 +85,7 @@ class SCSGateLight(Light):
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(
|
self._scsgate.append_task(ToggleStatusTask(target=self._scs_id, toggled=False))
|
||||||
ToggleStatusTask(target=self._scs_id, toggled=False)
|
|
||||||
)
|
|
||||||
|
|
||||||
self._toggled = False
|
self._toggled = False
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
|
@ -5,85 +5,91 @@ from scsgate.messages import ScenarioTriggeredMessage, StateMessage
|
||||||
from scsgate.tasks import ToggleStatusTask
|
from scsgate.tasks import ToggleStatusTask
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import scsgate
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE, CONF_DEVICES, CONF_NAME
|
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE, CONF_DEVICES, CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
from . import CONF_SCS_ID, DOMAIN, SCSGATE_SCHEMA
|
||||||
|
|
||||||
ATTR_SCENARIO_ID = "scenario_id"
|
ATTR_SCENARIO_ID = "scenario_id"
|
||||||
|
|
||||||
CONF_TRADITIONAL = "traditional"
|
CONF_TRADITIONAL = "traditional"
|
||||||
CONF_SCENARIO = "scenario"
|
CONF_SCENARIO = "scenario"
|
||||||
|
|
||||||
CONF_SCS_ID = "scs_id"
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(scsgate.SCSGATE_SCHEMA)}
|
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(SCSGATE_SCHEMA)}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the SCSGate switches."""
|
"""Set up the SCSGate switches."""
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
scsgate = hass.data[DOMAIN]
|
||||||
|
|
||||||
_setup_traditional_switches(
|
_setup_traditional_switches(
|
||||||
logger=logger, config=config, add_entities_callback=add_entities
|
logger=logger,
|
||||||
|
config=config,
|
||||||
|
scsgate=scsgate,
|
||||||
|
add_entities_callback=add_entities,
|
||||||
)
|
)
|
||||||
|
|
||||||
_setup_scenario_switches(logger=logger, config=config, hass=hass)
|
_setup_scenario_switches(logger=logger, config=config, scsgate=scsgate, hass=hass)
|
||||||
|
|
||||||
|
|
||||||
def _setup_traditional_switches(logger, config, add_entities_callback):
|
def _setup_traditional_switches(logger, config, scsgate, add_entities_callback):
|
||||||
"""Add traditional SCSGate switches."""
|
"""Add traditional SCSGate switches."""
|
||||||
traditional = config.get(CONF_TRADITIONAL)
|
traditional = config.get(CONF_TRADITIONAL)
|
||||||
switches = []
|
switches = []
|
||||||
|
|
||||||
if traditional:
|
if traditional:
|
||||||
for _, entity_info in traditional.items():
|
for _, entity_info in traditional.items():
|
||||||
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
|
if entity_info[CONF_SCS_ID] in scsgate.devices:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
name = entity_info[CONF_NAME]
|
name = entity_info[CONF_NAME]
|
||||||
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
scs_id = entity_info[CONF_SCS_ID]
|
||||||
|
|
||||||
logger.info("Adding %s scsgate.traditional_switch", name)
|
logger.info("Adding %s scsgate.traditional_switch", name)
|
||||||
|
|
||||||
switch = SCSGateSwitch(name=name, scs_id=scs_id, logger=logger)
|
switch = SCSGateSwitch(
|
||||||
|
name=name, scs_id=scs_id, logger=logger, scsgate=scsgate
|
||||||
|
)
|
||||||
switches.append(switch)
|
switches.append(switch)
|
||||||
|
|
||||||
add_entities_callback(switches)
|
add_entities_callback(switches)
|
||||||
scsgate.SCSGATE.add_devices_to_register(switches)
|
scsgate.add_devices_to_register(switches)
|
||||||
|
|
||||||
|
|
||||||
def _setup_scenario_switches(logger, config, hass):
|
def _setup_scenario_switches(logger, config, scsgate, hass):
|
||||||
"""Add only SCSGate scenario switches."""
|
"""Add only SCSGate scenario switches."""
|
||||||
scenario = config.get(CONF_SCENARIO)
|
scenario = config.get(CONF_SCENARIO)
|
||||||
|
|
||||||
if scenario:
|
if scenario:
|
||||||
for _, entity_info in scenario.items():
|
for _, entity_info in scenario.items():
|
||||||
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
|
if entity_info[CONF_SCS_ID] in scsgate.devices:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
name = entity_info[CONF_NAME]
|
name = entity_info[CONF_NAME]
|
||||||
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
scs_id = entity_info[CONF_SCS_ID]
|
||||||
|
|
||||||
logger.info("Adding %s scsgate.scenario_switch", name)
|
logger.info("Adding %s scsgate.scenario_switch", name)
|
||||||
|
|
||||||
switch = SCSGateScenarioSwitch(
|
switch = SCSGateScenarioSwitch(
|
||||||
name=name, scs_id=scs_id, logger=logger, hass=hass
|
name=name, scs_id=scs_id, logger=logger, hass=hass
|
||||||
)
|
)
|
||||||
scsgate.SCSGATE.add_device(switch)
|
scsgate.add_device(switch)
|
||||||
|
|
||||||
|
|
||||||
class SCSGateSwitch(SwitchDevice):
|
class SCSGateSwitch(SwitchDevice):
|
||||||
"""Representation of a SCSGate switch."""
|
"""Representation of a SCSGate switch."""
|
||||||
|
|
||||||
def __init__(self, scs_id, name, logger):
|
def __init__(self, scs_id, name, logger, scsgate):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._scs_id = scs_id
|
self._scs_id = scs_id
|
||||||
self._toggled = False
|
self._toggled = False
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
self._scsgate = scsgate
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scs_id(self):
|
def scs_id(self):
|
||||||
|
@ -108,7 +114,7 @@ class SCSGateSwitch(SwitchDevice):
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(ToggleStatusTask(target=self._scs_id, toggled=True))
|
self._scsgate.append_task(ToggleStatusTask(target=self._scs_id, toggled=True))
|
||||||
|
|
||||||
self._toggled = True
|
self._toggled = True
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
@ -116,9 +122,7 @@ class SCSGateSwitch(SwitchDevice):
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(
|
self._scsgate.append_task(ToggleStatusTask(target=self._scs_id, toggled=False))
|
||||||
ToggleStatusTask(target=self._scs_id, toggled=False)
|
|
||||||
)
|
|
||||||
|
|
||||||
self._toggled = False
|
self._toggled = False
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
Loading…
Add table
Reference in a new issue