mirror of
https://github.com/nymea/nymea-plugins.git
synced 2026-07-17 00:36:35 +02:00
Some moe progress
properly wait for the initialisation and add some exception handling to print errors from asyncio threads
This commit is contained in:
parent
9b57aad0af
commit
bd50fdf728
@ -48,17 +48,19 @@ timers = {}
|
||||
loop = asyncio.get_event_loop()
|
||||
accuweather = None
|
||||
|
||||
|
||||
def init():
|
||||
# AccuWeather lib needs asyncio, let's start the event loop for it.
|
||||
loop.run_forever();
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
async def initAccuWeather(latitude, longitude):
|
||||
# Need "global" here to be able to modify the global variable
|
||||
global accuweather
|
||||
logger.log("Initializing Accuweather, latitude", latitude, "longitude:", longitude)
|
||||
logger.log("Initializing Accuweather, latitude", latitude, "longitude:", longitude, apikey)
|
||||
websession = ClientSession()
|
||||
accuweather = AccuWeather(apikey, websession, latitude, longitude)
|
||||
logger.log("Init finished")
|
||||
|
||||
|
||||
def deinit():
|
||||
@ -81,23 +83,35 @@ def discoverThings(info):
|
||||
|
||||
async def getCurrentConditions(thing):
|
||||
logger.log("running get current conditions")
|
||||
currentConditions = await accuweather.async_get_current_conditions()
|
||||
try:
|
||||
currentConditions = await accuweather.async_get_current_conditions()
|
||||
except Exception as e:
|
||||
logger.warn("Error fetching eather conditions:", e)
|
||||
|
||||
logger.log("after await", currentConditions)
|
||||
|
||||
thing.setStateValue(weatherWeatherDescriptionStateId, currentConditions["WeatherText"]);
|
||||
|
||||
thing.setStateValue(weatherTemperatureStateId, currentConditions["Temperature"]["Metric"]["Value"]);
|
||||
thing.setStateValue(weatherHumidityStateId, currentConditions["Humidity"]["Metric"]["Value"]);
|
||||
thing.setStateValue(weatherPressureStateId, currentConditions["Pressure"]["Metric"]["Value"]);
|
||||
try:
|
||||
logger.log("weather condition:", currentConditions["Temperature"]["Metric"]["Value"])
|
||||
|
||||
thing.setStateValue(weatherWindSpeedStateId, currentConditions["Wind"]["Direction"]["Degrees"]);
|
||||
thing.setStateValue(weatherWindDirectionStateId, currentConditions["Wind"]["Speed"]);
|
||||
# No Weather condition string in accuweather
|
||||
#thing.setStateValue(weatherWeatherDescriptionStateId, currentConditions["WeatherText"]);
|
||||
thing.setStateValue(weatherTemperatureStateTypeId, currentConditions["Temperature"]["Metric"]["Value"]);
|
||||
thing.setStateValue(weatherHumidityStateTypeId, currentConditions["RelativeHumidity"]);
|
||||
thing.setStateValue(weatherPressureStateTypeId, currentConditions["Pressure"]["Metric"]["Value"]);
|
||||
|
||||
thing.setStateValue(weatherMinTemperatureStateId, currentConditions["TemperatureSummary"]["Past24HourRange"]["Minimum"]);
|
||||
thing.setStateValue(weatherMaxTemperatureStateId, currentConditions["TemperatureSummary"]["Past24HourRange"]["Maximum"]);
|
||||
thing.setStateValue(weatherWindSpeedStateTypeId, currentConditions["Wind"]["Speed"]["Metric"]["Value"]);
|
||||
thing.setStateValue(weatherWindDirectionStateTypeId, currentConditions["Wind"]["Direction"]["Degrees"]);
|
||||
|
||||
thing.setStateValue(weatherVisibilityStateId, currentConditions["Visibility"]["Metric"]["Value"]);
|
||||
thing.setStateValue(cloudinessStateId, currentConditions["CloudCover"]);
|
||||
thing.setStateValue(weatherMinTemperatureStateId, currentConditions["TemperatureSummary"]["Past24HourRange"]["Minimum"]);
|
||||
thing.setStateValue(weatherMaxTemperatureStateId, currentConditions["TemperatureSummary"]["Past24HourRange"]["Maximum"]);
|
||||
|
||||
thing.setStateValue(weatherVisibilityStateId, currentConditions["Visibility"]["Metric"]["Value"])
|
||||
thing.setStateValue(cloudinessStateId, currentConditions["CloudCover"])
|
||||
|
||||
# As we're running in asyncio, nymea can't catch our exception, we need to print ourselves if something goes wrong.
|
||||
except Exception as e:
|
||||
logger.warn("Exception in reading weather conditions", e)
|
||||
#descriptor = nymea.ThingDescriptor(weatherThingClassId, "Weather")
|
||||
#info.addDescriptor(descriptor)
|
||||
#info.finish(nymea.ThingErrorNoError)
|
||||
@ -113,7 +127,9 @@ def setupThing(info):
|
||||
longitude = info.thing.paramValue(weatherThingLongitudeParamTypeId)
|
||||
|
||||
if accuweather == None:
|
||||
asyncio.run_coroutine_threadsafe(initAccuWeather(latitude, longitude), loop);
|
||||
future = asyncio.run_coroutine_threadsafe(initAccuWeather(latitude, longitude), loop)
|
||||
# Need to wait for it to have initialized before calling anything on it
|
||||
future.result()
|
||||
|
||||
future = asyncio.run_coroutine_threadsafe(getCurrentConditions(info.thing), loop)
|
||||
info.finish(nymea.ThingErrorNoError)
|
||||
@ -139,6 +155,7 @@ def executeAction(info):
|
||||
|
||||
|
||||
def configValueChanged(paramTypeId, value):
|
||||
global apikey
|
||||
if paramTypeId == accuWeatherPluginCustomApiKeyParamTypeId:
|
||||
apikey = value
|
||||
logger.log("API Key changed to", apikey)
|
||||
|
||||
@ -131,3 +131,49 @@ yarl==1.6.3 \
|
||||
--hash=sha256:f040bcc6725c821a4c0665f3aa96a4d0805a7aaf2caf266d256b8ed71b9f041c \
|
||||
--hash=sha256:f0b059678fd549c66b89bed03efcabb009075bd131c248ecdf087bdb6faba24a \
|
||||
--hash=sha256:fcbb48a93e8699eae920f8d92f7160c03567b421bc17362a9ffbbd706a816f71
|
||||
simplejson==3.17.2 \
|
||||
--hash=sha256:034550078a11664d77bc1a8364c90bb7eef0e44c2dbb1fd0a4d92e3997088667 \
|
||||
--hash=sha256:05b43d568300c1cd43f95ff4bfcff984bc658aa001be91efb3bb21df9d6288d3 \
|
||||
--hash=sha256:0dd9d9c738cb008bfc0862c9b8fa6743495c03a0ed543884bf92fb7d30f8d043 \
|
||||
--hash=sha256:10fc250c3edea4abc15d930d77274ddb8df4803453dde7ad50c2f5565a18a4bb \
|
||||
--hash=sha256:2862beabfb9097a745a961426fe7daf66e1714151da8bb9a0c430dde3d59c7c0 \
|
||||
--hash=sha256:292c2e3f53be314cc59853bd20a35bf1f965f3bc121e007ab6fd526ed412a85d \
|
||||
--hash=sha256:2d3eab2c3fe52007d703a26f71cf649a8c771fcdd949a3ae73041ba6797cfcf8 \
|
||||
--hash=sha256:2e7b57c2c146f8e4dadf84977a83f7ee50da17c8861fd7faf694d55e3274784f \
|
||||
--hash=sha256:311f5dc2af07361725033b13cc3d0351de3da8bede3397d45650784c3f21fbcf \
|
||||
--hash=sha256:344e2d920a7f27b4023c087ab539877a1e39ce8e3e90b867e0bfa97829824748 \
|
||||
--hash=sha256:3fabde09af43e0cbdee407555383063f8b45bfb52c361bc5da83fcffdb4fd278 \
|
||||
--hash=sha256:42b8b8dd0799f78e067e2aaae97e60d58a8f63582939af60abce4c48631a0aa4 \
|
||||
--hash=sha256:4b3442249d5e3893b90cb9f72c7d6ce4d2ea144d2c0d9f75b9ae1e5460f3121a \
|
||||
--hash=sha256:55d65f9cc1b733d85ef95ab11f559cce55c7649a2160da2ac7a078534da676c8 \
|
||||
--hash=sha256:5c659a0efc80aaaba57fcd878855c8534ecb655a28ac8508885c50648e6e659d \
|
||||
--hash=sha256:72d8a3ffca19a901002d6b068cf746be85747571c6a7ba12cbcf427bfb4ed971 \
|
||||
--hash=sha256:75ecc79f26d99222a084fbdd1ce5aad3ac3a8bd535cd9059528452da38b68841 \
|
||||
--hash=sha256:76ac9605bf2f6d9b56abf6f9da9047a8782574ad3531c82eae774947ae99cc3f \
|
||||
--hash=sha256:7d276f69bfc8c7ba6c717ba8deaf28f9d3c8450ff0aa8713f5a3280e232be16b \
|
||||
--hash=sha256:7f10f8ba9c1b1430addc7dd385fc322e221559d3ae49b812aebf57470ce8de45 \
|
||||
--hash=sha256:8042040af86a494a23c189b5aa0ea9433769cc029707833f261a79c98e3375f9 \
|
||||
--hash=sha256:813846738277729d7db71b82176204abc7fdae2f566e2d9fcf874f9b6472e3e6 \
|
||||
--hash=sha256:845a14f6deb124a3bcb98a62def067a67462a000e0508f256f9c18eff5847efc \
|
||||
--hash=sha256:869a183c8e44bc03be1b2bbcc9ec4338e37fa8557fc506bf6115887c1d3bb956 \
|
||||
--hash=sha256:8acf76443cfb5c949b6e781c154278c059b09ac717d2757a830c869ba000cf8d \
|
||||
--hash=sha256:8f713ea65958ef40049b6c45c40c206ab363db9591ff5a49d89b448933fa5746 \
|
||||
--hash=sha256:934115642c8ba9659b402c8bdbdedb48651fb94b576e3b3efd1ccb079609b04a \
|
||||
--hash=sha256:9551f23e09300a9a528f7af20e35c9f79686d46d646152a0c8fc41d2d074d9b0 \
|
||||
--hash=sha256:9a2b7543559f8a1c9ed72724b549d8cc3515da7daf3e79813a15bdc4a769de25 \
|
||||
--hash=sha256:a55c76254d7cf8d4494bc508e7abb993a82a192d0db4552421e5139235604625 \
|
||||
--hash=sha256:ad8f41c2357b73bc9e8606d2fa226233bf4d55d85a8982ecdfd55823a6959995 \
|
||||
--hash=sha256:af4868da7dd53296cd7630687161d53a7ebe2e63814234631445697bd7c29f46 \
|
||||
--hash=sha256:afebfc3dd3520d37056f641969ce320b071bc7a0800639c71877b90d053e087f \
|
||||
--hash=sha256:b59aa298137ca74a744c1e6e22cfc0bf9dca3a2f41f51bc92eb05695155d905a \
|
||||
--hash=sha256:bc00d1210567a4cdd215ac6e17dc00cb9893ee521cee701adfd0fa43f7c73139 \
|
||||
--hash=sha256:c1cb29b1fced01f97e6d5631c3edc2dadb424d1f4421dad079cb13fc97acb42f \
|
||||
--hash=sha256:c94dc64b1a389a416fc4218cd4799aa3756f25940cae33530a4f7f2f54f166da \
|
||||
--hash=sha256:ceaa28a5bce8a46a130cd223e895080e258a88d51bf6e8de2fc54a6ef7e38c34 \
|
||||
--hash=sha256:cff6453e25204d3369c47b97dd34783ca820611bd334779d22192da23784194b \
|
||||
--hash=sha256:d0b64409df09edb4c365d95004775c988259efe9be39697d7315c42b7a5e7e94 \
|
||||
--hash=sha256:d4813b30cb62d3b63ccc60dd12f2121780c7a3068db692daeb90f989877aaf04 \
|
||||
--hash=sha256:da3c55cdc66cfc3fffb607db49a42448785ea2732f055ac1549b69dcb392663b \
|
||||
--hash=sha256:e058c7656c44fb494a11443191e381355388443d543f6fc1a245d5d238544396 \
|
||||
--hash=sha256:fed0f22bf1313ff79c7fc318f7199d6c2f96d4de3234b2f12a1eab350e597c06 \
|
||||
--hash=sha256:ffd4e4877a78c84d693e491b223385e0271278f5f4e1476a4962dca6824ecfeb
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user