예제 #1
0
  def available_hardware(__cls__):
    __CACHE_KEY = 'known_sensors'
    cache = terrariumCache()

    known_sensors = cache.get_data(__CACHE_KEY)
    if known_sensors is None:
      known_sensors = {}
      all_types = []
      # Start dynamically loading sensors (based on: https://www.bnmetrics.com/blog/dynamic-import-in-python3)
      for file in sorted(Path(__file__).parent.glob('*_sensor.py')):
        imported_module = import_module( '.' + file.stem, package='{}'.format(__name__))

        for i in dir(imported_module):
          attribute = getattr(imported_module, i)

          if inspect.isclass(attribute) and attribute != __cls__ and issubclass(attribute, __cls__):
            setattr(sys.modules[__name__], file.stem, attribute)
            if attribute.HARDWARE is not None:
              known_sensors[attribute.HARDWARE] = attribute
              all_types += attribute.TYPES

      # Update sensors that do not have a known type. Those are remote and scripts sensors
      all_types = list(set(all_types))
      for hardware in known_sensors:
        if len(known_sensors[hardware].TYPES) == 0:
          known_sensors[hardware].TYPES = all_types

      cache.set_data(__CACHE_KEY,known_sensors,-1)

    return known_sensors
예제 #2
0
    def available_hardware(__cls__):
        __CACHE_KEY = 'known_displays'
        cache = terrariumCache()

        data = cache.get_data(__CACHE_KEY)
        if data is None:
            data = {}
            # Start dynamically loading sensors (based on: https://www.bnmetrics.com/blog/dynamic-import-in-python3)
            for file in sorted(Path(__file__).parent.glob('*_display.py')):
                imported_module = import_module('.' + file.stem,
                                                package='{}'.format(__name__))

                for i in dir(imported_module):
                    attribute = getattr(imported_module, i)

                    if inspect.isclass(
                            attribute
                    ) and attribute != terrariumDisplay and issubclass(
                            attribute, terrariumDisplay):
                        setattr(sys.modules[__name__], file.stem, attribute)
                        data[attribute.HARDWARE] = attribute

            cache.set_data(__CACHE_KEY, data, -1)

        return data
예제 #3
0
 def __init__(self,
              switchid,
              address,
              name='',
              prev_state=None,
              callback=None):
     self.__cache = terrariumCache()
     super(terrariumPowerSwitchDenkoviV2,
           self).__init__(switchid, address, name, prev_state, callback)
예제 #4
0
  def _load_hardware(self):
    EMAIL    = terrariumUtils.decrypt(os.environ.get('MEROSS_EMAIL',''))
    PASSWORD = terrariumUtils.decrypt(os.environ.get('MEROSS_PASSWORD',''))

    if '' == EMAIL or '' == PASSWORD:
      raise terrariumSensorLoadingException('Meross cloud is not enabled.')

    self._cache = terrariumCache()
    return self.address
예제 #5
0
    def _load_hardware(self):
        # Use an internal caching for speeding things up.
        self.__state_cache = terrariumCache()
        self._cloud = TerrariumMerossCloud()

        address = self._address
        if len(address) == 1:
            # When no channels/plugs defined always use the first one...
            address.append(0)

        self._device['device'] = address[0]
        self._device['switch'] = int(address[1])

        return self._device['device']
예제 #6
0
    def __init__(self, username, password):

        self.__engine = {
            'cache': terrariumCache(),
            'running': False,
            'reconnecting': False,
            'restart_counter': 0,
            'error': False,
            'event': None,
            'asyncio': terrariumAsync()
        }

        self._data = {}
        self._username = username
        self._password = password

        self.start()
예제 #7
0
  def _load_hardware(self):
    # Input format should be either:
    # - [IP],[POWER_SWITCH_NR]

    # Use an internal caching for speeding things up.
    self.__state_cache = terrariumCache()
    self._async = terrariumAsync()

    address = self._address
    if len(address) == 1:
      self._device['device'] = SmartPlug(address[0])
      self._device['switch'] = 0
    else:
      self._device['device'] = SmartStrip(address[0])
      self._device['switch'] = int(address[1])-1

    return self._device['device']
예제 #8
0
  def _load_hardware(self):
    self.__cache = terrariumCache()
    address = self._address
    if len(address) == 1:
       address.append(1)
    elif address[1] is None or '' == address[1]:
      address[1] = 1

    number_mode = len(str(address[1])) <= 2 and terrariumUtils.is_float(address[1])
    # Only reduce the boardnumber if in number mode
    if number_mode:
      address[1] = int(address[1])
      address[1] -= 1 # Reduce board number by one, human start counting at 1, computers at 0 (zero)

    scan_regex = r'^(?P<serial>[^ ]+)\W(\[(?P<device>[^\]]+\]))\W\[id=\d\]$'
    counter = 0

    cmd = self.__CMD + ['list']

    try:
      data = subprocess.check_output(cmd).strip().decode('utf-8').strip().split('\n')
    except subprocess.CalledProcessError as ex:
      # Device does not exists....
      return False

    for line in data:
      line = re.match(scan_regex,line)
      if line is not None:
        line = line.groupdict()

        if (number_mode and counter != address[1]) or (not number_mode and address[1] != line['serial']):
          counter += 1
          continue

        self._device['device'] = line['serial']
        self._device['type']   = 'v2' if 'mcp' in line['device'].lower() else ''
        self._device['switch'] = int(address[0])
        if self._device['switch'] == 0:
          self._device['switch'] = self.__get_relay_count()

        self.address = '{},{}'.format(self._device['switch'],self._device['device'])
        break

    return self._device['device']
예제 #9
0
    def _load_hardware(self):
        # Input format should be either:
        # - http://[HOST]#[POWER_SWITCH_NR]
        # - http://[HOST]/#[POWER_SWITCH_NR]
        # - http://[PASSWORD]@[HOST]#[POWER_SWITCH_NR]
        # - http://[PASSWORD]@[HOST]/#[POWER_SWITCH_NR]

        address = self._address

        # Try Tasmota
        # http://sonoff/cm?cmnd=Power[POWER_SWITCH_NR]%201
        # http://sonoff/cm?cmnd=Power[POWER_SWITCH_NR]%200
        # http://sonoff/cm?user=admin&password=joker&cmnd=Power[POWER_SWITCH_NR]%201

        device = f'{address["protocol"]}://{address["host"]}/cm?'

        if 'user' in address and 'password' in address:
            device += f'user={address["user"]}&password={address["password"]}&'

        device += 'cmnd='
        state = terrariumUtils.get_remote_data(f'{device}Status%200')

        if state is None:
            return None

        # Create the cache key for caching the relay states.
        # This is usefull when there are more then 1 relay per hardware device.
        self.__cache_key = md5(
            f'{self.HARDWARE}{state["StatusNET"]["Mac"].lower()}'.encode(
            )).hexdigest()
        self.__cache = terrariumCache()
        self.__cache.set_data(self.__cache_key, state['StatusSTS'],
                              self._CACHE_TIMEOUT)

        # We need the use the address_nr value also, as there can multiple relays per sonoff device.
        if self._device['id'] is None:
            self.id = md5(
                f'{self.HARDWARE}{state["StatusNET"]["Mac"].lower()}{address["nr"]}'
                .encode()).hexdigest()

        return device
예제 #10
0
  def __init__(self, id, _, address, name = '', prev_state = None, callback = None):
    self._device = {'device'      : None,
                    'address'     : None,
                    'name'        : None,
                    'switch'      : None,
                    'type'        : None,
                    'id'          : None,
                    'wattage'     : 0.0,
                    'flow'        : 0.0,
                    'last_update' : 0,
                    'value'       : self.OFF}

    self.__relay_cache = terrariumCache()

    self._timer = None

    self.id = id
    self.name = name
    self.address = address
    self.callback = callback

    self.load_hardware()
예제 #11
0
  def __init__(self, id, _, sensor_type, address, name = '', unit_value_callback = None, trigger_callback = None):
    self._device = {'id'             : None,
                    'name'           : None,
                    'address'        : None,
                    'type'           : sensor_type,  # Readonly property

                    'device'         : None,
                    'cache_key'      : None,
                    'power_mngt'     : None,
                    'erratic_errors' : 0,
                    'last_update'    : 0,
                    'value'          : None}

    self._sensor_cache = terrariumCache()
    self.__unit_value_callback = unit_value_callback
    self.__trigger_callback = trigger_callback

    # Set the properties
    self.id          = id
    self.name        = name
    self.address     = address

    # Load hardware can update the address value that is used for making a unique ID when not set
    self.load_hardware()
예제 #12
0
    def _load_hardware(self):
        # Input format should be either:
        # - http://[HOST]#[POWER_SWITCH_NR]
        # - http://[HOST]/#[POWER_SWITCH_NR]
        # - http://[PASSWORD]@[HOST]#[POWER_SWITCH_NR]
        # - http://[PASSWORD]@[HOST]/#[POWER_SWITCH_NR]

        address = self._address
        # TEMP data. Will be overwritten by the return value later on
        self._device['device'] = energenieconnector.EnergenieConnector(
            f'{address["protocol"]}://{address["host"]}', address['passwd'])
        if not self.__connect():
            raise terrariumRelayLoadingException(
                f'Failed loading relay {self}. Unable to login')

        # Create the cache key for caching the relay states.
        # This is usefull when there are more then 1 relay per hardware device.
        self.__cache_key = md5(
            f'{self.HARDWARE}{address["host"]}'.encode()).hexdigest()
        self.__cache = terrariumCache()

        self.__logout()

        return self._device['device']