Ejemplo n.º 1
0
async def validate_host(hass: HomeAssistant,
                        host: str) -> tuple[str, FroniusConfigEntryData]:
    """Validate the user input allows us to connect.

    Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
    """
    fronius = Fronius(async_get_clientsession(hass), host)

    try:
        datalogger_info: dict[str, Any]
        datalogger_info = await fronius.current_logger_info()
    except FroniusError as err:
        _LOGGER.debug(err)
    else:
        logger_uid: str = datalogger_info["unique_identifier"]["value"]
        return logger_uid, FroniusConfigEntryData(
            host=host,
            is_logger=True,
        )
    # Gen24 devices don't provide GetLoggerInfo
    try:
        inverter_info = await fronius.inverter_info()
        first_inverter = next(inverter
                              for inverter in inverter_info["inverters"])
    except FroniusError as err:
        _LOGGER.debug(err)
        raise CannotConnect from err
    except StopIteration as err:
        raise CannotConnect(
            "No supported Fronius SolarNet device found.") from err
    first_inverter_uid: str = first_inverter["unique_id"]["value"]
    return first_inverter_uid, FroniusConfigEntryData(
        host=host,
        is_logger=False,
    )
Ejemplo n.º 2
0
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Set up of Fronius platform."""
    session = async_get_clientsession(hass)
    fronius = Fronius(session, config[CONF_RESOURCE])

    sensors = []
    for condition in config[CONF_MONITORED_CONDITIONS]:

        device = condition[CONF_DEVICE]
        name = "Fronius {} {} {}".format(
            condition[CONF_SENSOR_TYPE].replace("_", " ").capitalize(),
            device,
            config[CONF_RESOURCE],
        )
        sensor_type = condition[CONF_SENSOR_TYPE]
        scope = condition[CONF_SCOPE]
        if sensor_type == TYPE_INVERTER:
            if scope == SCOPE_SYSTEM:
                sensor_cls = FroniusInverterSystem
            else:
                sensor_cls = FroniusInverterDevice
        elif sensor_type == TYPE_METER:
            if scope == SCOPE_SYSTEM:
                sensor_cls = FroniusMeterSystem
            else:
                sensor_cls = FroniusMeterDevice
        elif sensor_type == TYPE_POWER_FLOW:
            sensor_cls = FroniusPowerFlow
        else:
            sensor_cls = FroniusStorage

        sensors.append(sensor_cls(fronius, name, device))

    async_add_entities(sensors, True)
Ejemplo n.º 3
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up fronius from a config entry."""
    host = entry.data[CONF_HOST]
    fronius = Fronius(async_get_clientsession(hass), host)
    solar_net = FroniusSolarNet(hass, entry, fronius)
    await solar_net.init_devices()

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = solar_net
    hass.config_entries.async_setup_platforms(entry, PLATFORMS)
    return True
Ejemplo n.º 4
0
async def async_setup_platform(hass,
                               config,
                               async_add_entities,
                               discovery_info=None):
    """Set up of Fronius platform."""
    session = async_get_clientsession(hass)
    fronius = Fronius(session, config[CONF_RESOURCE])

    scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
    adapters = []
    # Creates all adapters for monitored conditions
    for condition in config[CONF_MONITORED_CONDITIONS]:

        device = condition[CONF_DEVICE]
        sensor_type = condition[CONF_SENSOR_TYPE]
        scope = condition[CONF_SCOPE]
        name = "Fronius {} {} {}".format(
            condition[CONF_SENSOR_TYPE].replace("_", " ").capitalize(),
            device if scope == SCOPE_DEVICE else SCOPE_SYSTEM,
            config[CONF_RESOURCE],
        )
        if sensor_type == TYPE_INVERTER:
            if scope == SCOPE_SYSTEM:
                adapter_cls = FroniusInverterSystem
            else:
                adapter_cls = FroniusInverterDevice
        elif sensor_type == TYPE_METER:
            if scope == SCOPE_SYSTEM:
                adapter_cls = FroniusMeterSystem
            else:
                adapter_cls = FroniusMeterDevice
        elif sensor_type == TYPE_POWER_FLOW:
            adapter_cls = FroniusPowerFlow
        else:
            adapter_cls = FroniusStorage

        adapters.append(adapter_cls(fronius, name, device, async_add_entities))

    # Creates a lamdba that fetches an update when called
    def adapter_data_fetcher(data_adapter):
        async def fetch_data(*_):
            await data_adapter.async_update()

        return fetch_data

    # Set up the fetching in a fixed interval for each adapter
    for adapter in adapters:
        fetch = adapter_data_fetcher(adapter)
        # fetch data once at set-up
        await fetch()
        async_track_time_interval(hass, fetch, scan_interval)
Ejemplo n.º 5
0
 def test_error_reason(self):
     res = web_state.GET_INVERTER_REALTIME_DATA_SCOPE_DEVICE
     self.assertEqual(Fronius.error_reason(res), "")
     res = web_state.GET_STORAGE_REALTIME_DATA_SCOPE_DEVICE
     self.assertEqual(Fronius.error_reason(res),
                      "Storages are not supported")
Ejemplo n.º 6
0
 def test_error_code(self):
     res = web_state.GET_INVERTER_REALTIME_DATA_SCOPE_DEVICE
     self.assertEqual(Fronius.error_code(res), 0)
     res = web_state.GET_STORAGE_REALTIME_DATA_SCOPE_DEVICE
     self.assertEqual(Fronius.error_code(res), 255)