Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a SoC module for Nissan Leaf till MY 2019 #1682

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ea41953
Create __init__.py
mekrapp Jun 15, 2024
6c05372
Add files via upload
mekrapp Jun 15, 2024
55f6939
Add files via upload
mekrapp Jun 15, 2024
38c4aca
Add files via upload
mekrapp Jun 15, 2024
c90a586
Add files via upload
mekrapp Jun 15, 2024
08d7c3e
Update soc.py
mekrapp Jun 16, 2024
d62c77b
Update soc.py
mekrapp Jun 16, 2024
fbfd4ec
Update soc.py
mekrapp Jun 16, 2024
320c0b3
Update soc.py
mekrapp Jun 21, 2024
3ba67d2
Update responses.py
mekrapp Jun 21, 2024
f6c9754
Update packages/modules/vehicles/leaf/soc.py
mekrapp Jun 22, 2024
a03caf6
Update soc.py
mekrapp Jun 22, 2024
b93dd5f
Update __init__.py
mekrapp Aug 21, 2024
1c142c7
Update __init__.py
mekrapp Aug 21, 2024
38f7973
Update __init__.py
mekrapp Aug 21, 2024
f4db4e4
Merge branch 'openWB:master' into master
mekrapp Aug 21, 2024
da78bd8
Update __init__.py
mekrapp Aug 21, 2024
30e305d
Merge pull request #1 from mekrapp/mekrapp-patch-1
mekrapp Aug 21, 2024
a867764
Merge branch 'openWB:master' into master
mekrapp Aug 24, 2024
9a02623
Update pycarwings2.py
mekrapp Aug 24, 2024
69d663f
Update pycarwings2.py
mekrapp Aug 24, 2024
f8ee104
Update __init__.py
mekrapp Aug 24, 2024
c629c1e
Update soc.py
mekrapp Aug 24, 2024
920abad
Update __init__.py
mekrapp Aug 24, 2024
6ce31f1
Delete packages/modules/vehicles/leaf/__init__.py
mekrapp Aug 24, 2024
9b6a833
Add files via upload
mekrapp Aug 24, 2024
2d9d5b9
Update requirements.txt
mekrapp Aug 29, 2024
c41725f
Update packages/modules/vehicles/leaf/soc.py
mekrapp Aug 30, 2024
4d8783f
Delete packages/modules/vehicles/leaf/pycarwings2.py
mekrapp Aug 30, 2024
8f87879
Delete packages/modules/vehicles/leaf/responses.py
mekrapp Aug 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
17 changes: 17 additions & 0 deletions packages/modules/vehicles/leaf/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Optional


class LeafConfiguration:
def __init__(self, user_id: Optional[str] = None, password: Optional[str] = None):
self.user_id = user_id
self.password = password


class LeafSoc:
def __init__(self,
name: str = "Leaf",
type: str = "leaf",
configuration: LeafConfiguration = None) -> None:
self.name = name
self.type = type
self.configuration = configuration or LeafConfiguration()
81 changes: 81 additions & 0 deletions packages/modules/vehicles/leaf/soc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
from typing import List

import logging
import time

from helpermodules.cli import run_using_positional_cli_args
from modules.common import store
from modules.common.abstract_device import DeviceDescriptor
from modules.common.abstract_vehicle import VehicleUpdateData
from modules.common.component_state import CarState
from modules.common.configurable_vehicle import ConfigurableVehicle
from modules.vehicles.leaf.config import LeafSoc, LeafConfiguration

import pycarwings2

log = logging.getLogger(__name__)


def fetch_soc(username, password, chargepoint) -> CarState:

region = "NE"

def getNissanSession(): # open Https session with Nissan server
log.debug("LP%s: login = %s, region = %s" % (chargepoint, username, region))
s = pycarwings2.Session(username, password, region)
leaf = s.get_leaf()
time.sleep(1) # give Nissan server some time
return leaf

def readSoc(leaf): # get SoC from Nissan server
leaf_info = leaf.get_latest_battery_status()
bat_percent = int(leaf_info.battery_percent)
log.debug("LP%s: Battery status %s" % (chargepoint, bat_percent))
return bat_percent

def requestSoc(leaf): # request Nissan server to request last SoC from car
log.debug("LP%s: Request SoC Update" % (chargepoint))
key = leaf.request_update()
status = leaf.get_status_from_update(key)
sleepsecs = 20
for i in range(0, 9):
log.debug("Waiting {0} seconds".format(sleepsecs))
time.sleep(sleepsecs)
status = leaf.get_status_from_update(key)
if status is not None:
break
log.debug("LP%s: Finished updating" % (chargepoint))

leaf = getNissanSession() # start Https session with Nissan Server
readSoc(leaf) # old SoC needs to be read from server before requesting new SoC from car
time.sleep(1) # give Nissan server some time
requestSoc(leaf) # Nissan server to request new SoC from car
time.sleep(1) # give Nissan server some time
soc = readSoc(leaf) # final read of SoC from server
return CarState(soc)


def create_vehicle(vehicle_config: LeafSoc, vehicle: int):
def updater(vehicle_update_data: VehicleUpdateData) -> CarState:
return fetch_soc(
vehicle_config.configuration.user_id,
vehicle_config.configuration.password,
vehicle)
return ConfigurableVehicle(vehicle_config=vehicle_config, component_updater=updater, vehicle=vehicle)


def leaf_update(user_id: str, password: str, charge_point: int):
log.debug("Leaf: user_id="+user_id+"charge_point="+str(charge_point))
vehicle_config = LeafSoc(configuration=LeafConfiguration(charge_point, user_id, password))
store.get_car_value_store(charge_point).store.set(fetch_soc(
vehicle_config.configuration.user_id,
vehicle_config.configuration.password,
charge_point))


def main(argv: List[str]):
run_using_positional_cli_args(leaf_update, argv)


device_descriptor = DeviceDescriptor(configuration_factory=LeafSoc)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ pytz==2023.3.post1
grpcio==1.60.1
protobuf==4.25.3
bimmer_connected==0.15.1
pycarwings2==2.14