Im new to python and would like some experienced guidance in how to create this Connector.
The idea is to pass the userid/pwd to a site that my Childs uses and retrieve the lunch menu.
They uses a site called Schoolsoft.
The result from the lunch menu should be passed, per weekday back to 5 sensors, one for each schoolday.
I have done some things but can’t make it to work and are pretty sure that there are a few things that needs correction. eg. the pass of data from site to 5 sensors.
Anyone up for advices or would like to get payed to develop this for me?
Thanks!
"""
Schoolsoft lunchmeny
"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_SCHOOL CONF_USERNAME CONF_PASSWORD CONF_USERTYPE
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
# Validation of the user's configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SCHOOL): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERTYPE): cv.string
})
def setup_platform(hass, config, add_devices, discovery_info=None):
sensor_name = CONF_NAME
school = config.get(CONF_SCHOOL)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
usertype = config.get(CONF_USERTYPE)
sensor_name = 'Lunchmenu'
add_devices(schoolsoft)
class Schoolsoft(Entity):
"""Implementation of the schoolsoft sensor."""
def __init__(self, sensor_name, matsedel, school, username, password, usertype):
"""Initialize the sensor."""
self._name = sensor_name
self._state = None
self._data = {}
self.update()
self.school = school
self.username = username
self.password = password
self.usertype = usertype
self.cookies = {}
_login_page_re = r"https://sms(\d*).schoolsoft.se/%s/html/redirect_login.htm"
self._login_page_re = re.compile(_login_page_re % school)
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return 'mdi:food'
def try_get(self, url, attempts = 0):
r = requests.get(url, cookies=self.cookies)
login_page_match = self._login_page_re.match(r.url)
if login_page_match:
server_n = login_page_match.groups()
if attempts < 1:
# Sends a post request with self.username && self.password
loginr = requests.post(self.login_page, data = {
"action": "login",
"usertype": self.usertype,
"ssusername": self.username,
"sspassword": self.password
}, cookies=self.cookies, allow_redirects=False)
# Saves login cookie for faster access after first call
self.cookies = loginr.cookies
return self.try_get(url, attempts+1)
else:
raise AuthFailure("Invalid username or password")
else:
return r
def update(self):
menu_html = self.try_get("https://sms5.schoolsoft.se/{}/jsp/student/right_student_lunchmenu.jsp?menu=lunchmenu".format(self.school))
menu = BeautifulSoup(menu_html.text, "html.parser")
lunch_menu = []
for div in menu.find_all("td", {"style": "word-wrap: break-word"}):
food_info = div.get_text(separator=u"<br/>").split(u"<br/>")
lunch_menu.append(food_info)
self._state = true