Exemplo n.º 1
0
    def allParameters(self):
        """
		Similar as parameters() but this returnes more values such as the device type and the room
		"""
        params = self.parameters()
        if isinstance(params, dict):
            # Copy so we don't alter the original
            params = params.copy()
        else:
            # parameters() must return a dict
            params = {}

        devicetype = self.metadata('devicetype', None)
        if devicetype is not None:
            # Devicetype in metadata overrides the devicetype
            params['devicetype'] = devicetype
        else:
            try:
                params['devicetype'] = self.deviceType()
            except Exception as error:
                params['devicetype'] = Device.TYPE_UNKNOWN
                Application.printException(error)
        if self._room is None:
            # Make sure it's removed
            params.pop('room', None)
        else:
            params['room'] = self._room
        return params
Exemplo n.º 2
0
 def remove(self):
     for cls in self.classes:
         plugin = self.context.components.get(cls)
         if not plugin:
             continue
         if not hasattr(plugin, 'tearDown'):
             continue
         try:
             plugin.tearDown()
         except Exception as error:
             Application.printException(error)
     shutil.rmtree(self.path)
Exemplo n.º 3
0
	def allParameters(self):
		"""
		Similar as parameters() but this returnes more values such as the device type
		"""
		params = self.parameters()
		if isinstance(params, dict):
			# Copy so we don't alter the original
			params = params.copy()
		else:
			# parameters() must return a dict
			params = {}

		try:
			params['devicetype'] = self.deviceType()
		except Exception as error:
			params['devicetype'] = Device.TYPE_UNKNOWN
			Application.printException(error)
		return params
Exemplo n.º 4
0
    def runCheck(self, now):
        # Check configurations for weekdays first.
        weekdays = [
            'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday',
            'sunday'
        ]
        try:
            localHoliday = self.config(weekdays[now.weekday()])
            if localHoliday:
                return True, weekdays[now.weekday()]
        except Exception as error:
            # This should not happen. Just extra precaution
            Application.printException(error)

        country = self.config('country')
        if country == 'auto':
            # Auto lookup country from timezone
            for countrycode in pytz.country_timezones:
                timezones = pytz.country_timezones[countrycode]
                for timezone in timezones:
                    if timezone == now.tzinfo.tzname(None):
                        country = countrycode
                        break
        if country == 'auto':
            # The country could not be found from the timezone. Maybe set to UTC?
            return False, 'Unknown country'

        try:
            countryHolidays = holidays.CountryHoliday(country)
        except Exception as __error:
            return False, 'Unknown country'

        today = now.date()
        if today in countryHolidays:
            return True, countryHolidays[today]
        return False, weekdays[now.weekday()]
Exemplo n.º 5
0
    def command(self,
                action,
                value=None,
                origin=None,
                success=None,
                failure=None,
                callbackArgs=None,
                ignore=None):
        """
		This method executes a method with the device. This method must not be
		subclassed. Please subclass :func:`_command()` instead.

		:param action: description
		:return: return description

		Here below is the results of the :func:`Device.methods()` docstring.
		"""
        if callbackArgs is None:
            callbackArgs = []
        # Prevent loops from groups and similar
        if ignore is None:
            ignore = []
        if self.id() in ignore:
            return
        ignore.append(self.id())
        if isinstance(action, str) or isinstance(action, unicode):
            method = Device.methodStrToInt(action)
        else:
            method = action
        if method == Device.DIM:
            if value is None:
                value = 0  # this is an error, but at least won't crash now
            else:
                value = int(value)
        elif method == Device.RGB:
            if isinstance(value, str):
                value = int(value, 16)
            elif not isinstance(value, int):
                value = 0
            if action == 'rgbw':
                # For backwards compatibility, remove white component
                value = value >> 8
        elif method == Device.THERMOSTAT:
            if isinstance(value, str):
                # It could be a json-string. Try to decode it
                try:
                    value = json.loads(value)
                except ValueError:
                    # Could not decode, fallback to empty value
                    value = {}  # pylint: disable=R0204
            if not isinstance(value, dict):
                value = {}
        else:
            value = None

        def triggerFail(reason):
            if failure:
                try:
                    failure(reason, *callbackArgs)
                except DeviceAbortException:
                    return

        def s(state=None, stateValue=None):
            if state is None:
                state = method
            if stateValue is None:
                stateValue = value
            if success:
                try:
                    success(state=state, stateValue=stateValue, *callbackArgs)
                except DeviceAbortException:
                    return
            self.setState(state, stateValue, origin=origin)

        if method == 0:
            triggerFail(0)
            return
        try:
            self._command(method,
                          value,
                          success=s,
                          failure=triggerFail,
                          ignore=ignore)
        except Exception as error:
            Application.printException(error)
            triggerFail(0)
Exemplo n.º 6
0
    def command(self,
                action,
                value=None,
                origin=None,
                success=None,
                failure=None,
                callbackArgs=None,
                ignore=None):
        """
		This method executes a method with the device. This method must not be
		subclassed. Please subclass :func:`_command()` instead.

		:param action: description
		:return: return description

		Here below is the results of the :func:`Device.methods()` docstring.
		"""
        if callbackArgs is None:
            callbackArgs = []
        # Prevent loops from groups and similar
        if ignore is None:
            ignore = []
        if self.id() in ignore:
            return
        ignore.append(self.id())
        method, value = Device.normalizeStateValue(action, value)
        if method is Device.THERMOSTAT:
            # If mode is missing, add current mode
            # This can't be done in normalizeStateValue() since this needs the current mode (non static)
            if 'mode' not in value:
                value['mode'] = self.stateValue(Device.THERMOSTAT).get('mode')
                value[
                    'changeMode'] = False  # Probably not needed, just to be sure

        def triggerFail(reason):
            self.setStateFailed(method, value, reason, origin)
            if failure:
                try:
                    failure(reason, *callbackArgs)
                except DeviceAbortException:
                    return

        def s(state=None, stateValue=None):
            if state is None:
                state = method
            if stateValue is None:
                stateValue = value
            if success:
                try:
                    success(state=state, stateValue=stateValue, *callbackArgs)
                except DeviceAbortException:
                    return
            self.setState(state,
                          stateValue,
                          origin=origin,
                          executedStateValue=value)

        if method == 0:
            triggerFail(0)
            return
        try:
            self._command(method,
                          value,
                          success=s,
                          failure=triggerFail,
                          ignore=ignore)
        except Exception as error:
            Application.printException(error)
            triggerFail(0)