Esempio n. 1
0
    def test_discovering_configuration_auto_detect_fails(
            self, mock_detect, mock_elevation):
        """Test config remains unchanged if discovery fails."""
        config = Config()
        hass = mock.Mock(config=config)

        config_util.process_ha_core_config(hass, {})

        blankConfig = Config()
        assert config.latitude == blankConfig.latitude
        assert config.longitude == blankConfig.longitude
        assert config.elevation == blankConfig.elevation
        assert config.location_name == blankConfig.location_name
        assert config.units == blankConfig.units
        assert config.time_zone == blankConfig.time_zone
Esempio n. 2
0
    def test_discovering_configuration_auto_detect_fails(
            self, mock_detect, mock_elevation):
        """Test config remains unchanged if discovery fails."""
        self.hass.config = Config()

        run_coroutine_threadsafe(
            config_util.async_process_ha_core_config(self.hass, {}),
            self.hass.loop).result()

        blankConfig = Config()
        assert self.hass.config.latitude == blankConfig.latitude
        assert self.hass.config.longitude == blankConfig.longitude
        assert self.hass.config.elevation == blankConfig.elevation
        assert self.hass.config.location_name == blankConfig.location_name
        assert self.hass.config.units == blankConfig.units
        assert self.hass.config.time_zone == blankConfig.time_zone
Esempio n. 3
0
async def test_discovering_configuration_auto_detect_fails(
        mock_detect, mock_elevation, hass):
    """Test config remains unchanged if discovery fails."""
    hass.config = Config()
    hass.config.config_dir = "/test/config"

    await config_util.async_process_ha_core_config(hass, {})

    blankConfig = Config()
    assert hass.config.latitude == blankConfig.latitude
    assert hass.config.longitude == blankConfig.longitude
    assert hass.config.elevation == blankConfig.elevation
    assert hass.config.location_name == blankConfig.location_name
    assert hass.config.units == blankConfig.units
    assert hass.config.time_zone == blankConfig.time_zone
    assert len(hass.config.whitelist_external_dirs) == 1
    assert "/test/config/www" in hass.config.whitelist_external_dirs
Esempio n. 4
0
async def async_setup(hass: HomeAssistant, config: Config):
    """Read configuration from yaml."""

    ocpp_config = config.get(DOMAIN, {})
    if DOMAIN not in hass.data:
        hass.data[DOMAIN] = {}
    hass.data[DOMAIN][CONFIG] = ocpp_config
    _LOGGER.info(f"config = {ocpp_config}")
    return True
Esempio n. 5
0
async def async_setup(hass: HomeAssistant, config: Config):
    """AMS hub YAML setup."""
    if config.get(DOMAIN) is None:
        _LOGGER.info("No YAML config available, using config_entries")
        return True
    _setup(hass, config[DOMAIN])
    if not hass.config_entries.async_entries(DOMAIN):
        hass.async_create_task(
            hass.config_entries.flow.async_init(
                DOMAIN, context={"source":
                                 SOURCE_IMPORT}, data=config[DOMAIN]))
    return True
async def async_setup(hass: HomeAssistant, config: Config):
    _LOGGER.debug("Async setup meteo swiss")

    conf = config.get(DOMAIN)
    if conf is None:
        return True

    hass.async_create_task(
        hass.config_entries.flow.async_init(DOMAIN,
                                            context={"source": SOURCE_IMPORT},
                                            data=conf))
    _LOGGER.debug("END Async setup meteo swiss")
    return True
Esempio n. 7
0
async def async_setup(hass: HomeAssistant, config: Config):
    """Set up from config."""
    hass.data.setdefault(DOMAIN, {})

    await add_services(hass)

    # if there are no configuration.yaml settings then terminate
    if config.get(DOMAIN) is None:
        # We get her if the integration is set up using config flow
        return True

    conf = config.get(DOMAIN, {})
    hass.data.setdefault(DOMAIN_CONFIG_YAML, conf)

    if CONF_USERNAME in conf:
        # https://www.programcreek.com/python/?code=davesmeghead%2Fvisonic%2Fvisonic-master%2Fcustom_components%2Fvisonic%2F__init__.py
        # has there been a flow configured panel connection before
        configured = set(
            entry for entry in hass.config_entries.async_entries(DOMAIN))

        # if there is not a flow configured connection previously
        #   then create a flow connection from the configuration.yaml data
        if len(configured) == 0:
            # get the configuration.yaml settings and make a 'flow' task :)
            #   this will run 'async_step_import' in config_flow.py
            log.info(
                "Importing configuration from yaml...after you can remove from yaml"
            )
            hass.async_create_task(
                hass.config_entries.flow.async_init(
                    DOMAIN,
                    context={"source": config_entries.SOURCE_IMPORT},
                    data=conf.copy()))
        else:
            log.debug(
                "Configuration from yaml already imported: you can remove from yaml"
            )

    return True
