juham-visualcrossing 0.1.8__tar.gz → 0.1.9__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.
Files changed (19) hide show
  1. {juham_visualcrossing-0.1.8/juham_visualcrossing.egg-info → juham_visualcrossing-0.1.9}/PKG-INFO +5 -27
  2. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing/visualcrossing.py +17 -14
  3. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing/visualcrossing_plugin.py +0 -1
  4. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9/juham_visualcrossing.egg-info}/PKG-INFO +5 -27
  5. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing.egg-info/requires.txt +1 -1
  6. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/pyproject.toml +4 -4
  7. juham_visualcrossing-0.1.9/tests/test_visualcrossing.py +217 -0
  8. juham_visualcrossing-0.1.8/tests/test_visualcrossing.py +0 -16
  9. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/LICENSE.rst +0 -0
  10. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/MANIFEST.in +0 -0
  11. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/README.rst +0 -0
  12. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing/__init__.py +0 -0
  13. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing/py.typed +0 -0
  14. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing.egg-info/SOURCES.txt +0 -0
  15. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing.egg-info/dependency_links.txt +0 -0
  16. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing.egg-info/entry_points.txt +0 -0
  17. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/juham_visualcrossing.egg-info/top_level.txt +0 -0
  18. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/setup.cfg +0 -0
  19. {juham_visualcrossing-0.1.8 → juham_visualcrossing-0.1.9}/tests/__init__.py +0 -0
@@ -1,32 +1,10 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: juham-visualcrossing
3
- Version: 0.1.8
3
+ Version: 0.1.9
4
4
  Summary: A Weather forecast plugin extending `Juham` applications
5
5
  Author-email: J Meskanen <juham.api@gmail.com>
6
6
  Maintainer-email: "J. Meskanen" <juham.api@gmail.com>
7
- License: MIT License
8
- ===========
9
-
10
- Copyright (c) 2024, Juha Meskanen
11
-
12
- Permission is hereby granted, free of charge, to any person obtaining a copy
13
- of this software and associated documentation files (the "Software"), to deal
14
- in the Software without restriction, including without limitation the rights
15
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
- copies of the Software, and to permit persons to whom the Software is
17
- furnished to do so, subject to the following conditions:
18
-
19
- The above copyright notice and this permission notice shall be included in all
20
- copies or substantial portions of the Software.
21
-
22
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
-
29
-
7
+ License-Expression: MIT
30
8
  Project-URL: Homepage, https://gitlab.com/juham/juham/juham-visualcrossing
31
9
  Project-URL: Bug Reports, https://gitlab.com/juham/juham/juham-visualcrossing
32
10
  Project-URL: Funding, https://meskanen.com
@@ -36,15 +14,15 @@ Keywords: object-oriented,plugin,framework
36
14
  Classifier: Development Status :: 3 - Alpha
37
15
  Classifier: Intended Audience :: Developers
38
16
  Classifier: Topic :: Software Development
39
- Classifier: License :: OSI Approved :: MIT License
40
17
  Classifier: Programming Language :: Python :: 3.8
41
18
  Requires-Python: >=3.8
42
19
  Description-Content-Type: text/x-rst
43
20
  License-File: LICENSE.rst
44
- Requires-Dist: juham-core>=0.1.1
21
+ Requires-Dist: juham-core>=0.1.5
45
22
  Requires-Dist: pytz>=2024.1
46
23
  Provides-Extra: dev
47
24
  Requires-Dist: check-manifest; extra == "dev"
25
+ Dynamic: license-file
48
26
 
49
27
  VisualCrossing forecast plugin for Juham™
50
28
  =========================================
@@ -2,7 +2,7 @@ from datetime import datetime, timedelta, timezone
2
2
  import json
3
3
  from typing_extensions import override
4
4
  from typing import Any, Optional, cast
5
- from masterpiece.mqtt import MqttMsg, Mqtt
5
+ from masterpiece import MqttMsg, Mqtt
6
6
  from juham_core import JuhamThread, JuhamCloudThread
7
7
 
8
8
 
@@ -68,7 +68,7 @@ class VisualCrossingThread(JuhamCloudThread):
68
68
  def process_data(self, data: Any) -> None:
