Blocking Call Error #419
-
Hi, import requests
from bs4 import BeautifulSoup
import json
import time
import datetime
def find_origin_destination(reg):
URL = "https://www.radarbox.com/data/registration/" + reg
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
origin = soup.find("div", id="origin")
origindata = []
try:
for block in origin.find_all("div"):
origindata.append(block.text)
except AttributeError:
origindata.append("Unavailable")
destination = soup.find("div", id="destination")
destinationdata = []
try:
for block in destination.find_all("div"):
destinationdata.append(block.text)
except AttributeError:
destinationdata.append("Unavailable")
if len(origindata) < 3 and len(destinationdata) < 3:
return [origindata[0], destinationdata[0]]
elif len(origindata) < 3 and len(destinationdata) > 3:
return [origindata[0], destinationdata[2][1:4]]
elif len(origindata) > 3 and len(destinationdata) < 3:
return [origindata[2][1:4], destinationdata[0]]
else:
return [origindata[2][1:4], destinationdata[2][1:4]]
@service
def getAircraft():
url = "http://192.168.1.4:8078/data/aircraft.json"
page = requests.get(url)
parsedpage = json.loads(page.text)
data = []
outputjson = {"now":time.mktime(datetime.datetime.now().timetuple()), "num_aircraft":"", "aircraft":[]}
for aircraft in parsedpage["aircraft"]:
row_data = {"Registration": "","Type": "","Flight": "","Altitude": "","Airspeed": "","Origin": "","Destination": ""}
try:
row_data["Registration"] = (aircraft["r"])
except KeyError:
row_data["Registration"] = ("-")
try:
row_data["Type"]=(aircraft["t"])
except KeyError:
row_data["Type"]=("-")
try:
row_data["Flight"]=(aircraft["flight"])
except KeyError:
row_data["Flight"]=("-")
try:
row_data["Altitude"]=(aircraft["alt_baro"])
except KeyError:
row_data["Altitude"]=("-")
try:
row_data["Airspeed"]=(aircraft["tas"])
except KeyError:
row_data["Airspeed"]=("-")
try:
row_data["Origin"]=(find_origin_destination(aircraft["r"])[0])
except:
row_data["Origin"]=("-")
try:
row_data["Destination"]=(find_origin_destination(aircraft["r"])[1])
except:
row_data["Destination"]=("-")
data.append(row_data)
outputjson["aircraft"]=data
outputjson["num_aircraft"]=str(len(data))
state.set(aircraft, value=None, new_attributes=outputjson) and I get the following error when I call
Could you please help me? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Instead of Also, unrelated to your question, your loop could be more compact by using try:
origin = find_origin_destination(aircraft["r"])[0]
except:
origin = "-"
row_data = {
"Airspeed": aircraft.get("tas", "-"),
"Altitude": aircraft.get("alt_baro", "-"),
"Origin": origin,
...
} |
Beta Was this translation helpful? Give feedback.
-
This line doesn't look correct, since state.set(aircraft, value=None, new_attributes=outputjson) Shouldn't you do something like this: state.set("aircraft.data", value=None, new_attributes=outputjson) |
Beta Was this translation helpful? Give feedback.
-
Thank you, @craigbarratt for all your help! My code now looks like this: import aiohttp
from bs4 import BeautifulSoup
import json
import time
import datetime
async def find_origin_destination(reg):
url = "https://www.radarbox.com/data/registration/" + reg
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
page = resp.text()
soup = BeautifulSoup(page, "html.parser")
origin = soup.find("div", id="origin")
origindata = []
try:
for block in origin.find_all("div"):
origindata.append(block.text)
except AttributeError:
origindata.append("Unavailable")
destination = soup.find("div", id="destination")
destinationdata = []
try:
for block in destination.find_all("div"):
destinationdata.append(block.text)
except AttributeError:
destinationdata.append("Unavailable")
if len(origindata) < 3 and len(destinationdata) < 3:
return [origindata[0], destinationdata[0]]
elif len(origindata) < 3 and len(destinationdata) > 3:
return [origindata[0], destinationdata[2][1:4]]
elif len(origindata) > 3 and len(destinationdata) < 3:
return [origindata[2][1:4], destinationdata[0]]
else:
return [origindata[2][1:4], destinationdata[2][1:4]]
@service
async def getAircraft():
localurl = "http://192.168.1.4:8078/data/aircraft.json"
async with aiohttp.ClientSession() as session:
async with session.get(localurl) as resp:
page = resp.text()
parsedpage = json.loads(page)
data = []
outputjson = {"now":time.mktime(datetime.datetime.now().timetuple()), "num_aircraft":"", "aircraft":[]}
for aircraft in parsedpage["aircraft"]:
row_data = {
"Registration": aircraft.get("r","-"),
"Type": aircraft.get("t","-"),
"Flight": aircraft.get("flight","-"),
"Altitude": aircraft.get("alt_baro","-"),
"Airspeed": aircraft.get("tas","-"),
"Origin": find_origin_destination(aircraft["r"])[0],
"Destination": find_origin_destination(aircraft["r"])[1]
}
data.append(row_data)
outputjson["aircraft"]=data
outputjson["num_aircraft"]=str(len(data))
state.set("sensor.local_aircraft", value=len(data), new_attributes=outputjson) And works perfectly - gives me the following entity in Home Assistant: This is exactly what I was trying to achieve! This will now allow me to make lovelace cards. I will continuously improve the code in the coming days - any suggestions appreciated :) I will continue to learn! Thanks! |
Beta Was this translation helpful? Give feedback.
pyscript
uses the hass main event loop, so you should not do anything that can block (eg, network requests). See the documentation.Instead of
requests
, the best solution is to useaiohttp
(see the 3rd example).Also, unrelated to your question, your loop could be more compact by using
get
to fetchdict
values, since it allows a default value, eg: