Skip to content

Commit

Permalink
Merge pull request #50 from syssi/apply-isort-and-black
Browse files Browse the repository at this point in the history
Apply isort and black formatting
  • Loading branch information
marcelwestrahome authored May 31, 2023
2 parents a2078fb + 98b8911 commit 00a3357
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 101 deletions.
11 changes: 6 additions & 5 deletions custom_components/niu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""niu component."""
from __future__ import annotations

import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import CONF_SENSORS, DOMAIN, CONF_AUTH
from .const import CONF_AUTH, CONF_SENSORS, DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand All @@ -16,17 +17,17 @@

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Niu Smart Plug from a config entry."""

niu_auth = entry.data.get(CONF_AUTH, None)
if niu_auth == None:
return False
sensors_selected = niu_auth[CONF_SENSORS]

sensors_selected = niu_auth[CONF_SENSORS]
if len(sensors_selected) < 1:
_LOGGER.error("You did NOT selected any sensor... cant setup the integration..")
return False

if 'LastTrackThumb' in sensors_selected:
if "LastTrackThumb" in sensors_selected:
PLATFORMS.append("camera")

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down
45 changes: 25 additions & 20 deletions custom_components/niu/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from .const import *
from datetime import datetime, timedelta
import hashlib
import requests
import json
from datetime import datetime, timedelta

# from homeassistant.util import Throttle
from time import gmtime, strftime

import requests

from .const import *


class NiuApi:
def __init__(self, username, password, scooter_id) -> None:
self.username = username
Expand All @@ -20,14 +24,17 @@ def __init__(self, username, password, scooter_id) -> None:
def initApi(self):
self.token = self.get_token()
api_uri = MOTOINFO_LIST_API_URI
self.sn = self.get_vehicles_info(api_uri)["data"]["items"][self.scooter_id]["sn_id"]
self.sensor_prefix = self.get_vehicles_info(api_uri)["data"]["items"][self.scooter_id]["scooter_name"]
self.sn = self.get_vehicles_info(api_uri)["data"]["items"][self.scooter_id][
"sn_id"
]
self.sensor_prefix = self.get_vehicles_info(api_uri)["data"]["items"][
self.scooter_id
]["scooter_name"]
self.updateBat()
self.updateMoto()
self.updateMotoInfo()
self.updateTrackInfo()


def get_token(self):
username = self.username
password = self.password
Expand All @@ -49,9 +56,7 @@ def get_token(self):
data = json.loads(r.content.decode())
return data["data"]["token"]["access_token"]


def get_vehicles_info(self, path):

token = self.token

url = API_BASE_URL + path
Expand All @@ -65,8 +70,10 @@ def get_vehicles_info(self, path):
data = json.loads(r.content.decode())
return data


def get_info(self,path, ):
def get_info(
self,
path,
):
sn = self.sn
token = self.token
url = API_BASE_URL + path
Expand All @@ -77,7 +84,6 @@ def get_info(self,path, ):
"user-agent": "manager/4.10.4 (android; IN2020 11);lang=zh-CN;clientIdentifier=Domestic;timezone=Asia/Shanghai;model=IN2020;deviceName=IN2020;ostype=android",
}
try:

r = requests.get(url, headers=headers, params=params)

except ConnectionError:
Expand All @@ -89,8 +95,10 @@ def get_info(self,path, ):
return False
return data


def post_info(self, path,):
def post_info(
self,
path,
):
sn, token = self.sn, self.token
url = API_BASE_URL + path
params = {}
Expand All @@ -106,7 +114,6 @@ def post_info(self, path,):
return False
return data


def post_info_track(self, path):
sn, token = self.sn, self.token
url = API_BASE_URL + path
Expand All @@ -132,7 +139,6 @@ def post_info_track(self, path):
return False
return data


def getDataBat(self, id_field):
return self.dataBat["data"]["batteries"]["compartmentA"][id_field]

