Exemplo n.º 1
0
def add(item, item_type=None, category=None, groups=None, label=None, gi_base_type=None, group_function=None):
    try:
        if isinstance(item, basestring):
            if item_type is None:
                raise Exception("Must provide item_type when creating an item by name")

            itemBuilderFactory = osgi.get_service("org.eclipse.smarthome.core.items.ItemBuilderFactory")
            baseItem = None if item_type != "Group" or gi_base_type is None else itemBuilderFactory.newItemBuilder( gi_base_type       \
                                                                                                                  , item + "_baseItem")\
                                                                                                   .build()
            group_function = None if item_type != "Group" else group_function
            item = itemBuilderFactory.newItemBuilder(item_type, item)  \
                                     .withCategory(category)           \
                                     .withGroups(groups)               \
                                     .withLabel(label)                 \
                                     .withBaseItem(baseItem)           \
                                     .withGroupFunction(group_function)\
                                     .build()
        JythonItemProvider.add(item)
    except:
        import traceback
        log.error(traceback.format_exc())
        return None
    else:
        return item
 def FromSystemLocation():
     LocationProvider = get_service(
         "org.eclipse.smarthome.core.i18n.LocationProvider")
     systemLocation = LocationProvider.getLocation()
     if (systemLocation is not None):
         return Location(systemLocation.getLongitude(),
                         systemLocation.getLatitude())
     return None
Exemplo n.º 3
0
def remove_all_links(item):
    try:
        item = validate_item(item)
        ItemChannelLinkRegistry = osgi.get_service("org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry")
        channels = ItemChannelLinkRegistry.getBoundChannels(item)
        from org.eclipse.smarthome.core.thing.link import ItemChannelLink
        links = map(lambda channel: ItemChannelLink(item, channel), channels)
        for link in links:
            JythonItemChannelLinkProvider.remove(link)
            log.debug("Link removed: [{}]".format(link))
    except:
        import traceback
        log.error(traceback.format_exc())
Exemplo n.º 4
0
# NOTE: Requires JythonItemChannelLinkProvider component
from core.jsr223 import scope
scope.scriptExtension.importPreset(None)

import core
from core import osgi, JythonItemChannelLinkProvider
from core.log import logging, LOG_PREFIX

try:
    from org.openhab.core.thing import ChannelUID
    from org.openhab.core.thing.link import ItemChannelLink
except:
    from org.eclipse.smarthome.core.thing import ChannelUID
    from org.eclipse.smarthome.core.thing.link import ItemChannelLink

ItemChannelLinkRegistry = osgi.get_service(
    "org.openhab.core.thing.link.ItemChannelLinkRegistry") or osgi.get_service(
        "org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry")

log = logging.getLogger(LOG_PREFIX + ".core.links")

__all__ = ["add_link", "remove_link"]


def validate_item(item_or_item_name):  # returns string
    if not isinstance(item_or_item_name, basestring) and not hasattr(
            item_or_item_name, 'name'):
        raise Exception(
            "\"{}\" is not a string or Item".format(item_or_item_name))
    item_name = item_or_item_name if isinstance(
        item_or_item_name, basestring) else item_or_item_name.name
    if scope.itemRegistry.getItems(item_name) == []:
def getRuleManager():
    return get_service(
        "org.openhab.core.automation.RuleManager") or get_service(
            "org.eclipse.smarthome.automation.RuleManager")
Exemplo n.º 6
0
__all__ = ["add_link", "remove_link"]

from core.jsr223.scope import scriptExtension
scriptExtension.importPreset(None)

try:
    from org.openhab.core.thing.link import ItemChannelLink
except:
    from org.eclipse.smarthome.core.thing.link import ItemChannelLink

import core
from core import osgi
from core.log import logging, LOG_PREFIX
from core.utils import validate_item, validate_channel_uid

ItemChannelLinkRegistry = osgi.get_service(
    "org.openhab.core.thing.link.ItemChannelLinkRegistry") or osgi.get_service(
        "org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry")

ManagedItemChannelLinkProvider = osgi.get_service(
    "org.openhab.core.thing.link.ManagedItemChannelLinkProvider"
) or osgi.get_service(
    "org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider")

log = logging.getLogger("{}.core.links".format(LOG_PREFIX))


