Skip to content

Commit

Permalink
Merge pull request #42 from CoMPaTech/maintenance
Browse files Browse the repository at this point in the history
Quality improvements
  • Loading branch information
CoMPaTech authored Oct 17, 2023
2 parents b8c3729 + 89cd308 commit 792b1bb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ Even though available does not mean it's stable yet, the HA part is solid but th

# Changelog

## OCT 2023 [0.2.4]
- Improve quality

## SEP 2023 [0.2.3]
- Conform to hacs and HA files
- Adding HACS validation
Expand Down
10 changes: 7 additions & 3 deletions custom_components/stromer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, DOMAIN, LOGGER
from .coordinator import StromerDataUpdateCoordinator
from .stromer import Stromer
from .stromer import Stromer, ApiError

SCAN_INTERVAL = timedelta(minutes=10)

Expand All @@ -28,7 +28,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

# Initialize connection to stromer
stromer = Stromer(username, password, client_id, client_secret)
await stromer.stromer_connect()
try:
await stromer.stromer_connect()
except ApiError as ex:
raise ConfigEntryNotReady("Error while communicating to Stromer API") from ex

LOGGER.debug("Stromer entry: {}".format(entry))

# Use Bike ID as unique id
Expand Down Expand Up @@ -63,4 +67,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)

return
return unload_ok
3 changes: 1 addition & 2 deletions custom_components/stromer/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, DOMAIN, LOGGER
from .stromer import Stromer

# TODO adjust the data schema to the data that you need
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
Expand All @@ -24,7 +23,7 @@
)


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
async def validate_input(_, data: dict[str, Any]) -> dict[str, Any]:
"""Validate the user input allows us to connect."""
username = data[CONF_USERNAME]
password = data[CONF_PASSWORD]
Expand Down
6 changes: 1 addition & 5 deletions custom_components/stromer/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
UpdateFailed)

from .const import DOMAIN, LOGGER
from .stromer import Stromer
from .stromer import Stromer, ApiError


class StromerData(NamedTuple):
Expand Down Expand Up @@ -47,7 +47,3 @@ async def _async_update_data(self) -> StromerData:
except ApiError as err:
raise UpdateFailed(f"Error communicating with API: {err}")
return StromerData(*data)


class ApiError(BaseException):
"""Error to indicate something wrong with the API."""
2 changes: 1 addition & 1 deletion custom_components/stromer/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/CoMPaTech/stromer/issues",
"requirements": [],
"version": "0.2.3"
"version": "0.2.4"
}
14 changes: 13 additions & 1 deletion custom_components/stromer/stromer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,20 @@ async def stromer_update(self):
LOGGER.error("Stromer error: api call failed: {}".format(e))
LOGGER.debug("Stromer retry: {}/10".format(attempts))

LOGGER.error("Stromer error: api call failed 10 times, cowardly failing")
raise ApiError

async def stromer_get_code(self):
url = f"{self.base_url}/mobile/v4/login/"
if self._api_version == 'v3':
url = f"{self.base_url}/users/login/"
res = await self._websession.get(url)
cookie = res.headers.get("Set-Cookie")
try:
cookie = res.headers.get("Set-Cookie")
except Exception as e:
LOGGER.error("Stromer error: api call failed: {}".format(e))
raise ApiError

pattern = "=(.*?);"
csrftoken = re.search(pattern, cookie).group(1)

Expand Down Expand Up @@ -157,3 +165,7 @@ async def stromer_call_api(self, endpoint, data={}):
# LOGGER.debug("ret %s" % ret)
# LOGGER.debug("res status %s" % res.status)
return ret["data"][0]

class ApiError(Exception):
"""Error to indicate something wrong with the API."""

0 comments on commit 792b1bb

Please sign in to comment.