You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Platform for ourclublogin.com sensor integration."""importloggingimportrefromdatetimeimportdatetime, timedelta, timezoneimporthomeassistant.helpers.config_validationascvimporthomeassistant.util.dtasdt_utilimportrequestsimportvoluptuousasvolfromhomeassistant.constimportCONF_ID, CONF_PASSWORD, CONF_USERNAMEfromhomeassistant.helpers.entityimportEntityfrom .constimportATTR_CHECK_IN_DATE, ATTR_DATETIME_FORMAT, ATTR_ICON, ATTR_URL, DOMAIN_LOGGER=logging.getLogger(__name__)
SCAN_INTERVAL=timedelta(minutes=15)
asyncdefasync_setup_entry(hass, config_entry, async_add_entities):
"""Setup ourclublogin.com sensors from a config entry created in the integrations UI."""config=hass.data[DOMAIN][config_entry.entry_id]
club_id=config[CONF_ID]
username=config[CONF_USERNAME]
password=config[CONF_PASSWORD]
our_club_login_data=OurClubLoginData(club_id, username, password)
async_add_entities(
[OurClubLoginSensor(our_club_login_data)], update_before_add=True
)
classOurClubLoginSensor(Entity):
"""Representation of a ourclublogin.com Sensor."""def__init__(self, our_club_login_data):
"""Initialize the ourclublogin.com sensor."""self._state=Noneself._our_club_login_data=our_club_login_dataself._data=self._our_club_login_data.data@propertydefname(self):
"""Return the name of the ourclublogin.com sensor."""returnf'Last {self._data.get("ClubName")} Check In'@propertydefstate(self):
"""Return the state of the ourclublogin.com sensor."""returnself._data.get("CheckInDate")
@propertydeficon(self):
"""Return the icon to use in ourclublogin.com frontend."""returnATTR_ICONdefupdate(self):
"""Update data from ourclublogin.com for the sensor."""self._our_club_login_data.update()
self._data=self._our_club_login_data.dataclassOurClubLoginData:
"""Coordinate retrieving and updating data from ourclublogin.com."""def__init__(self, club_id, username, password):
"""Initialize the OurClubLoginData object."""self._club_id=club_idself._username=usernameself._password=passwordself._session=requests.Session()
self.data=None# TODO: OOP thisdefour_club_login_query(self):
"""Query ourclublogin.com for data."""params= (("ReturnUrl", f"^%^{self._club_id}"),)
response=self._session.get(f"{ATTR_URL}/Account/Login", params=params)
ifresponse.ok:
_LOGGER.info("Connected to ourclublogin.com")
else:
_LOGGER.critical("Could not connect to ourclublogin.com")
csrf_hidden_token=re.search(
'"\_\_RequestVerificationToken".+?value\="(.+?)"', str(response.content)
)
ifcsrf_hidden_token:
csrf_hidden_token=csrf_hidden_token[1]
_LOGGER.info("Hidden CSRF token acquired")
else:
_LOGGER.critical("Could not acquire hidden CSRF token")
data= {
"__RequestVerificationToken": csrf_hidden_token,
"Username": self._username,
"Password": self._password,
}
response=self._session.post(f"{ATTR_URL}/Account/Login", data=data)
ifresponse.ok:
_LOGGER.info("Authenticated with ourclublogin.com")
else:
_LOGGER.critical("Could not authenticate with ourclublogin.com")
datetime_local_now=datetime.now().astimezone()
datetime_local_new_year=datetime(datetime_local_now.year, 1, 1)
datetime_utc_now=datetime_local_now.astimezone(timezone.utc)
datetime_utc_new_year=datetime_local_new_year.astimezone(timezone.utc)
data= {
"startDate": datetime_utc_new_year.strftime(ATTR_DATETIME_FORMAT),
"endDate": datetime_utc_now.strftime(ATTR_DATETIME_FORMAT),
}
response=self._session.post(
f"{ATTR_URL}/Checkin/GetCustomerVisits", data=data
)
ifresponse.ok:
_LOGGER.info("Posted to GetCustomerVisits endpoint")
else:
_LOGGER.critical("Could not post to GetCustomerVisits endpoint")
customer_visits=response.json()
customer_visits.sort(key=lambdaitem: item[ATTR_CHECK_IN_DATE]) # ensure sortedlast_customer_visit=customer_visits[-1:][0]
# TODO: add other data pointsself.data=last_customer_visitdefupdate(self):
"""Update data from ourclublogin.com via our_club_login_query."""returnself.our_club_login_query()
de93d90701a7e1d69af01ad3b518d15f82bd4d67
The text was updated successfully, but these errors were encountered:
OOP this
https://github.com/jcgoette/our_club_login_homeassistant/blob/110f893a628862d5dd75483e3e981d102ca6c353/custom_components/our_club_login/sensor.py#L76
de93d90701a7e1d69af01ad3b518d15f82bd4d67
The text was updated successfully, but these errors were encountered: