Example #1
0
 async def _init_network(self):
     await config._log.asyncLog("info", "Added module", self.COMPONENT_NAME, "version",
                                self.VERSION, "as component", config.getComponentName(self),
                                timeout=5)
     gc.collect()
     if config.MQTT_DISCOVERY_ENABLED and self.__discover:
         await self._discovery(True)
         gc.collect()
Example #2
0
 async def _remove(self):
     """
     Cleanup method.
     Stop all loops and unsubscribe all topics.
     Also removes the component from Home-Assistant if discovery is enabled.
     """
     await _mqtt.unsubscribe(None, self)
     await config._log.asyncLog("info", "Removed component", config.getComponentName(self),
                                "module", self.COMPONENT_NAME, "version", self.VERSION,
                                timeout=5)
     if config.MQTT_DISCOVERY_ENABLED and self.__discover:
         await self._discovery(False)
Example #3
0
 def __init__(self, component: ComponentSwitch, modes_enabled: list,
              mqtt_topic_mode=None, friendly_name_mode=None, discover=True):
     global _unit_index
     _unit_index += 1
     super().__init__(COMPONENT_NAME, __version__, _unit_index, discover)
     if type(component) == str:
         self._component = config.getComponent(component)
         if self._component is None:
             raise TypeError("Component {!s} not found".format(component))
     else:
         self._component = component
     if not isinstance(self._component, ComponentSwitch):
         raise TypeError("Component needs to be of instance ComponentSwitch")
     # make this class control the component
     self._component.on_message = self.on_message
     self._component_on = self._component.on
     self._component_off = self._component.off
     self._component.on = self.on
     self._component.off = self.off
     self._component.toggle = self.toggle
     if type(modes_enabled) != list:
         raise TypeError("modes enabled needs to be a list")
     count = self._component._count if hasattr(self._component, "_count") else ""
     _name = self._component._name if hasattr(self._component, "_name") else "{!s}{!s}".format(
         COMPONENT_NAME, count)
     mqtt_topic_mode = mqtt_topic_mode or _mqtt.getDeviceTopic("{!s}/mode".format(_name),
                                                               is_request=True)
     self._topic_mode = mqtt_topic_mode
     self._frn_mode = friendly_name_mode or "{!s} Mode".format(_name)
     self._mode = Mode  # Mode is default switch behaviour if no mode is enabled
     name = config.getComponentName(self._component)
     if name is None:
         self._log = logging.getLogger("{!s}".format(_name))
     else:
         self._log = logging.getLogger("{!s}_{!s}".format(name, "Switch"))
     del name
     self._mode_lock = Lock()
     gc.collect()
     r = []
     self._modes_enabled = []
     for mode in modes_enabled:
         try:
             mod = __import__(
                 "pysmartnode.components.switches.switch_extension.{}".format(mode), globals(),
                 locals(), [], 0)
         except ImportError as e:
             _log.error("Mode {!s} not available: {!s}".format(mode, e))
             r.append(mode)
             continue
         if hasattr(mod, mode):
             modeobj = getattr(mod, mode)
         else:
             _log.error("Mode {!s} has no class {!r}".format(mode, mode))
             r.append(mode)
             continue
         try:
             modeobj = modeobj(self, self._component, self._component_on, self._component_off)
         except Exception as e:
             _log.error("Error creating mode {!s} object: {!s}".format(mode, e))
             r.append(mode)
             continue
         self._modes_enabled.append(modeobj)
     if len(r) > 0:
         _log.error("Not supported modes found which will be ignored: {!s}".format(r))
     del r