Esempio n. 1
0
def setUpModule():  # pylint: disable=invalid-name
    """ Initalizes a Home Assistant server and Slave instance. """
    global hass, slave, master_api, broken_api

    hass = ha.HomeAssistant()

    hass.bus.listen('test_event', lambda _: _)
    hass.states.set('test.test', 'a_state')

    http.setup(
        hass, {
            http.DOMAIN: {
                http.CONF_API_PASSWORD: API_PASSWORD,
                http.CONF_SERVER_PORT: 8122
            }
        })

    hass.start()

    master_api = remote.API("127.0.0.1", API_PASSWORD, 8122)

    # Start slave
    slave = remote.HomeAssistant(master_api)

    slave.start()

    # Setup API pointing at nothing
    broken_api = remote.API("127.0.0.1", "", 8125)
Esempio n. 2
0
def from_config_dict(config, hass=None):
    """
    Tries to configure Home Assistant from a config dict.

    Dynamically loads required components and its dependencies.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

    enable_logging(hass)

    loader.prepare(hass)

    # Make a copy because we are mutating it.
    # Convert it to defaultdict so components can always have config dict
    config = defaultdict(dict, config)

    # Filter out the repeating and common config section [homeassistant]
    components = (key for key in config.keys()
                  if ' ' not in key and key != homeassistant.DOMAIN)

    if not core_components.setup(hass, config):
        _LOGGER.error("Home Assistant core failed to initialize. "
                      "Further initialization aborted.")

        return hass

    _LOGGER.info("Home Assistant core initialized")

    # Setup the components
    for domain in loader.load_order_components(components):
        setup_component(hass, domain, config)

    return hass
Esempio n. 3
0
def from_config_file(config_path, hass=None):
    """
    Reads the configuration file and tries to start all the required
    functionality. Will add functionality to 'hass' parameter if given,
    instantiates a new Home Assistant object if 'hass' is not given.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

        # Set config dir to directory holding config file
        hass.config_dir = os.path.abspath(os.path.dirname(config_path))

    config_dict = {}
    # check config file type
    if os.path.splitext(config_path)[1] == '.yaml':
        # Read yaml
        config_dict = yaml.load(io.open(config_path, 'r'))
    else:
        # Read config
        config = configparser.ConfigParser()
        config.read(config_path)

        for section in config.sections():
            config_dict[section] = {}

            for key, val in config.items(section):
                config_dict[section][key] = val

    return from_config_dict(config_dict, hass)
Esempio n. 4
0
    def setUp(self):  # pylint: disable=invalid-name
        self.hass = ha.HomeAssistant()

        self.test_entity = media_player.ENTITY_ID_FORMAT.format('living_room')
        self.hass.states.set(self.test_entity, STATE_OFF)

        self.test_entity2 = media_player.ENTITY_ID_FORMAT.format('bedroom')
        self.hass.states.set(self.test_entity2, "YouTube")
    def setUp(self):  # pylint: disable=invalid-name
        self.hass = ha.HomeAssistant()

        self.test_entity = chromecast.ENTITY_ID_FORMAT.format('living_room')
        self.hass.states.set(self.test_entity, chromecast.STATE_NO_APP)

        self.test_entity2 = chromecast.ENTITY_ID_FORMAT.format('bedroom')
        self.hass.states.set(self.test_entity2, "Youtube")
Esempio n. 6
0
    def setUp(self):  # pylint: disable=invalid-name
        """ Init needed objects. """
        self.hass = ha.HomeAssistant()
        loader.prepare(self.hass)
        self.assertTrue(comps.setup(self.hass, {}))

        self.hass.states.set('light.Bowl', STATE_ON)
        self.hass.states.set('light.Ceiling', STATE_OFF)
Esempio n. 7
0
    def setUp(self):  # pylint: disable=invalid-name
        """ Init needed objects. """
        self.hass = ha.HomeAssistant()

        self.hass.states.set('light.Bowl', STATE_ON)
        self.hass.states.set('light.Ceiling', STATE_OFF)
        test_group = group.Group(self.hass, 'init_group',
                                 ['light.Bowl', 'light.Ceiling'], False)

        self.group_entity_id = test_group.entity_id
