예제 #1
0
def test_bad_input():
    assert get_covid_19_cases(
        123) == "Error city name not valid or the UK Covid19 api is offline!"
예제 #2
0
def test_wrong_city():
    assert get_covid_19_cases(
        "odosfsdo"
    ) == "Error city name not valid or the UK Covid19 api is offline!"
예제 #3
0
def run_alarm(alarm_label: str, news: bool = False, weather: bool = False) -> None:
    '''
    When an alarm is run we set off a noise aswell as showing
    any updates we need in news or weather
    '''

    text_to_read = []
    title = ""
    alarm_active = False

    # Find the alarm in the ALARMS list and remove it
    for alarm in ALARMS:
        if alarm['title'] == alarm_label:
            ALARMS.remove(alarm)
            alarm_active = True
            continue

    # If we can't find the alarm then return an error
    if not alarm_active:
        # Return error to log only, error alarm doesn't exist
        write_to_log(
            "ERROR TECH - No alarm found", False)
        return index

    write_to_log(
        "ALARM - Alarm: " + alarm_label + " has rung")
    # Add the alarm too the notifcation pannel
    title = "Alarm - " + alarm_label
    NOTIFICATIONS.append({"title": title, "content": (
        "Alarm " + alarm_label + " has rung")})

    # Add the alarm to the text to speech list
    text_to_read.append(("Alarm " + alarm_label + " has rung!"))

    # If news is enabled show the news and if Covid brief enabled show this too
    if news:
        # Same logic as the earlier brieifing just adds
        # the content too the text too speech
        text_to_read.append("News Update")

        # Make sure to still run if an api error is caught
        try:
            news_update, covid_update = get_news(
                CURRENT_COUNTRY, NEWS_KEY, NEWS_PROVIDER_PREFERENCES)
        except ValueError as e:
            message = news_update, covid_update = get_news(
                CURRENT_COUNTRY, NEWS_KEY, NEWS_PROVIDER_PREFERENCES)
            write_to_log(message)
            text_to_read.append(message)
            # Read out all loaded text so far
            tts_request(text_to_read)
            LOGGED_ERRORS.append(
                message)
            NOTIFICATIONS.append(
                {"title": ("Error - UID:" + str(len(LOGGED_ERRORS))), "content": message})
            # Save the NOTIFICATIONS and the removed alarm too the config
            write_config()
            return index

        for article in news_update:
            title = "News Update - " + \
                str(datetime.now().strftime("%d/%m/%Y %H:%M")) +\
                " - Story " + str(news_update.index(article) + 1)

            NOTIFICATIONS.append({"title": title, "content": article})

            text_to_read.append(article)

        if COVID_BRIEF:
            text_to_read.append("COVID-19 Update")

            # Make sure to run through even if the api is down or we have wrong information
            try:
                total_cases, cases_today = get_covid_19_cases(CURRENT_CITY)
            except ValueError as e:
                message = get_covid_19_cases(CURRENT_CITY)
                write_to_log(message)
                text_to_read.append(message)
                # Read out all loaded text so far
                tts_request(text_to_read)
                LOGGED_ERRORS.append(
                    message)
                NOTIFICATIONS.append(
                    {"title": ("Error - UID:" + str(len(LOGGED_ERRORS))), "content": message})
                # Save the NOTIFICATIONS and the removed alarm too the config
                write_config()
                return index

            message = "Covid-19 cases today in " + CURRENT_CITY + " " + \
                str(cases_today) + " giving a cumulative total of " + \
                str(total_cases) + " in " + CURRENT_CITY
            text_to_read.append(message)

            NOTIFICATIONS.append({"title": "COVID Statistics Update - " + str(
                datetime.now().strftime("%d/%m/%Y %H:%M")), "content": message})

            for covid_article in covid_update:
                title = "COVID News Update - " + \
                    str(datetime.now().strftime("%d/%m/%Y %H:%M")) +\
                    " - Story " + str(covid_update.index(covid_article) + 1)

                NOTIFICATIONS.append(
                    {"title": title, "content": covid_article})

                text_to_read.append(covid_article)

    # If weather brief is on then display the weather too
    if weather:
        # Same logic as the earlier brieifing just adds
        # the content too the text too speech
        text_to_read.append("Weather Update")

        # We make sure that there is a correct api key or city set otherwise we call an error
        try:
            current_temperature, current_pressure, current_humidiy, \
                weather_description = get_weather(CURRENT_CITY, WEATHER_KEY)
        except ValueError as e:
            message = get_weather(CURRENT_CITY, WEATHER_KEY)
            write_to_log(message)
            LOGGED_ERRORS.append(
                message)
            text_to_read.append(message)
            # Read out all loaded text so far
            tts_request(text_to_read)
            NOTIFICATIONS.append(
                {"title": ("Error - UID:" + str(len(LOGGED_ERRORS))), "content": message})
            # Save the NOTIFICATIONS and the removed alarm too the config
            write_config()
            return index

        NOTIFICATIONS.append(
            {"title": ("Weather Update - " +
                       str(datetime.now().strftime("%d/%m/%Y %H:%M"))),
             "content": ("Temperature (in degrees): " +
                         str(int(current_temperature) - 273) +
                         "\n | Atmospheric pressure (in hPa unit): " +
                         str(current_pressure) +
                         "\n | Humidity (as a percentage): " +
                         str(current_humidiy) +
                         "\n | Current weather: " +
                         str(weather_description))})
        text_to_read.append(("Temperature (in degrees) | " +
                             str(int(current_temperature) - 273) +
                             "\n | Atmospheric pressure (in hPa unit) | " +
                             str(current_pressure) +
                             "\n | Humidity (as a percentage) | " +
                             str(current_humidiy) +
                             "\n | Current weather | " +
                             str(weather_description)))

    # Save the NOTIFICATIONS and the removed alarm too the config
    write_config()

    # Read out the alarm aswell as any annoucements we added
    tts_request(text_to_read)

    # Refresh the main page
    return index
예제 #4
0
def test_good_input():
    assert get_covid_19_cases(
        "London"
    ) != "Error city name not valid or the UK Covid19 api is offline!"
예제 #5
0
def run_briefing(time_select: str, date_time) -> None:
    '''
    When a briefing time is reched we get the current news stories
    and weather aswell as covid-19 stories if this enabled by the
    user and display them silently in the NOTIFICATIONS
    '''

    # Load in the news and covid stories from the news api
    try:
        news_update, covid_update = get_news(
            CURRENT_COUNTRY, NEWS_KEY, NEWS_PROVIDER_PREFERENCES)
    except ValueError as e:
        message = get_news(
            CURRENT_COUNTRY, NEWS_KEY, NEWS_PROVIDER_PREFERENCES)
        write_to_log(message)
        LOGGED_ERRORS.append(
            message)
        NOTIFICATIONS.append(
            {"title": ("Error - UID:" + str(len(LOGGED_ERRORS))), "content": message})
        # Save the NOTIFICATIONS and the removed alarm too the config
        write_config()
        return index

    # Loop through all the news stories
    for article in news_update:
        # Add the news story as content along with a custom title
        # (effectively a UID for the notification) to the notification pannel
        title = "News Update - " + \
                str(datetime.now().strftime("%d/%m/%Y %H:%M")) +\
            " - Story " + str(news_update.index(article) + 1)
        NOTIFICATIONS.append({"title": title, "content": article})

    # If the user has enabled Covid-19 updates in their config (On by default)
    if COVID_BRIEF:

        # Make sure to run through even if the api is down or we have wrong information
        try:
            # Load in the daily covid cases for the current/nearest city
            total_cases, cases_today = get_covid_19_cases(CURRENT_CITY)
        except ValueError as e:
            message = get_covid_19_cases(CURRENT_CITY)
            write_to_log(message)
            LOGGED_ERRORS.append(
                message)
            NOTIFICATIONS.append(
                {"title": ("Error - UID:" + str(len(LOGGED_ERRORS))), "content": message})
            # Save the NOTIFICATIONS and the removed alarm too the config
            write_config()
            return index

        # Then create a message to show the covid cases for the day
        # aswell as the total covid cases
        message = "Covid-19 cases today in " + CURRENT_CITY + " " + \
            str(cases_today) + " giving a cumulative total of " + \
            str(total_cases) + " in " + CURRENT_CITY

        # Add this message along with a custom
        # dated title too the NOTIFICATIONS
        NOTIFICATIONS.append({"title": "COVID Statistics Update - " + str(
            datetime.now().strftime("%d/%m/%Y %H:%M")), "content": message})

        # Then loop through the covid-19 news articles
        for covid_article in covid_update:

            # Give the article a title using the date and time as UID
            title = "COVID News Update - " + \
                str(datetime.now().strftime("%d/%m/%Y %H:%M")) +\
                " - Story " + str(covid_update.index(covid_article) + 1)

            NOTIFICATIONS.append(
                {"title": title, "content": covid_article})

    # Run the get_weather to open weather api
    # to get the current weather conditions
    try:
        current_temperature, current_pressure, current_humidiy, \
            weather_description = get_weather(CURRENT_CITY, WEATHER_KEY)
    except ValueError as e:
        message = get_weather(CURRENT_CITY, WEATHER_KEY)
        write_to_log(message)
        LOGGED_ERRORS.append(
            message)

        NOTIFICATIONS.append(
            {"title": ("Error - UID:" + str(len(LOGGED_ERRORS))), "content": message})
        # Save the NOTIFICATIONS and the removed alarm too the config
        write_config()
        return index

    # Add the weather to the NOTIFICATIONS
    NOTIFICATIONS.append(
        {"title": ("Weather Update - " +
                   str(datetime.now().strftime("%d/%m/%Y %H:%M"))),
         "content": ("Temperature (in degrees): " +
                     str(int(current_temperature) - 273) +
                     "\n | Atmospheric pressure (in hPa unit): " +
                     str(current_pressure) +
                     "\n | Humidity (as a percentage): " +
                     str(current_humidiy) +
                     "\n | Current weather: " +
                     str(weather_description))})

    # Add a day too the current briefing time
    date_time = date_time + timedelta(days=1)

    # Calculate the time too the next briefing
    # and then re-add it too the scheduler
    time_to_alarm = (date_time - datetime.now()).total_seconds()
    S.enter(time_to_alarm, 1, run_briefing, (time_select, date_time))

    # Refresh the page
    return index