Esempio n. 8
0
    def test_discovering_configuration(self, mock_detect, mock_elevation):
        """Test auto discovery for missing core configs."""
        config = Config()
        hass = mock.Mock(config=config)

        config_util.process_ha_core_config(hass, {})

        assert config.latitude == 32.8594
        assert config.longitude == -117.2073
        assert config.elevation == 101
        assert config.location_name == 'San Diego'
        assert config.temperature_unit == TEMP_FAHRENHEIT
        assert config.time_zone.zone == 'America/Los_Angeles'
Esempio n. 9
0
    def test_discovering_configuration(self, mock_detect, mock_elevation):
        """Test auto discovery for missing core configs."""
        config = Config()
        hass = mock.Mock(config=config)

        config_util.process_ha_core_config(hass, {})

        assert config.latitude == 32.8594
        assert config.longitude == -117.2073
        assert config.elevation == 101
        assert config.location_name == 'San Diego'
        assert config.units.name == CONF_UNIT_SYSTEM_METRIC
        assert config.units.is_metric
        assert config.time_zone.zone == 'America/Los_Angeles'
Esempio n. 10
0
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
    """Set up this component using YAML."""
    _LOGGER.info(STARTUP)
    if config.get(DOMAIN) is None:
        # We get her if the integration is set up using config flow
        return True

    try:
        await hass.config_entries.async_forward_entry(config, "sensor")
    except ValueError:
        pass

    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
        )
    )
    return True
Esempio n. 11
0
    def test_loading_configuration(self):
        """Test loading core config onto hass object."""
        config = Config()
        hass = mock.Mock(config=config)

        config_util.process_ha_core_config(hass, {
            'latitude': 60,
            'longitude': 50,
            'elevation': 25,
            'name': 'Huis',
            'temperature_unit': 'F',
            'time_zone': 'America/New_York',
        })

        assert config.latitude == 60
        assert config.longitude == 50
        assert config.elevation == 25
        assert config.location_name == 'Huis'
        assert config.temperature_unit == TEMP_FAHRENHEIT
        assert config.time_zone.zone == 'America/New_York'
Esempio n. 12
0
    def test_loading_configuration_temperature_unit(self):
        """Test backward compatibility when loading core config."""
        config = Config()
        hass = mock.Mock(config=config)

        config_util.process_ha_core_config(
            hass, {
                'latitude': 60,
                'longitude': 50,
                'elevation': 25,
                'name': 'Huis',
                CONF_TEMPERATURE_UNIT: 'C',
                'time_zone': 'America/New_York',
            })

        assert config.latitude == 60
        assert config.longitude == 50
        assert config.elevation == 25
        assert config.location_name == 'Huis'
        assert config.units.name == CONF_UNIT_SYSTEM_METRIC
        assert config.time_zone.zone == 'America/New_York'
Esempio n. 13
0
    def test_loading_configuration(self):
        """Test loading core config onto hass object."""
        config = Config()
        hass = mock.Mock(config=config)

        config_util.process_ha_core_config(
            hass, {
                'latitude': 60,
                'longitude': 50,
                'elevation': 25,
                'name': 'Huis',
                CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
                'time_zone': 'America/New_York',
            })

        assert config.latitude == 60
        assert config.longitude == 50
        assert config.elevation == 25
        assert config.location_name == 'Huis'
        assert config.units.name == CONF_UNIT_SYSTEM_IMPERIAL
        assert config.time_zone.zone == 'America/New_York'
