Skip to content

Commit

Permalink
Change from username and password to auto to fix the capcha issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesper committed Feb 19, 2022
1 parent 23c1229 commit f55d53c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ Use [HACS](https://custom-components.github.io/hacs/) to point to this github UR
https://github.com/skelgaard/homeassistant-apsystems

### Configuration
Use your apsystemsema.com user to configure the configuration.yaml.
Use your apsystemsema.com to configure the configuration.yaml.

```yaml
# Minimal configuration.yaml entry:
sensor:
- platform: apsystems
username: apsystemsema_user
password: !secret apsystems
authId: apsystemsema_user
systemId: apsystemsema_system_id
ecuId: apsystemsema_ecu_id
sunset: off
```
1 - your systemId is found at apsystemsema.com. See the page source code and at the Settings Menu there is a code like that:
1 - set "Allow visitors to access to this system" and get the authid from here
2 - your systemId is found at apsystemsema.com. See the page source code and at the Settings Menu there is a code like that:
```html
<span>Settings</span>
<ul>
Expand All @@ -43,9 +45,9 @@ sensor:
```
Get the system id inside the ```managementClickCustomer()```.

2 - There is an ecu id data at https://apsystemsema.com/ema/security/optmainmenu/intoLargeReport.action
3 - There is an ecu id data at https://apsystemsema.com/ema/security/optmainmenu/intoLargeReport.action

3 - sunset attribute could be on or off
4 - sunset attribute could be on or off


### Thanx
Expand Down
46 changes: 16 additions & 30 deletions custom_components/apsystems/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import time
import mechanize

CONF_USERNAME = 'username'
CONF_PASSWORD = 'password'
CONF_AUTH_ID = 'authId'
CONF_SYSTEM_ID = 'systemId'
CONF_ECU_ID = 'ecuId'
CONF_NAME = 'name'
Expand All @@ -33,8 +32,7 @@
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_AUTH_ID): cv.string,
vol.Required(CONF_SYSTEM_ID): cv.string,
vol.Required(CONF_ECU_ID): cv.string,
vol.Optional(CONF_NAME, default='APsystems'): cv.string,
Expand All @@ -55,33 +53,31 @@


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
username = config[CONF_USERNAME]
password = config[CONF_PASSWORD]
auth_id = config[CONF_AUTH_ID]
system_id = config[CONF_SYSTEM_ID]
ecu_id = config[CONF_ECU_ID]
sunset = config[CONF_SUNSET]

#data fetcher
fetcher = APsystemsFetcher(hass, username, password, system_id, ecu_id)
fetcher = APsystemsFetcher(hass, auth_id, system_id, ecu_id)

sensors = []
for type in SENSORS:
metadata = SENSORS[type]
sensor_name = config.get(CONF_NAME).lower() + "_" + type
sensor = ApsystemsSensor(sensor_name, username, password, system_id, sunset, fetcher, metadata)
sensor = ApsystemsSensor(sensor_name, auth_id, system_id, sunset, fetcher, metadata)
sensors.append(sensor)

async_add_entities(sensors, True)

class ApsystemsSensor(Entity):
"""Representation of a Sensor."""

def __init__(self, sensor_name, username, password, system_id, sunset, fetcher, metadata):
def __init__(self, sensor_name, auth_id, system_id, sunset, fetcher, metadata):
"""Initialize the sensor."""
self._state = None
self._name = sensor_name
self._username = username
self._password = password
self._auth_id = auth_id
self._system_id = system_id
self._sunset = sunset
self._fetcher = fetcher
Expand Down Expand Up @@ -182,37 +178,27 @@ def find_stop_time(self, now):


class APsystemsFetcher:
url_login = "https://apsystemsema.com/ema/loginEMA.action"
url_login = "https://apsystemsema.com/ema/intoDemoUser.action?id="
url_data = "https://apsystemsema.com/ema/ajax/getReportApiAjax/getPowerOnCurrentDayAjax"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
cache = None
cache_timestamp = None
running = False

def __init__(self, hass, username, password, system_id, ecu_id):
def __init__(self, hass, auth_id, system_id, ecu_id):
self._hass = hass
self._username = username
self._password = password
self._auth_id = auth_id
self._system_id = system_id
self._ecu_id = ecu_id
self._today = datetime.fromisoformat(date.today().isoformat())

async def login(self):
browser = mechanize.Browser()
s = requests.Session()

await self._hass.async_add_executor_job(
browser.open, self.url_login
r = await self._hass.async_add_executor_job(
s.get, self.url_login + self._auth_id
)
browser.select_form(nr=0)
browser.form.set_all_readonly(False)
browser.form['username'] = self._username
browser.form['password'] = self._password

await self._hass.async_add_executor_job(
browser.submit
)

return browser
return s

async def run(self):
self.running = True
Expand All @@ -229,10 +215,10 @@ async def run(self):

now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
_LOGGER.debug('starting: ' + now)
session = requests.sessions.session()
result_data = await self._hass.async_add_executor_job(
session.request, "POST", self.url_data, None, post_data, self.headers, browser.cookiejar
browser.post, self.url_data, post_data
)

_LOGGER.debug("status code data: " + str(result_data.status_code))

if result_data.status_code == 204:
Expand Down

0 comments on commit f55d53c

Please sign in to comment.