Expand All @@ -154,9 +160,7 @@ def getDataTrack(self, id_field):
(self.dataTrackInfo["data"][0][id_field]) / 1000
).strftime("%Y-%m-%d %H:%M:%S")
if id_field == "ridingtime":
return strftime(
"%H:%M:%S", gmtime(self.dataTrackInfo["data"][0][id_field])
)
return strftime("%H:%M:%S", gmtime(self.dataTrackInfo["data"][0][id_field]))
if id_field == "track_thumb":
thumburl = self.dataTrackInfo["data"][0][id_field].replace(
"app-api.niucache.com", "app-api-fk.niu.com"
Expand All @@ -176,7 +180,8 @@ def updateMotoInfo(self):
def updateTrackInfo(self):
self.dataTrackInfo = self.post_info_track(TRACK_LIST_API_URI)

'''class NiuDataBridge(object):

"""class NiuDataBridge(object):
async def __init__(self, api):
# hass, username, password, country, scooter_id):
Expand Down Expand Up @@ -246,4 +251,4 @@ def updateMotoInfo(self):
@Throttle(timedelta(seconds=1))
def updateTrackInfo(self):
self._dataTrackInfo = self.api.post_info_track(TRACK_LIST_API_URI)'''
self._dataTrackInfo = self.api.post_info_track(TRACK_LIST_API_URI)"""
56 changes: 33 additions & 23 deletions custom_components/niu/camera.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
"""Last Track for Niu Integration integration.
Author: Giovanni P. (@pikka97)
"""
from .api import NiuApi
from .const import *

import httpx
import logging
from typing import final

from homeassistant.components.generic.camera import GenericCamera
import httpx

from homeassistant.components.camera import STATE_IDLE
from homeassistant.components.generic.camera import GenericCamera
from homeassistant.helpers.httpx_client import get_async_client

from .api import NiuApi
from .const import *

_LOGGER = logging.getLogger(__name__)
GET_IMAGE_TIMEOUT = 10


async def async_setup_entry(hass, entry, async_add_entities) -> None:
niu_auth = entry.data.get(CONF_AUTH, None)
if niu_auth == None:
_LOGGER.error("The authenticator of your Niu integration is None.. can not setup the integration...")
_LOGGER.error(
"The authenticator of your Niu integration is None.. can not setup the integration..."
)
return False

username = niu_auth[CONF_USERNAME]
password = niu_auth[CONF_PASSWORD]
scooter_id = niu_auth[CONF_SCOOTER_ID]

api = NiuApi(username, password, scooter_id)
api = NiuApi(username, password, scooter_id)
await hass.async_add_executor_job(api.initApi)

camera_name = api.sensor_prefix + ' Last Track Camera'

entry = {'name': camera_name, 'still_image_url': "", 'stream_source': None, 'authentication': 'basic', 'username': None, 'password': None, 'limit_refetch_to_url_change': False, 'content_type': 'image/jpeg', 'framerate': 2, 'verify_ssl': True}
camera_name = api.sensor_prefix + " Last Track Camera"

entry = {
"name": camera_name,
"still_image_url": "",
"stream_source": None,
"authentication": "basic",
"username": None,
"password": None,
"limit_refetch_to_url_change": False,
"content_type": "image/jpeg",
"framerate": 2,
"verify_ssl": True,
}
async_add_entities([LastTrackCamera(hass, api, entry, camera_name, camera_name)])


Expand All @@ -40,42 +54,38 @@ def __init__(self, hass, api, device_info, identifier: str, title: str) -> None:
self._api = api
super().__init__(hass, device_info, identifier, title)



@property
@final
def state(self) -> str:
"""Return the camera state."""
return STATE_IDLE


@property
def is_on(self) -> bool:
"""Return true if on."""
return self._last_image != b''

return self._last_image != b""

@property
def device_info(self):
device_name = 'Niu E-scooter'
device_name = "Niu E-scooter"
dev = {
"identifiers": {('niu', device_name)},
"identifiers": {("niu", device_name)},
"name": device_name,
"manufacturer": "Niu",
"model": 1.0,
}
}
return dev

async def async_camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
get_last_track = lambda : self._api.getDataTrack("track_thumb")
get_last_track = lambda: self._api.getDataTrack("track_thumb")
last_track_url = await self.hass.async_add_executor_job(get_last_track)

if last_track_url == self._last_url and self._previous_image != b'':
#The path image is the same as before so the image is the same:
if last_track_url == self._last_url and self._previous_image != b"":
# The path image is the same as before so the image is the same:
return self._previous_image

try:
async_client = get_async_client(self.hass, verify_ssl=self.verify_ssl)
response = await async_client.get(
Expand Down
54 changes: 30 additions & 24 deletions custom_components/niu/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
"""
from __future__ import annotations

from .const import *
from .api import NiuApi


import logging
from typing import Any

Expand All @@ -15,41 +11,44 @@
from homeassistant import config_entries
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError

from homeassistant.helpers import selector

from .api import NiuApi
from .const import *

_LOGGER = logging.getLogger(__name__)

STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_SCOOTER_ID, default=DEFAULT_SCOOTER_ID): int,
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_SCOOTER_ID, default=DEFAULT_SCOOTER_ID): int,
vol.Required(CONF_SENSORS, default=AVAILABLE_SENSORS): selector.SelectSelector(
selector.SelectSelectorConfig(options=AVAILABLE_SENSORS,
multiple=True,
mode=selector.SelectSelectorMode.LIST),
)
selector.SelectSelectorConfig(
options=AVAILABLE_SENSORS,
multiple=True,
mode=selector.SelectSelectorMode.LIST,
),
),
}
)



class NiuAuthenticator:
def __init__(self, username, password, scooter_id, sensors_selected) -> None:
self.username = username
self.password = password
self.scooter_id = scooter_id
self.sensors_selected = sensors_selected

async def authenticate(self, hass):
api = NiuApi(self.username, self.password, self.scooter_id)
try:
token = await hass.async_add_executor_job(api.get_token)
if isinstance(token, bool):
return token
else:
return token != ''
return token != ""
except:
return False

Expand All @@ -59,24 +58,31 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 1

async def async_step_user(self, user_input: dict[str, Any] | None = None) -> FlowResult:
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Invoked when a user clicks the add button"""


integration_title = 'Niu EScooter Integration'

integration_title = "Niu EScooter Integration"
errors = {}

if user_input != None:
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]
scooter_id = user_input[CONF_SCOOTER_ID]
sensors_selected = user_input[CONF_SENSORS]
niu_auth = NiuAuthenticator(username, password, scooter_id, sensors_selected)
niu_auth = NiuAuthenticator(
username, password, scooter_id, sensors_selected
)
auth_result = await niu_auth.authenticate(self.hass)
if auth_result:
return self.async_create_entry(title=integration_title, data={CONF_AUTH:niu_auth.__dict__})
return self.async_create_entry(
title=integration_title, data={CONF_AUTH: niu_auth.__dict__}
)
else:
# The user used wrong credentials...
errors['base']= "invalid_auth"
errors["base"] = "invalid_auth"

return self.async_show_form(step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)
Loading

0 comments on commit 00a3357

Please sign in to comment.