Esempio n. 8
0
def from_config_file(config_path, hass=None, enable_logging=True):
    """
    Reads the configuration file and tries to start all the required
    functionality. Will add functionality to 'hass' parameter if given,
    instantiates a new Home Assistant object if 'hass' is not given.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

        # Set config dir to directory holding config file
        hass.config_dir = os.path.abspath(os.path.dirname(config_path))

    if enable_logging:
        # Setup the logging for home assistant.
        logging.basicConfig(level=logging.INFO)

        # Log errors to a file if we have write access to file or config dir
        err_log_path = hass.get_config_path("home-assistant.log")
        err_path_exists = os.path.isfile(err_log_path)

        # Check if we can write to the error log if it exists or that
        # we can create files in the containgin directory if not.
        if (err_path_exists and os.access(err_log_path, os.W_OK)) or \
           (not err_path_exists and os.access(hass.config_dir, os.W_OK)):

            err_handler = logging.FileHandler(
                err_log_path, mode='w', delay=True)

            err_handler.setLevel(logging.ERROR)
            err_handler.setFormatter(
                logging.Formatter('%(asctime)s %(name)s: %(message)s',
                                  datefmt='%H:%M %d-%m-%y'))
            logging.getLogger('').addHandler(err_handler)

        else:
            logging.getLogger(__name__).error(
                "Unable to setup error log %s (access denied)", err_log_path)

    # Read config
    config = configparser.ConfigParser()
    config.read(config_path)

    config_dict = {}

    for section in config.sections():
        config_dict[section] = {}

        for key, val in config.items(section):
            config_dict[section][key] = val

    return from_config_dict(config_dict, hass)
Esempio n. 9
0
    def setUp(self):  # pylint: disable=invalid-name
        self.hass = ha.HomeAssistant()
        loader.prepare(self.hass)
        loader.set_component('switch.test', mock_toggledevice_platform)

        mock_toggledevice_platform.init()
        self.assertTrue(
            switch.setup(self.hass, {switch.DOMAIN: {
                ha.CONF_TYPE: 'test'
            }}))

        # Switch 1 is ON, switch 2 is OFF
        self.switch_1, self.switch_2, self.switch_3 = \
            mock_toggledevice_platform.get_switches(None, None)
Esempio n. 10
0
    def setUp(self):  # pylint: disable=invalid-name
        """ Init needed objects. """
        self.hass = ha.HomeAssistant()

        self.hass.states.set('light.Bowl', STATE_ON)
        self.hass.states.set('light.Ceiling', STATE_OFF)
        self.hass.states.set('switch.AC', STATE_OFF)
        group.setup_group(self.hass, 'init_group',
                          ['light.Bowl', 'light.Ceiling'], False)
        group.setup_group(self.hass, 'mixed_group',
                          ['light.Bowl', 'switch.AC'], False)

        self.group_name = group.ENTITY_ID_FORMAT.format('init_group')
        self.mixed_group_name = group.ENTITY_ID_FORMAT.format('mixed_group')
Esempio n. 11
0
def from_config_file(config_path, hass=None):
    """
    Reads the configuration file and tries to start all the required
    functionality. Will add functionality to 'hass' parameter if given,
    instantiates a new Home Assistant object if 'hass' is not given.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

    # Set config dir to directory holding config file
    hass.config.config_dir = os.path.abspath(os.path.dirname(config_path))

    config_dict = config_util.load_config_file(config_path)

    return from_config_dict(config_dict, hass)
Esempio n. 12
0
def get_test_home_assistant(num_threads=None):
    """ Returns a Home Assistant object pointing at test config dir. """
    if num_threads:
        orig_num_threads = ha.MIN_WORKER_THREAD
        ha.MIN_WORKER_THREAD = num_threads

    hass = ha.HomeAssistant()

    if num_threads:
        ha.MIN_WORKER_THREAD = orig_num_threads

    hass.config.config_dir = get_test_config_dir()
    hass.config.latitude = 32.87336
    hass.config.longitude = -117.22743

    return hass
Esempio n. 13
0
def setUpModule():   # pylint: disable=invalid-name
    """ Initalizes a Home Assistant server. """
    global hass

    hass = ha.HomeAssistant()

    hass.bus.listen('test_event', lambda _: _)
    hass.states.set('test.test', 'a_state')

    bootstrap.setup_component(
        hass, http.DOMAIN,
        {http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
         http.CONF_SERVER_PORT: SERVER_PORT}})

    bootstrap.setup_component(hass, 'api')

    hass.start()
Esempio n. 14
0
def ensure_homeassistant_started():
    """ Ensures home assistant is started. """

    if not HAHelper.hass:
        hass = ha.HomeAssistant()

        hass.bus.listen('test_event', lambda _: _)
        hass.states.set('test.test', 'a_state')

        http.setup(hass,
                   {http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD}})

        hass.start()

        # Give objects time to startup
        time.sleep(1)

        HAHelper.hass = hass

    return HAHelper.hass
Esempio n. 15
0
def from_config_dict(config, hass=None):
    """
    Tries to configure Home Assistant from a config dict.

    Dynamically loads required components and its dependencies.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

    logger = logging.getLogger(__name__)

    loader.prepare(hass)

    # Make a copy because we are mutating it.
    # Convert it to defaultdict so components can always have config dict
    config = defaultdict(dict, config)

    # Filter out the repeating and common config section [homeassistant]
    components = (key for key in config.keys()
                  if ' ' not in key and key != homeassistant.DOMAIN)

    # Setup the components
    if core_components.setup(hass, config):
        logger.info("Home Assistant core initialized")

        for domain in loader.load_order_components(components):
            try:
                if loader.get_component(domain).setup(hass, config):
                    logger.info("component %s initialized", domain)
                else:
                    logger.error("component %s failed to initialize", domain)

            except Exception:  # pylint: disable=broad-except
                logger.exception("Error during setup of component %s", domain)

    else:
        logger.error(("Home Assistant core failed to initialize. "
                      "Further initialization aborted."))

    return hass
Esempio n. 16
0
def from_config_dict(config, hass=None):
    """
    Tries to configure Home Assistant from a config dict.

    Dynamically loads required components and its dependencies.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

    process_ha_core_config(hass, config.get(homeassistant.DOMAIN, {}))

    enable_logging(hass)

    _ensure_loader_prepared(hass)

    # Make a copy because we are mutating it.
    # Convert it to defaultdict so components can always have config dict
    # Convert values to dictionaries if they are None
    config = defaultdict(dict,
                         {key: value or {}
                          for key, value in config.items()})

    # Filter out the repeating and common config section [homeassistant]
    components = (key for key in config.keys()
                  if ' ' not in key and key != homeassistant.DOMAIN)

    if not core_components.setup(hass, config):
        _LOGGER.error('Home Assistant core failed to initialize. '
                      'Further initialization aborted.')

        return hass

    _LOGGER.info('Home Assistant core initialized')

    # Setup the components
    for domain in loader.load_order_components(components):
        _setup_component(hass, domain, config)

    return hass
Esempio n. 17
0
 def setUp(self):  # pylint: disable=invalid-name
     self.hass = ha.HomeAssistant()
     mock_http_component(self.hass)
Esempio n. 18
0
 def setUp(self):  # pylint: disable=invalid-name
     """ Init needed objects. """
     self.entity = entity.Entity()
     self.entity.entity_id = 'test.overwrite_hidden_true'
     self.hass = self.entity.hass = ha.HomeAssistant()
     self.entity.update_ha_state()
Esempio n. 19
0
import morsecode
import dht, machine
import homeassistant

print("minion v0.1.2")

sensor_name = 'sensor.minion5'
hass = homeassistant.HomeAssistant('http://jarvis:8123', 'suzymatt')

adc = machine.ADC(0)

while True:
    l = adc.read()
    print("lux", l)
    try:
        new_state = hass.set_state('sensor.sunroom_lux', str(l), {
            'unit_of_measurement': 'L',
            'friendly_name': 'sensor.sunroom_lux'
        })
    except Exception as inst:
        print('cant send to homeassistant')
        print(inst)

    d = dht.DHT22(machine.Pin(5))
    d.measure()
    c = d.temperature()
    f = c * (9.0 / 5.0) + 32
    s = str(f).split('.')[0]
    print("temp", s)
    try:
        new_state = hass.set_state('sensor.sunroom_temp', s, {
 def setUp(self):  # pylint: disable=invalid-name
     self.hass = ha.HomeAssistant()
Esempio n. 21
0
 def setUp(self):  # pylint: disable=invalid-name
     """ things to be run when tests are started. """
     self.hass = ha.HomeAssistant()
     self.hass.states.set("light.Bowl", "on")
     self.hass.states.set("switch.AC", "off")
Esempio n. 22
0
def get_test_home_assistant():
    """ Returns a Home Assistant object pointing at test config dir. """
    hass = ha.HomeAssistant()
    hass.config.config_dir = os.path.join(os.path.dirname(__file__), "config")

    return hass