69
69
  self.info("VisualCrossing process_data()")
70
70
  data = data.json()
71
- forecast = []
71
+ forecast: list[dict[str, Any]] = []
72
72
  self.info(f"VisualCrossing {data}")
73
73
  for day in data["days"]:
74
74
  for hour in day["hours"]:
@@ -113,6 +113,8 @@ class VisualCrossing(JuhamThread):
113
113
  update_interval.
114
114
  """
115
115
 
116
+ _VISUALCROSSING: str = "visualcrossing"
117
+
116
118
  workerThreadId: str = VisualCrossingThread.get_class_id()
117
119
  base_url: str = (
118
120
  "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/"
@@ -121,7 +123,7 @@ class VisualCrossing(JuhamThread):
121
123
  api_key: str = "SE9W7EHP775N7NDNW8ANM2MZN"
122
124
  location: str = "lahti,finland"
123
125
 
124
- def __init__(self, name="visualcrossing") -> None:
126
+ def __init__(self, name: str = "visualcrossing") -> None:
125
127
  """Constructs VisualCrossing automation object for acquiring and publishing
126
128
  forecast data.
127
129
 
@@ -148,11 +150,11 @@ class VisualCrossing(JuhamThread):
148
150
  else:
149
151
  super().on_message(client, userdata, msg)
150
152
 
151
- def on_forecast(self, em: dict) -> None:
153
+ def on_forecast(self, em: dict[str, Any]) -> None:
152
154
  """Handle weather forecast data.
153
155
 
154
156
  Args:
155
- em (dict): forecast
157
+ em (dict[str, Any]): forecast
156
158
  """
157
159
  # self.debug(f"VisualCrossing: got mqtt message {em}")
158
160
 
@@ -176,19 +178,20 @@ class VisualCrossing(JuhamThread):
176
178
  super().run()
177
179
 
178
180
  @override
