Esempio n. 1
0
def check_list(onecall: dict = None) -> dict:
    try:
        secrets = get_secrets()
        LOW_TEMP = secrets['LOW_TEMP']
        HIGH_WIND = secrets['HIGH_WIND_OPEN_WEATHER']
        if onecall is None:
            onecall = get_Open_Weather_JSON()
        is_current(onecall)
        next_fcst = get_next_forecast(onecall)
        current = onecall['current']

        checks = dict()

        # Check the current weather
        checks['current_has_low_temp'] = has_low_temp(current, LOW_TEMP)
        checks['current_has_rain'] = has_rain(current)
        checks['current_has_high_winds'] = has_high_winds(current, HIGH_WIND)
        checks['current_is_cloudy'] = is_cloudy(current, secrets['CLOUDS'])
        # Check the forecast
        checks['fcst_has_rain'] = has_rain(next_fcst)
        checks['fcst_has_high_winds'] = has_high_winds(next_fcst, HIGH_WIND)

        return checks

    except Exception as e:
        raise Exception(e)
class WindmanagerTest(unittest.TestCase):
    secrets = get_secrets()

    test_json_string_true = '[{"id":"0EGR2Z6Z4CS9QN8WWNR5TTQKR5","value":"' + str(
        secrets['HIGH_WIND_DIRECT_MEASUREMENT']
    ) + '","feed_id":1408032,"feed_key":"wind","created_at":"2020-08-08T09:27:04Z","created_epoch":1596878824,"expiration":"2020-09-07T09:27:04Z"},{"id":"0EGR2YPTHQKHT5H2M4M765H1QM","value":"0.208333","feed_id":1408032,"feed_key":"wind","created_at":"2020-08-08T09:26:12Z","created_epoch":1596878772,"expiration":"2020-09-07T09:26:12Z"},{"id":"0EGR2Y2BE8XE15G50TDYCZSJWD","value":"0.125","feed_id":1408032,"feed_key":"wind","created_at":"2020-08-08T09:25:04Z","created_epoch":1596878704,"expiration":"2020-09-07T09:25:04Z"}]'
    test_json_string_false = '[{"id":"0EGR2Z6Z4CS9QN8WWNR5TTQKR5","value":"' + str(
        0
    ) + '","feed_id":1408032,"feed_key":"wind","created_at":"2020-08-08T09:27:04Z","created_epoch":1596878824,"expiration":"2020-09-07T09:27:04Z"},{"id":"0EGR2YPTHQKHT5H2M4M765H1QM","value":"0.208333","feed_id":1408032,"feed_key":"wind","created_at":"2020-08-08T09:26:12Z","created_epoch":1596878772,"expiration":"2020-09-07T09:26:12Z"},{"id":"0EGR2Y2BE8XE15G50TDYCZSJWD","value":"0.125","feed_id":1408032,"feed_key":"wind","created_at":"2020-08-08T09:25:04Z","created_epoch":1596878704,"expiration":"2020-09-07T09:25:04Z"}]'

    def test_closing(self):
        self.assertTrue(
            check_array_high_wind(
                [0, 0, 0, self.secrets['HIGH_WIND_DIRECT_MEASUREMENT']]))
        self.assertFalse(
            check_array_high_wind(
                [0, 0, 0, self.secrets['HIGH_WIND_DIRECT_MEASUREMENT'] - 1]))

    def test_25_min(self):
        self.assertTrue(
            check_last_25_minutes_had_high_winds(
                test_json_string=self.test_json_string_true))
        self.assertFalse(
            check_last_25_minutes_had_high_winds(
                test_json_string=self.test_json_string_false))
def should_sunscreen_open(solar_noon):
    secrets = get_secrets()
    current_azimuth = solar_azimuth_compass(
        solar_noon, float(secrets['LAT']) * DEG_TO_RAD)

    checks = list()

    # Check the current weather
    checks.append(sun_not_on_window(current_azimuth,
                                    secrets['AZIMUTH_SUNUP'], secrets['AZIMUTH_SUNDOWN']))

    return not any(checks)
Esempio n. 4
0
def post_to_adafruit(feed, value):
    try:
        secrets = get_secrets()
        adafruitFeed = secrets['ADAFRUIT_IO_FEEDS_URL'] + f"{feed}/data"
        headers = {
            'X-AIO-Key': secrets['ADAFRUIT_IO_KEY'],
            "Content-Type": "application/json"
        }
        payload = {'value': value}

        r = requests.post(adafruitFeed, json=payload, headers=headers)

    except:
        pass
Esempio n. 5
0
def get_Open_Weather_JSON(typeString="onecall") -> dict:
    """Gets the JSON response from openweathermap. Types are forecast, weather, onecall. Defauls to onecall."""
    secrets = get_secrets()
    openWeatherAPIurl = "https://api.openweathermap.org/data/2.5/"+typeString+"?lat=" + \
        secrets['LAT'] + "&lon=" + secrets['LON'] + \
        "&appid=" + secrets['OPENWEATHERMAP_ORG_KEY']
    r = requests.get(openWeatherAPIurl)
    json_buffer = r.json()
    if secrets['LOGGING']:
        write_json(json_buffer, 'most_recent.json')
    if(is_current(json_buffer)):
        return json_buffer
    else:
        raise Exception(
            'JSON data was not current in get_Open_Weather_JSON'
        )
def get_open_percentage_required(solar_noon: float, latitude: float = None, reference_time=None, adjustment=0) -> int:
    """Retuns the percentage that the sunscreen should open"""
    if latitude is None:
        secrets = get_secrets()
        latitude = float(secrets['LAT']) * DEG_TO_RAD
    elevation_angle = 0.5*pi - \
        acos(cosine_of_solar_zenith(solar_noon, latitude, reference_time))
    azimuth_deg = solar_azimuth_compass(solar_noon, latitude, reference_time)
    house_incidence_angle_rad = incidence_angle(
        azimuth_deg, secrets['HOUSE_FACING_DEG'])

    result = open_percentage_required(
        elevation_angle, house_incidence_angle_rad, adjustment)
    print(result)
    # If we are in mode 1, take bigger steps
    if (secrets['CURRENT_MODE'] == 1):
        result = round_to_nearest(result, 50)
    print(result)
    return result
Esempio n. 7
0
class OpenPosCalcTest(unittest.TestCase):

    secrets = get_secrets()

    def test_open_pos(self):
        self.assertEqual(
            int(24.46524406),
            open_percentage_required(49.83 * DEG_TO_RAD, -1.158375025))

    def test_open_pos_with_adjustment(self):
        self.assertEqual(
            int(24.46524406) + 10,
            open_percentage_required(49.83 * DEG_TO_RAD,
                                     -1.158375025,
                                     adjustment=10))

    def test_round_to_nearest(self):
        self.assertEqual(0, round_to_nearest(0, 50))
        self.assertNotEqual(0, round_to_nearest(1, 50))
        self.assertEqual(50, round_to_nearest(1, 50))
        self.assertEqual(50, round_to_nearest(50, 50))
        self.assertEqual(100, round_to_nearest(51, 50))