def add_link(item_or_item_name, channel_uid_or_string):
    """
    This function adds a Link to an Item using a
    ManagedItemChannelLinkProvider.
Exemplo n.º 7
0
try:
    from org.openhab.core.binding import BindingInfo, BindingInfoProvider
except:
    from org.eclipse.smarthome.core.binding import BindingInfo, BindingInfoProvider

import core
from core.osgi import register_service, unregister_service, get_service

BINDING_ID = "jython"
INPUT_CHANNEL_NAME = "input"
OUTPUT_CHANNEL_NAME = "output"

THING_NAME = "echo"
THING_TYPE_UID = ThingTypeUID(BINDING_ID, THING_NAME)

config_description_registry = get_service(
    "org.openhab.core.config.core.ConfigDescriptionRegistry") or get_service(
        "org.eclipse.smarthome.config.core.ConfigDescriptionRegistry")
thing_type_registry = get_service(
    "org.openhab.core.thing.type.ThingTypeRegistry") or get_service(
        "org.eclipse.smarthome.core.thing.type.ThingTypeRegistry")

log = core.log.logging.getLogger("{}.{}".format(BINDING_ID, THING_NAME))


class EchoThingHandler(BaseThingHandler):
    def __init__(self, thing):
        BaseThingHandler.__init__(self, thing)
        self.input_channel_id = ChannelUID(thing.getUID(), INPUT_CHANNEL_NAME)
        self.output_channel_id = ChannelUID(thing.getUID(),
                                            OUTPUT_CHANNEL_NAME)
        log.debug('output_channel_id: %s', self.output_channel_id)
This example shows how to retrieve the RuleRegistry service and use it to query rule instances based on tags, enable and disable rule instances dynamically, and manually fire rules with specified inputs.
Requires a rule with a "Test tag" tag.
Tags can be set in the rule decorator or rule constructors (self.tags = ["tag1", "tag2"]).
"""

import time

from core.osgi import get_service
from core.log import logging, LOG_PREFIX

log = logging.getLogger("{}.registry_example".format(LOG_PREFIX))

# Get rules by tags
# Tags can be set in rule constructors
# Example: self.tags = ["tag1", "tag2"]
rule_registry = get_service("org.openhab.core.automation.RuleRegistry") or get_service("org.eclipse.smarthome.automation.RuleRegistry")
rules_with_tag = rule_registry.getByTag("Test tag")

for rule in rules:
    rule_status = rule_registry.getStatusInfo(rule.UID)
    log.info("Rule name=[{}], description=[{}], tags=[{}], UID=[{}], status=[{}]".format(rule.name, rule.description, rule.tags, rule.UID, rule_status))

    # disable a rule
    rule_registry.setEnabled(rule.UID, False)

    # later...
    time.sleep(5)

    # reenable the rule
    rule_registry.setEnabled(rule.UID, True)
Exemplo n.º 9
0
            from org.openhab.core.thing.link import ItemChannelLinkRegistry, ManagedItemChannelLinkProvider
        except:
            from org.eclipse.smarthome.core.thing.link import ItemChannelLinkRegistry, ManagedItemChannelLinkProvider
except:
    pass

from core import osgi
from core.log import getLogger
from core.utils import validate_item, validate_channel_uid

try:
    from org.openhab.core.thing.link import ItemChannelLink
except:
    from org.eclipse.smarthome.core.thing.link import ItemChannelLink

ITEM_CHANNEL_LINK_REGISTRY = osgi.get_service(
    "org.openhab.core.thing.link.ItemChannelLinkRegistry") or osgi.get_service(
        "org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry"
    )  # type: ItemChannelLinkRegistry

MANAGED_ITEM_CHANNEL_LINK_PROVIDER = osgi.get_service(
    "org.openhab.core.thing.link.ManagedItemChannelLinkProvider"
) or osgi.get_service(
    "org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider"
)  # type: ManagedItemChannelLinkProvider

LOG = getLogger(u"core.links")


def add_link(item_or_item_name, channel_uid_or_string):
    """
    This function adds a Link to an Item using a
Exemplo n.º 10
0
except:
    pass

from core.jsr223.scope import scriptExtension, itemRegistry

try:
    scriptExtension.importPreset(None)
except:
    pass

import core
from core import osgi
from core.log import getLogger
from core.links import remove_all_links

ItemBuilderFactory = osgi.get_service(
    "org.openhab.core.items.ItemBuilderFactory") or osgi.get_service(
        "org.eclipse.smarthome.core.items.ItemBuilderFactory"
    )  # type: ohItemBuilderFactory

ManagedItemProvider = osgi.get_service(
    "org.openhab.core.items.ManagedItemProvider") or osgi.get_service(
        "org.eclipse.smarthome.core.items.ManagedItemProvider"
    )  # type: ohManagedItemProvider

log = getLogger("core.items")


def add_item(item_or_item_name,
             item_type=None,
             category=None,
             groups=None,
Exemplo n.º 11
0
"""
__all__ = [
    "get_all_namespaces", "get_metadata", "set_metadata", "remove_metadata",
    "get_value", "set_value", "get_key_value", "set_key_value",
    "remove_key_value"
]

