1 """Support for SCSGate components."""
4 from threading
import Lock
6 from scsgate.connection
import Connection
7 from scsgate.messages
import ScenarioTriggeredMessage, StateMessage
8 from scsgate.reactor
import Reactor
9 from scsgate.tasks
import GetStatusTask
10 import voluptuous
as vol
17 _LOGGER = logging.getLogger(__name__)
19 CONF_SCS_ID =
"scs_id"
23 CONFIG_SCHEMA = vol.Schema(
24 {DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.string})}, extra=vol.ALLOW_EXTRA
27 SCSGATE_SCHEMA = vol.Schema(
28 {vol.Required(CONF_SCS_ID): cv.string, vol.Optional(CONF_NAME): cv.string}
32 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
33 """Set up the SCSGate component."""
34 device = config[DOMAIN][CONF_DEVICE]
38 scsgate =
SCSGate(device=device, logger=_LOGGER)
40 except Exception
as exception:
41 _LOGGER.error(
"Cannot setup SCSGate component: %s", exception)
44 def stop_monitor(event):
45 """Stop the SCSGate."""
46 _LOGGER.debug(
"Stopping SCSGate monitor thread")
49 hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_monitor)
50 hass.data[DOMAIN] = scsgate
56 """The class for dealing with the SCSGate device via scsgate.Reactor."""
59 """Initialize the SCSGate."""
67 connection = Connection(device=device, logger=self.
_logger_logger)
70 connection=connection,
76 """Handle a messages seen on the bus."""
78 self.
_logger_logger.debug(
"Received message %s", message)
79 if not isinstance(message, StateMessage)
and not isinstance(
80 message, ScenarioTriggeredMessage
82 msg = f
"Ignored message {message} - not relevant type"
86 if message.entity
in self.
_devices_devices:
87 new_device_activated =
False
91 new_device_activated =
True
92 if new_device_activated:
96 self.
_devices_devices[message.entity].process_event(message)
97 except Exception
as exception:
98 msg = f
"Exception while processing event: {exception}"
102 "Ignoring state message for device %s because unknown", message.entity
107 """Return a dictionary with known devices.
109 Key is device ID, value is the device itself.
114 """Add the specified device.
116 The list contain already registered ones.
117 Beware: this is not what you usually want to do, take a look at
118 `add_devices_to_register`
120 self.
_devices_devices[device.scs_id] = device
123 """List of devices to be registered."""
125 for device
in devices:
130 """Start the activation of the first device."""
135 self.
_devices_devices[device.scs_id] = device
140 """Check whether a device is already registered or not."""
152 """Start the scsgate.Reactor."""
156 """Stop the scsgate.Reactor."""
160 """Register a new task to be executed."""
def handle_message(self, message)
def add_devices_to_register(self, devices)
_devices_to_register_lock
def add_device(self, device)
def append_task(self, task)
def is_device_registered(self, device_id)
_device_being_registered_lock
def __init__(self, device, logger)
def _activate_next_device(self)
bool setup(HomeAssistant hass, ConfigType config)