pyopenweathermap 0.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyopenweathermap-0.0.2/LICENSE +21 -0
- pyopenweathermap-0.0.2/PKG-INFO +20 -0
- pyopenweathermap-0.0.2/README.md +3 -0
- pyopenweathermap-0.0.2/pyproject.toml +25 -0
- pyopenweathermap-0.0.2/src/pyopenweathermap/__init__.py +9 -0
- pyopenweathermap-0.0.2/src/pyopenweathermap/exception.py +10 -0
- pyopenweathermap-0.0.2/src/pyopenweathermap/owm_client.py +71 -0
- pyopenweathermap-0.0.2/src/pyopenweathermap/weather.py +93 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Evgeny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyopenweathermap
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: lib for OpenWeatherMap for Home Assistant
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: homeassistant,owm,openweathermap
|
|
7
|
+
Author: Evgeny
|
|
8
|
+
Author-email: iam@freekode.org
|
|
9
|
+
Requires-Python: >=3.11,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Requires-Dist: aiohttp (>=3.9.5,<4.0.0)
|
|
14
|
+
Requires-Dist: pydantic (>=2.7.1,<3.0.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# pyopenweathermap
|
|
18
|
+
|
|
19
|
+
Python library for OpenWeatherMap API for Home Assistant
|
|
20
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "pyopenweathermap"
|
|
3
|
+
version = "0.0.2"
|
|
4
|
+
description = "lib for OpenWeatherMap for Home Assistant"
|
|
5
|
+
authors = ["Evgeny <iam@freekode.org>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
keywords = ["homeassistant", "owm", "openweathermap"]
|
|
9
|
+
packages = [
|
|
10
|
+
{ include = "pyopenweathermap", from = "src" },
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[tool.poetry.dependencies]
|
|
14
|
+
python = "^3.11"
|
|
15
|
+
aiohttp = "^3.9.5"
|
|
16
|
+
pydantic = "^2.7.1"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
[tool.poetry.group.dev.dependencies]
|
|
20
|
+
pytest = "^8.1.1"
|
|
21
|
+
pytest-asyncio = "^0.23.6"
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["poetry-core"]
|
|
25
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from aiohttp import ClientSession
|
|
2
|
+
|
|
3
|
+
from .exception import RequestError, UnauthorizedError, TooManyRequestsError
|
|
4
|
+
from .weather import WeatherReport, CurrentWeather, HourlyWeatherForecast, DailyWeatherForecast
|
|
5
|
+
|
|
6
|
+
API_URL = 'https://api.openweathermap.org/data/3.0/onecall'
|
|
7
|
+
WEATHER_TYPES = {'current', 'minutely', 'hourly', 'daily', 'alerts'}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class OWMClient:
|
|
11
|
+
session: ClientSession | None = None
|
|
12
|
+
request_timeout: int = 10
|
|
13
|
+
|
|
14
|
+
def __init__(self, api_key, units="metric", lang='en'):
|
|
15
|
+
self.api_key = api_key
|
|
16
|
+
self.units = units
|
|
17
|
+
self.lang = lang
|
|
18
|
+
|
|
19
|
+
async def get_weather(self, lat, lon, weather_types=None) -> WeatherReport:
|
|
20
|
+
if weather_types is None:
|
|
21
|
+
exclude_weather_types = {}
|
|
22
|
+
else:
|
|
23
|
+
exclude_weather_types = WEATHER_TYPES - set(weather_types)
|
|
24
|
+
|
|
25
|
+
url = self._get_url(lat, lon, exclude_weather_types)
|
|
26
|
+
json_response = await self._request(url)
|
|
27
|
+
|
|
28
|
+
current, hourly, daily = None, None, None
|
|
29
|
+
if json_response.get('current') is not None:
|
|
30
|
+
current = CurrentWeather(**json_response['current'])
|
|
31
|
+
if json_response.get('hourly') is not None:
|
|
32
|
+
hourly = [HourlyWeatherForecast(**item) for item in json_response['hourly']]
|
|
33
|
+
if json_response.get('daily') is not None:
|
|
34
|
+
daily = [DailyWeatherForecast(**item) for item in json_response['daily']]
|
|
35
|
+
|
|
36
|
+
return WeatherReport(current, hourly, daily)
|
|
37
|
+
|
|
38
|
+
async def validate_key(self) -> bool:
|
|
39
|
+
url = self._get_url(50.06, 14.44, WEATHER_TYPES)
|
|
40
|
+
try:
|
|
41
|
+
await self._request(url)
|
|
42
|
+
return True
|
|
43
|
+
except UnauthorizedError:
|
|
44
|
+
return False
|
|
45
|
+
|
|
46
|
+
async def _request(self, url):
|
|
47
|
+
print(url)
|
|
48
|
+
async with ClientSession() as session:
|
|
49
|
+
try:
|
|
50
|
+
async with session.get(url=url, timeout=self.request_timeout) as response:
|
|
51
|
+
if response.status == 200:
|
|
52
|
+
return await response.json()
|
|
53
|
+
elif response.status == 401:
|
|
54
|
+
raise UnauthorizedError
|
|
55
|
+
elif response.status == 404:
|
|
56
|
+
raise RequestError("Not Found")
|
|
57
|
+
elif response.status == 429:
|
|
58
|
+
raise TooManyRequestsError
|
|
59
|
+
else:
|
|
60
|
+
raise RequestError("Unknown Error")
|
|
61
|
+
except TimeoutError:
|
|
62
|
+
raise RequestError("Request timeout")
|
|
63
|
+
|
|
64
|
+
def _get_url(self, lat, lon, exclude):
|
|
65
|
+
return (f"{API_URL}?"
|
|
66
|
+
f"lat={lat}&"
|
|
67
|
+
f"lon={lon}&"
|
|
68
|
+
f"exclude={','.join(exclude)}&"
|
|
69
|
+
f"appid={self.api_key}&"
|
|
70
|
+
f"units={self.units}&"
|
|
71
|
+
f"lang={self.lang}")
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import decimal
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DailyTemperature(BaseModel):
|
|
9
|
+
day: decimal.Decimal
|
|
10
|
+
min: decimal.Decimal = None
|
|
11
|
+
max: decimal.Decimal = None
|
|
12
|
+
night: decimal.Decimal
|
|
13
|
+
evening: decimal.Decimal = Field(alias='eve')
|
|
14
|
+
morning: decimal.Decimal = Field(alias='morn')
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class WeatherCondition(BaseModel):
|
|
18
|
+
id: int
|
|
19
|
+
main: str
|
|
20
|
+
description: str
|
|
21
|
+
icon: str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class CurrentWeather(BaseModel):
|
|
25
|
+
date_time: datetime = Field(alias='dt')
|
|
26
|
+
temperature: decimal.Decimal = Field(alias='temp')
|
|
27
|
+
feels_like: decimal.Decimal
|
|
28
|
+
pressure: int
|
|
29
|
+
humidity: int
|
|
30
|
+
dew_point: decimal.Decimal
|
|
31
|
+
uv_index: decimal.Decimal = Field(alias='uvi')
|
|
32
|
+
clouds: int
|
|
33
|
+
visibility: int
|
|
34
|
+
wind_speed: decimal.Decimal
|
|
35
|
+
wind_gust: decimal.Decimal = None
|
|
36
|
+
wind_deg: int
|
|
37
|
+
rain: dict = {}
|
|
38
|
+
snow: dict = {}
|
|
39
|
+
condition: list[WeatherCondition] = Field(alias='weather')
|
|
40
|
+
|
|
41
|
+
def get_weather(self):
|
|
42
|
+
return self.condition[0]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class HourlyWeatherForecast(BaseModel):
|
|
46
|
+
date_time: datetime = Field(alias='dt')
|
|
47
|
+
temperature: decimal.Decimal = Field(alias='temp')
|
|
48
|
+
feels_like: decimal.Decimal
|
|
49
|
+
pressure: int
|
|
50
|
+
humidity: int
|
|
51
|
+
dew_point: decimal.Decimal
|
|
52
|
+
uv_index: decimal.Decimal = Field(alias='uvi')
|
|
53
|
+
clouds: int
|
|
54
|
+
visibility: int
|
|
55
|
+
wind_speed: decimal.Decimal
|
|
56
|
+
wind_gust: decimal.Decimal = None
|
|
57
|
+
wind_deg: int
|
|
58
|
+
precipitation_probability: decimal.Decimal = Field(alias='pop', default=0)
|
|
59
|
+
rain: dict = {}
|
|
60
|
+
snow: dict = {}
|
|
61
|
+
condition: list[WeatherCondition] = Field(alias='weather')
|
|
62
|
+
|
|
63
|
+
def get_weather(self):
|
|
64
|
+
return self.condition[0]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class DailyWeatherForecast(BaseModel):
|
|
68
|
+
date_time: datetime = Field(alias='dt')
|
|
69
|
+
summary: str
|
|
70
|
+
temperature: DailyTemperature = Field(alias='temp')
|
|
71
|
+
feels_like: DailyTemperature
|
|
72
|
+
pressure: int
|
|
73
|
+
humidity: int
|
|
74
|
+
dew_point: decimal.Decimal
|
|
75
|
+
uv_index: decimal.Decimal = Field(alias='uvi')
|
|
76
|
+
clouds: int
|
|
77
|
+
wind_speed: decimal.Decimal
|
|
78
|
+
wind_gust: decimal.Decimal = None
|
|
79
|
+
wind_deg: int
|
|
80
|
+
precipitation_probability: decimal.Decimal = Field(alias='pop', default=0)
|
|
81
|
+
rain: decimal.Decimal = 0
|
|
82
|
+
snow: decimal.Decimal = 0
|
|
83
|
+
condition: list[WeatherCondition] = Field(alias='weather')
|
|
84
|
+
|
|
85
|
+
def get_weather(self):
|
|
86
|
+
return self.condition[0]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class WeatherReport:
|
|
91
|
+
current: CurrentWeather
|
|
92
|
+
hourly_forecast: list[HourlyWeatherForecast]
|
|
93
|
+
daily_forecast: list[DailyWeatherForecast]
|