from core import osgi
from core.log import logging, LOG_PREFIX

try:
    from org.openhab.core.items import Metadata, MetadataKey
except:
    from org.eclipse.smarthome.core.items import Metadata, MetadataKey

METADATA_REGISTRY = osgi.get_service(
    "org.openhab.core.items.MetadataRegistry") or osgi.get_service(
        "org.eclipse.smarthome.core.items.MetadataRegistry")

LOG = logging.getLogger(u"{}.core.metadata".format(LOG_PREFIX))


def get_all_namespaces(item_name):
    """
    This function will return a list of an Item's namespaces.

    Examples:
        .. code-block::

            # Get a list of an Item's namespaces
            get_all_namespaces("Item_Name")
Exemplo n.º 12
0
# This example demonstrates using the rule registry, getting a rule's status, enabling/disabling
# rules, and manually running a rule. This will require a rule with a "Test tag" tag. Tags can
# be set in rule constructors (self.tags = ["tag1", "tag2"]), or in the rule decorator.

import time

from core.osgi import get_service
from core.log import logging, LOG_PREFIX

log = logging.getLogger("registry_example")
rule_registry = get_service(LOG_PREFIX + ".rule_registry")
rules_with_tag = rule_registry.getByTag("Test tag")

for rule in rules:
    rule_status = rule_registry.getStatusInfo(rule.UID)
    log.info("rule_status=[{}]".format(rule_status))

    # disable a rule
    rule_registry.setEnabled(rule.UID, False)

    # later...
    time.sleep(5)

    # reenable the rule
    rule_registry.setEnabled(rule.UID, True)

    # fire the rule manually (with inputs)
    log.info("Triggering rule manually")
    rule_registry.runNow(rule.UID, False, {'name': 'EXAMPLE'})
Exemplo n.º 13
0
__all__ = [
    "get_all_namespaces", "get_metadata", "set_metadata", "remove_metadata",
    "get_value", "set_value", "get_key_value", "set_key_value",
    "remove_key_value"
]

from core import osgi
from core.jsr223.scope import itemRegistry

try:
    from org.openhab.core.items import Metadata, MetadataKey
except:
    from org.eclipse.smarthome.core.items import Metadata, MetadataKey

metadata_registry = osgi.get_service(
    "org.openhab.core.items.MetadataRegistry") or osgi.get_service(
        "org.eclipse.smarthome.core.items.MetadataRegistry")

from core.log import logging, LOG_PREFIX
log = logging.getLogger("{}.core.metadata".format(LOG_PREFIX))


def get_all_namespaces(item_name):
    """
    This function will return a list of an Item's namespaces.

    Examples:
        .. code-block::

            # Get a list of an Item's namespaces
            get_all_namespaces("Item_Name")
Exemplo n.º 14
0
META_DISABLE_MOTION_TRIGGERING_IF_OTHER_LIGHT_IS_ON = 'disableMotionTriggeringIfOtherLightIsOn'

# Indicates that the switch must not be turned on when the associated
# motion sensor is triggered.
TAG_DISABLE_TRIGGERING_FROM_MOTION_SENSOR = "disable-triggering-from-motion-sensor"

# The light level threshold; if it is below this value, turn on the light.
ILLUMINANCE_THRESHOLD_IN_LUX = 8

TIME_OF_DAY_ITEM_NAME = 'VT_Time_Of_Day'

ITEM_NAME_PATTERN = '([^_]+)_([^_]+)_(.+)'  # level_location_deviceName

ZONE_NAME_PATTERN = 'Zone_([^_]+)'  # Zone_Name

MetadataRegistry = osgi.get_service(
    "org.eclipse.smarthome.core.items.MetadataRegistry")


class ZoneParser:
    '''
    Construct the zones from the existing items in OpenHab, using this naming
    convention: Floor_Location_ItemType
    For example, item "Switch FF_Foyer_LightSwitch ..." will create a zone named
    'Foyer' at the first floor; and the zone contains a Light object.

    See :class:`.ZoneManager` and :class:`.Zone`.
    '''
    def parse(self, items, itemRegistry, zoneManager):
        '''
        :param scope.items items:
        :param scope.itemRegistry itemRegistry: