lib.utils

This library contails the Utile-class for SmartHomeNG.

New helper-functions are going to be implemented in this library.

class lib.utils.Utils[source]

Bases: object

static check_hashed_password(pwd_to_check, hashed_pwd)[source]

Check if given plaintext password matches the hashed password An empty password is always rejected

Parameters:
  • pwd_to_check (str) – plaintext password to check
  • hashed_pwd (str) – hashed password
Returns:

True: password matches, False: password does not match

Return type:

bool

static create_hash(plaintext)[source]

Create hash (currently sha512) for given plaintext value

Parameters:plaintext (str) – plaintext
Returns:hash of plaintext, lowercase letters
Return type:str
static get_local_ipv4_address()[source]

Get’s local ipv4 address TODO: What if more than one interface present ?

Returns:IPv4 address as a string
Return type:string
static get_local_ipv6_address()[source]

Get’s local ipv6 address TODO: What if more than one interface present ?

Returns:IPv6 address as a string
Return type:string
static is_float(string)[source]

Checks if a string is a float.

Parameters:string (str) – String to check.
Returns:True if a cast to float works, false otherwise.
Return type:bool
static is_hash(value)[source]

Check if value is a valid hash (currently sha512) value

Parameters:value (str) – value to check
Returns:True: given value can be a sha512 hash, False: given value can not be a sha512 hash
Return type:bool
static is_hostname(string)[source]

Checks if a string is a valid hostname

The hostname has is checked to have a valid format

Parameters:string (str) – String to check
Returns:True if a hostname, false otherwise.
Return type:bool
static is_int(string)[source]

Checks if a string is a integer.

Parameters:string (str) – String to check.
Returns:True if a cast to int works, false otherwise.
Return type:bool
static is_ip(string)[source]

FUTURE: Checks if a string is a valid ip-address (v4 or v6) ACTUAL: redirects to ipv4 only check for backwards compatibility

Parameters:string (str) – String to check
Returns:True if an ip, false otherwise.
Return type:bool
static is_ipv4(string)[source]

Checks if a string is a valid ip-address (v4)

Parameters:string (str) – String to check
Returns:True if an ip, false otherwise.
Return type:bool
static is_ipv6(string)[source]

Checks if a string is a valid ip-address (v6)

Parameters:string (str) – String to check
Returns:True if an ipv6, false otherwise.
Return type:bool
static is_knx_groupaddress(groupaddress)[source]

Checks if the passed string is a valid knx goup address

The checked format is:
main group (0-31 = 5 bits) middle group (0-7 = 3 bits) subgroup (0-255 = 8 bits)
Parameters:groupaddress (str) – String to check
Returns:True if a groupaddress can be recognized, false otherwise.
Return type:bool
static is_mac(mac)[source]

Validates a MAC address

Parameters:mac – MAC address
Returns:True if value is a MAC
Return type:bool
static is_timeframe(string)[source]

Checks if a string is a timeframe. A timeframe consists of a number and an optional unit identifier (e.g. 2h, 30m, …). Unit identifiers are: i for minutes, h for hours, d for days, w for weeks, m for months, y for years. If omitted milliseconds are assumed.

Parameters:string (str) – String to check.
Returns:True if a timeframe can be recognized, false otherwise.
Return type:bool
static string_to_list(string)[source]

Convert a string to a list

If the parameter is not of type str, the parameter gest returned unchanged

If parameter string is - a list, it gets returned unchanged - a simple datatype other than string, it gets returned unchanged - an empty string, it gets returned unchanged - a non-empty string, it gets converted to a list of len=1 - format [<str>,<str>], it gets converted to a list

Parameters:string (str) – string to convert
Returns:list of unchanged value
static strip_quotes(string)[source]

If a string contains quotes as first and last character, this function returns the string without quotes, otherwise the string is returned unchanged

Parameters:string (str) – string to check for quotes
Returns:string with quotes stripped
Return type:str
static strip_quotes_fromlist(string)[source]

If a string representation of a list contains quotes as first and last character of a list entry, this function returns the string representation without the qoutes, otherwise the string is returned unchanged

Parameters:string (str) – string representation of a list to check for quotes
Returns:string representation with square quotes stripped
Return type:str
static strip_square_brackets(string)[source]

If a string contains square brackets as first and last character, this function returns the string without the brackets, otherwise the string is returned unchanged

Parameters:string (str) – string to check for square brackets
Returns:string with square brackets stripped
Return type:str
static to_bool(value, default='exception')[source]

Converts a value to boolean.

Raises exception if value is a string and can’t be converted and if no default value is given Case is ignored. These string values are allowed: - True: ‘True’, “1”, “true”, “yes”, “y”, “t”, “on” - False: “”, “0”, “faLse”, “no”, “n”, “f”, “off” Non-string values are passed to bool constructor.

:param value : value to convert :param default: optional, value to return if value can not be parsed, if default is not set this method throws an exception :type value: str, object, int, … :type value: str, object, int, …

Returns:True if cant be converted and is true, False otherwise.
Return type:bool
static to_timeframe(value)[source]

Converts a timeframe value to milliseconds. See is_timeframe() method. The special value ‘now’ is supported for the current time.

:param value : value to convert :type value: str, int, …

Returns:True if cant be converted and is true, False otherwise.
Return type:bool