Esempio n. 14
0
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
    """Set up this component using YAML."""
    async def handle_add_date(call: ServiceCall) -> None:
        """Handle the add_date service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            collection_date = call.data.get(const.CONF_DATE)
            _LOGGER.debug("called add_date %s from %s", collection_date,
                          entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.add_date(collection_date)
            except KeyError as err:
                _LOGGER.error("Failed adding date %s to %s (%s)",
                              collection_date, entity_id, err)

    async def handle_remove_date(call: ServiceCall) -> None:
        """Handle the remove_date service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            collection_date = call.data.get(const.CONF_DATE)
            _LOGGER.debug("called remove_date %s from %s", collection_date,
                          entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.remove_date(collection_date)
            except KeyError as err:
                _LOGGER.error(
                    "Failed removing date %s from %s. Most likely, "
                    "it was removed by competing automation runing in parallel. "
                    "(%s)",
                    collection_date,
                    entity_id,
                    err,
                )

    async def handle_offset_date(call: ServiceCall) -> None:
        """Handle the offset_date service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            offset = call.data.get(const.CONF_OFFSET)
            collection_date = call.data.get(const.CONF_DATE)
            _LOGGER.debug(
                "called offset_date %s by %d days for %s",
                collection_date,
                offset,
                entity_id,
            )
            try:
                new_date = collection_date + relativedelta(days=offset)
            except TypeError as err:
                _LOGGER.error("Failed to offset the date - %s", err)
                break
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.remove_date(collection_date)
                await entity.add_date(new_date)
            except KeyError as err:
                _LOGGER.error("Failed ofsetting date for %s - %s", entity_id,
                              err)

    async def handle_update_state(call: ServiceCall) -> None:
        """Handle the update_state service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            _LOGGER.debug("called update_state for %s", entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.async_update_state()
            except KeyError as err:
                _LOGGER.error("Failed updating state for %s - %s", entity_id,
                              err)

    async def handle_collect_garbage(call: ServiceCall) -> None:
        """Handle the collect_garbage service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            last_collection = call.data.get(const.ATTR_LAST_COLLECTION)
            _LOGGER.debug("called collect_garbage for %s", entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                if last_collection is None:
                    entity.last_collection = dt_util.now()
                else:
                    entity.last_collection = dt_util.as_local(last_collection)
                await entity.async_update_state()
            except KeyError as err:
                _LOGGER.error("Failed setting last collection for %s - %s",
                              entity_id, err)

    if const.DOMAIN not in hass.services.async_services():
        hass.services.async_register(
            const.DOMAIN,
            "collect_garbage",
            handle_collect_garbage,
            schema=COLLECT_NOW_SCHEMA,
        )
        hass.services.async_register(
            const.DOMAIN,
            "update_state",
            handle_update_state,
            schema=UPDATE_STATE_SCHEMA,
        )
        hass.services.async_register(const.DOMAIN,
                                     "add_date",
                                     handle_add_date,
                                     schema=ADD_REMOVE_DATE_SCHEMA)
        hass.services.async_register(
            const.DOMAIN,
            "remove_date",
            handle_remove_date,
            schema=ADD_REMOVE_DATE_SCHEMA,
        )
        hass.services.async_register(const.DOMAIN,
                                     "offset_date",
                                     handle_offset_date,
                                     schema=OFFSET_DATE_SCHEMA)
    else:
        _LOGGER.debug("Services already registered")

    if config.get(const.DOMAIN) is None:
        # We get here if the integration is set up using config flow
        return True

    platform_config = config[const.DOMAIN].get(const.CONF_SENSORS, {})
    # If platform is not enabled, skip.
    if not platform_config:
        return False

    for entry in hass.config_entries.async_entries(const.DOMAIN):
        if entry.source == SOURCE_IMPORT:
            _LOGGER.error("garbage_collection already imported. "
                          "Remove it from configuration.yaml now!")
            return True
    for entry in platform_config:
        _LOGGER.debug(
            "Importing %s(%s) from YAML configuration",
            entry[CONF_NAME],
            entry[const.CONF_FREQUENCY],
        )
        # Import YAML to ConfigFlow
        hass.async_create_task(
            hass.config_entries.flow.async_init(
                const.DOMAIN,
                context={"source": SOURCE_IMPORT},
                data=entry,
            ))
    return True