179
- def to_dict(self) -> dict:
180
- data = super().to_dict()
181
- data["_visualcrossing"] = {
182
- "topic": self.forecast_topic,
183
- "url": self.base_url,
181
+ def to_dict(self) -> dict[str, Any]:
182
+ data: dict[str, dict[str, Any]] = super().to_dict()
183
+ data[self._VISUALCROSSING] = {
184
+ "forecast_topic": self.forecast_topic,
185
+ "base_url": self.base_url,
184
186
  "api_key": self.api_key,
185
- "interval": self.update_interval,
187
+ "update_interval": self.update_interval,
188
+ "location": self.location,
186
189
  }
187
190
  return data
188
191
 
189
192
  @override
190
- def from_dict(self, data) -> None:
193
+ def from_dict(self, data: dict[str, Any]) -> None:
191
194
  super().from_dict(data)
192
- if "_visualcrossing" in data:
193
- for key, value in data["_visualcrossing"].items():
195
+ if self._VISUALCROSSING in data:
196
+ for key, value in data[self._VISUALCROSSING].items():
194
197
  setattr(self, key, value)
@@ -1,4 +1,3 @@
1
- from ast import Str
2
1
  from typing_extensions import override
3
2
  from masterpiece import Plugin, Composite
4
3
  from .visualcrossing import VisualCrossing
@@ -1,32 +1,10 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: juham-visualcrossing
3
- Version: 0.1.8
3
+ Version: 0.1.9
4
4
  Summary: A Weather forecast plugin extending `Juham` applications
5
5
  Author-email: J Meskanen <juham.api@gmail.com>
6
6
  Maintainer-email: "J. Meskanen" <juham.api@gmail.com>
7
- License: MIT License
8
- ===========
9
-
10
- Copyright (c) 2024, Juha Meskanen
11
-
12
- Permission is hereby granted, free of charge, to any person obtaining a copy
13
- of this software and associated documentation files (the "Software"), to deal
14
- in the Software without restriction, including without limitation the rights
15
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
- copies of the Software, and to permit persons to whom the Software is
17
- furnished to do so, subject to the following conditions:
18
-
19
- The above copyright notice and this permission notice shall be included in all
20
- copies or substantial portions of the Software.
21
-
22
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
-
29
-
7
+ License-Expression: MIT
30
8
  Project-URL: Homepage, https://gitlab.com/juham/juham/juham-visualcrossing
31
9
  Project-URL: Bug Reports, https://gitlab.com/juham/juham/juham-visualcrossing
32
10
  Project-URL: Funding, https://meskanen.com
@@ -36,15 +14,15 @@ Keywords: object-oriented,plugin,framework
36
14
  Classifier: Development Status :: 3 - Alpha
37
15
  Classifier: Intended Audience :: Developers
38
16
  Classifier: Topic :: Software Development
39
- Classifier: License :: OSI Approved :: MIT License
40
17
  Classifier: Programming Language :: Python :: 3.8
41
18
  Requires-Python: >=3.8
42
19
  Description-Content-Type: text/x-rst
43
20
  License-File: LICENSE.rst
44
- Requires-Dist: juham-core>=0.1.1
21
+ Requires-Dist: juham-core>=0.1.5
45
22
  Requires-Dist: pytz>=2024.1
46
23
  Provides-Extra: dev
47
24
  Requires-Dist: check-manifest; extra == "dev"
25
+ Dynamic: license-file
48
26
 
49
27
  VisualCrossing forecast plugin for Juham™
50
28
  =========================================
@@ -1,4 +1,4 @@
1
- juham-core>=0.1.1
1
+ juham-core>=0.1.5
2
2
  pytz>=2024.1
3
3
 
4
4
  [dev]
@@ -1,4 +1,5 @@
1
1
 
2
+
2
3
  [build-system]
3
4
  requires = ["setuptools"]
4
5
  build-backend = "setuptools.build_meta"
@@ -10,11 +11,11 @@ packages = ["juham_visualcrossing"]
10
11
 
11
12
  [project]
12
13
  name = "juham-visualcrossing"
13
- version = "0.1.8"
14
+ version = "0.1.9"
14
15
  requires-python = ">=3.8"
15
16
  description = "A Weather forecast plugin extending `Juham` applications"
16
17
  readme = {file = "README.rst", content-type = "text/x-rst"}
17
- license = {file = "LICENSE.rst", content-type = "text/x-rst"}
18
+ license = "MIT"
18
19
  keywords = ["object-oriented", "plugin", "framework"]
19
20
  authors = [
20
21
  {name = "J Meskanen", email = "juham.api@gmail.com" }
@@ -27,12 +28,11 @@ classifiers = [
27
28
  "Development Status :: 3 - Alpha",
28
29
  "Intended Audience :: Developers",
29
30
  "Topic :: Software Development",
30
- "License :: OSI Approved :: MIT License",
31
31
  "Programming Language :: Python :: 3.8",
32
32
  ]
33
33
 
34
34
  dependencies = [
35
- "juham-core >= 0.1.1",
35
+ "juham-core >= 0.1.5",
36
36
  "pytz >= 2024.1",
37
37
  ]
38
38
 
@@ -0,0 +1,217 @@
1
+ import unittest
2
+
3
+ from datetime import datetime, timedelta
4
+ from unittest.mock import MagicMock, patch
5
+ import json
6
+ from typing_extensions import Any
7
+ from juham_visualcrossing.visualcrossing import VisualCrossing, VisualCrossingThread
8
+
9
+
10
+ class MockResponse:
11
+
12
+ def __init__(self, data: Any) -> None:
13
+ self._data = data
14
+
15
+ def json(self) -> Any:
16
+ return self._data
17
+
18
+
19
+ # Assuming your classes are imported as follows:
20
+ # from your_module import VisualCrossingThread, VisualCrossing, Mqtt
21
+
22
+
23
+ class TestVisualCrossingThread(unittest.TestCase):
24
+
25
+ @patch("juham_visualcrossing.visualcrossing.Mqtt")
26
+ def test_visualcrossing_thread_init(self, MockMqtt: MagicMock) -> None:
27
+ # Mock the MQTT client
28
+ mock_client = MockMqtt.return_value
29
+ thread: VisualCrossingThread = VisualCrossingThread(mock_client)
30
+
31
+ # Assert that the MQTT client is assigned
32
+ self.assertEqual(thread.mqtt_client, mock_client)
33
+
34
+ @patch("juham_visualcrossing.visualcrossing.datetime")
35
+ def test_make_weburl(self, MockDatetime: MagicMock) -> None:
36
+ # Mock datetime to return a specific date
37
+ mock_now = datetime(2023, 10, 20)
38
+ mock_end = mock_now + timedelta(days=1)
39
+
40
+ # Mock the return value of now() and strftime() on the mocked datetime
41
+ MockDatetime.now.return_value = mock_now
42
+ MockDatetime.return_value.strftime.side_effect = lambda fmt: (
43
+ mock_now.strftime(fmt) if fmt != "%Y-%m-%d" else mock_end.strftime(fmt)
44
+ )
45
+
46
+ # Initialize and set class-level attributes
47
+ thread = VisualCrossingThread(None)
48
+ thread.init(
49
+ "forecast_topic", "https://api.example.com/", 3600, "API_KEY", "city"
50
+ )
51
+
52
+ # Call method
53
+ result = thread.make_weburl()
54
+
55
+ # Expected URL based on the mocked datetime
56
+ expected_url = "https://api.example.com/city/2023-10-20/2023-10-21?unitGroup=metric&contentType=json&include=hours&key=API_KEY"
57
+
58
+ # Assert the result is as expected
59
+ self.assertEqual(result, expected_url)
60
+
61
+ # @patch("juham_visualcrossing.visualcrossing.json.dumps")
62
+ # @patch("juham_visualcrossing.visualcrossing.Mqtt")
63
+ def test_process_data(self) -> None:
64
+ # Setup mock MQTT and data
65
+ mock_client = MagicMock()
66
+ thread = VisualCrossingThread(mock_client)
67
+
68
+ # Prepare mock response data
69
+ mock_data: dict[str, list[dict[str, list[dict[str, Any]]]]] = {
70
+ "days": [
71
+ {
72
+ "hours": [
73
+ {
74
+ "datetimeEpoch": 1632952200,
75
+ "uvindex": 5,
76
+ "solarradiation": 1.5,
77
+ "solarenergy": 10.5,
78
+ "cloudcover": 80,
79
+ "snow": 0,
80
+ "snowdepth": 0,
81
+ "pressure": 1015,
82
+ "temp": 20,
83
+ "humidity": 60,
84
+ "windspeed": 5,
85
+ "winddir": 180,
86
+ "dew": 10,
87
+ }
88
+ ]
89
+ }
90
+ ]
91
+ }
92
+
93
+ with patch.object(thread, "publish") as mock_publish, patch.object(
94
+ thread, "info"
95
+ ) as mock_info:
96
+ # Act
97
+ thread.process_data(MockResponse(mock_data))
98
+
99
+ # Assert
100
+ mock_publish.assert_called_once()
101
+
102
+
103
+ class TestVisualCrossing(unittest.TestCase):
104
+
105
+ def test_visualcrossing_init(self) -> None:
106
+ # Instantiate VisualCrossing and check its properties
107
+ vc = VisualCrossing(name="test_visualcrossing")
108
+
109
+ self.assertEqual(vc.forecast_topic, vc.make_topic_name("forecast"))
110
+
111
+ @patch("juham_visualcrossing.visualcrossing.VisualCrossingThread")
112
+ def test_visualcrossing_on_connect(
113
+ self, MockVisualCrossingThread: MagicMock
114
+ ) -> None:
115
+ # Mock the thread and MQTT client
116
+
117
+ vc = VisualCrossing(name="test_visualcrossing")
118
+
119
+ # Ensure forecast_topic is set
120
+ vc.forecast_topic = "/forecast" # Make sure forecast_topic has a value
121
+
122
+ # Mock subscribe method before calling on_connect
123
+ with patch.object(vc, "subscribe", MagicMock()):
124
+
125
+ # Mock the MQTT connection callback
126
+ mock_client = MagicMock()
127
+ userdata = None
128
+ flags = 0
129
+ rc = 0 # Success
130
+
131
+ # Call the on_connect method
132
+ vc.on_connect(mock_client, userdata, flags, rc)
133
+
134
+ # Assert that it subscribed to the topic
135
+ vc.subscribe.assert_called_once_with(vc.forecast_topic)
136
+
137
+ @patch("juham_visualcrossing.visualcrossing.VisualCrossingThread")
138
+ def test_visualcrossing_run(self, MockVisualCrossingThread: MagicMock) -> None:
139
+ # Mock the thread's methods
140
+ mock_thread = MockVisualCrossingThread.return_value
141
+ vc = VisualCrossing(name="test_visualcrossing")
142
+
143
+ # Patch instantiate to return our mock
144
+ vc.instantiate = MagicMock(return_value=mock_thread)
145
+
146
+ # Initialize with required attributes
147
+ vc.forecast_topic = "forecast_topic"
148
+ vc.base_url = "https://example.com"
149
+ vc.update_interval = 3600
150
+ vc.api_key = "API_KEY"
151
+ vc.location = "city"
152
+
153
+ # Call the method
154
+ vc.run()
155
+
156
+ # Check if the thread was initialized correctly
157
+ mock_thread.init.assert_called_once_with(
158
+ "forecast_topic", "https://example.com", 3600, "API_KEY", "city"
159
+ )
160
+
161
+ def test_visualcrossing_to_dict(self) -> None:
162
+ vc = VisualCrossing(name="test_visualcrossing")
163
+ vc.forecast_topic = "forecast_topic"
164
+ vc.base_url = "https://example.com"
165
+ vc.api_key = "API_KEY"
166
+ vc.update_interval = 3600
167
+ vc.location = "city"
168
+
169
+ expected_dict: dict[str, Any] = {
170
+ "_class": "VisualCrossing",
171
+ "_version": 0,
172
+ "_object": {"name": "test_visualcrossing", "payload": None},
173
+ "_base": {},
174
+ "visualcrossing": {
175
+ "forecast_topic": "forecast_topic",
176
+ "base_url": "https://example.com",
177
+ "api_key": "API_KEY",
178
+ "update_interval": 3600,
179
+ "location": "city",
180
+ },
181
+ }
182
+
183
+ # Check if the to_dict method works
184
+ result = vc.to_dict()
185
+ self.assertEqual(result["visualcrossing"], expected_dict["visualcrossing"])
186
+
187
+ @patch("juham_visualcrossing.visualcrossing.VisualCrossingThread")
188
+ def test_visualcrossing_from_dict(
189
+ self, MockVisualCrossingThread: MagicMock
190
+ ) -> None:
191
+ data: dict[str, Any] = {
192
+ "_class": "VisualCrossing",
193
+ "_version": 0,
194
+ "_object": {"name": "visualcrossing", "payload": None},
195
+ "_base": {},
196
+ "visualcrossing": {
197
+ "forecast_topic": "forecast_topic",
198
+ "base_url": "https://example.com",
199
+ "api_key": "API_KEY",
200
+ "update_interval": 3600,
201
+ "location": "dusseldorf",
202
+ },
203
+ }
204
+
205
+ vc = VisualCrossing(name="test_visualcrossing")
206
+ vc.from_dict(data)
207
+
208
+ # Assert that the from_dict method correctly assigns values
209
+ self.assertEqual(vc.forecast_topic, "forecast_topic")
210
+ self.assertEqual(vc.base_url, "https://example.com")
211
+ self.assertEqual(vc.api_key, "API_KEY")
212
+ self.assertEqual(vc.update_interval, 3600)
213
+ self.assertEqual(vc.location, "dusseldorf")
214
+
215
+
216
+ if __name__ == "__main__":
217
+ unittest.main()
@@ -1,16 +0,0 @@
1
- import unittest
2
-
3
- from juham_visualcrossing.visualcrossing import VisualCrossing
4
-
5
-
6
- class TestVisualCrossing(unittest.TestCase):
7
- """Unit tests for `VisualCrossing` weather forecast masterpiece."""
8
-
9
- def test_get_classid(self):
10
- """Assert that the meta-class driven class initialization works."""
11
- classid = VisualCrossing.get_class_id()
12
- self.assertEqual("VisualCrossing", classid)
13
-
14
-
15
- if __name__ == "__main__":
16
- unittest.main()