Skip to content

Commit

Permalink
Nissanleaf login fix (home-assistant#26139)
Browse files Browse the repository at this point in the history
* Upgrade to pycarwings2.9 per 25 July 2019 API change

* Remove rest of location tracker. Fix get_status_from_update call.
  • Loading branch information
filcole authored and balloob committed Aug 22, 2019
1 parent 08471e3 commit a582110
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 116 deletions.
87 changes: 19 additions & 68 deletions homeassistant/components/nissan_leaf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
DATA_LEAF = "nissan_leaf_data"

DATA_BATTERY = "battery"
DATA_LOCATION = "location"
DATA_CHARGING = "charging"
DATA_PLUGGED_IN = "plugged_in"
DATA_CLIMATE = "climate"
DATA_RANGE_AC = "range_ac_on"
DATA_RANGE_AC_OFF = "range_ac_off"

CONF_NCONNECT = "nissan_connect"
CONF_INTERVAL = "update_interval"
CONF_CHARGING_INTERVAL = "update_interval_charging"
CONF_CLIMATE_INTERVAL = "update_interval_climate"
Expand Down Expand Up @@ -61,7 +59,6 @@
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_REGION): vol.In(CONF_VALID_REGIONS),
vol.Optional(CONF_NCONNECT, default=True): cv.boolean,
vol.Optional(CONF_INTERVAL, default=DEFAULT_INTERVAL): (
vol.All(cv.time_period, vol.Clamp(min=MIN_UPDATE_INTERVAL))
),
Expand All @@ -84,7 +81,7 @@
extra=vol.ALLOW_EXTRA,
)

LEAF_COMPONENTS = ["sensor", "switch", "binary_sensor", "device_tracker"]
LEAF_COMPONENTS = ["sensor", "switch", "binary_sensor"]

SIGNAL_UPDATE_LEAF = "nissan_leaf_update"

Expand Down Expand Up @@ -177,8 +174,7 @@ def setup_leaf(car_config):
hass.data[DATA_LEAF][leaf.vin] = data_store

for component in LEAF_COMPONENTS:
if component != "device_tracker" or car_config[CONF_NCONNECT]:
load_platform(hass, component, DOMAIN, {}, car_config)
load_platform(hass, component, DOMAIN, {}, car_config)

async_track_point_in_utc_time(
hass, data_store.async_update_data, utcnow() + INITIAL_UPDATE
Expand Down Expand Up @@ -209,24 +205,20 @@ def __init__(self, hass, leaf, car_config):
self.hass = hass
self.leaf = leaf
self.car_config = car_config
self.nissan_connect = car_config[CONF_NCONNECT]
self.force_miles = car_config[CONF_FORCE_MILES]
self.data = {}
self.data[DATA_CLIMATE] = False
self.data[DATA_BATTERY] = 0
self.data[DATA_CHARGING] = False
self.data[DATA_LOCATION] = False
self.data[DATA_RANGE_AC] = 0
self.data[DATA_RANGE_AC_OFF] = 0
self.data[DATA_PLUGGED_IN] = False
self.next_update = None
self.last_check = None
self.request_in_progress = False
# Timestamp of last successful response from battery,
# climate or location.
# Timestamp of last successful response from battery or climate.
self.last_battery_response = None
self.last_climate_response = None
self.last_location_response = None
self._remove_listener = None

async def async_update_data(self, now):
Expand Down Expand Up @@ -334,20 +326,6 @@ async def async_refresh_data(self, now):
except CarwingsError:
_LOGGER.error("Error fetching climate info")

if self.nissan_connect:
try:
location_response = await self.async_get_location()

if location_response is None:
_LOGGER.debug("Empty Location Response Received")
self.data[DATA_LOCATION] = None
else:
_LOGGER.debug("Location Response: %s", location_response.__dict__)
self.data[DATA_LOCATION] = location_response
self.last_location_response = utcnow()
except CarwingsError:
_LOGGER.error("Error fetching location info")

self.request_in_progress = False
async_dispatcher_send(self.hass, SIGNAL_UPDATE_LEAF)

Expand All @@ -364,19 +342,6 @@ async def async_get_battery(self):
from pycarwings2 import CarwingsError

try:
# First, check nissan servers for the latest data
start_server_info = await self.hass.async_add_executor_job(
self.leaf.get_latest_battery_status
)

# Store the date from the nissan servers
start_date = self._extract_start_date(start_server_info)
if start_date is None:
_LOGGER.info("No start date from servers. Aborting")
return None

_LOGGER.debug("Start server date=%s", start_date)

# Request battery update from the car
_LOGGER.debug("Requesting battery update, %s", self.leaf.vin)
request = await self.hass.async_add_executor_job(self.leaf.request_update)
Expand All @@ -393,21 +358,30 @@ async def async_get_battery(self):
)
await asyncio.sleep(PYCARWINGS2_SLEEP)

# Note leaf.get_status_from_update is always returning 0, so
# don't try to use it anymore.
server_info = await self.hass.async_add_executor_job(
self.leaf.get_latest_battery_status
# We don't use the response from get_status_from_update
# apart from knowing that the car has responded saying it
# has given the latest battery status to Nissan.
check_result_info = await self.hass.async_add_executor_job(
self.leaf.get_status_from_update, request
)

latest_date = self._extract_start_date(server_info)
_LOGGER.debug("Latest server date=%s", latest_date)
if latest_date is not None and latest_date != start_date:
if check_result_info is not None:
# Get the latest battery status from Nissan servers.
# This has the SOC in it.
server_info = await self.hass.async_add_executor_job(
self.leaf.get_latest_battery_status
)
return server_info

_LOGGER.debug(
"%s attempts exceeded return latest data from server",
MAX_RESPONSE_ATTEMPTS,
)
# Get the latest data from the nissan servers, even though
# it may be out of date, it's better than nothing.
server_info = await self.hass.async_add_executor_job(
self.leaf.get_latest_battery_status
)
return server_info
except CarwingsError:
_LOGGER.error("An error occurred getting battery status.")
Expand Down Expand Up @@ -465,29 +439,6 @@ async def async_set_climate(self, toggle):
_LOGGER.debug("Climate result not returned by Nissan servers")
return False

async def async_get_location(self):
"""Get location from Nissan servers."""
request = await self.hass.async_add_executor_job(self.leaf.request_location)
for attempt in range(MAX_RESPONSE_ATTEMPTS):
if attempt > 0:
_LOGGER.debug(
"Location data not in yet. (%s) (%s). " "Waiting %s seconds",
self.leaf.vin,
attempt,
PYCARWINGS2_SLEEP,
)
await asyncio.sleep(PYCARWINGS2_SLEEP)

location_status = await self.hass.async_add_executor_job(
self.leaf.get_status_from_location, request
)

if location_status is not None:
_LOGGER.debug("Location_status=%s", location_status.__dict__)
break

return location_status


class LeafEntity(Entity):
"""Base class for Nissan Leaf entity."""
Expand Down
46 changes: 0 additions & 46 deletions homeassistant/components/nissan_leaf/device_tracker.py

This file was deleted.

2 changes: 1 addition & 1 deletion homeassistant/components/nissan_leaf/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Nissan leaf",
"documentation": "https://www.home-assistant.io/components/nissan_leaf",
"requirements": [
"pycarwings2==2.8"
"pycarwings2==2.9"
],
"dependencies": [],
"codeowners": [
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ pyblackbird==0.5
pybotvac==0.0.15

# homeassistant.components.nissan_leaf
pycarwings2==2.8
pycarwings2==2.9

# homeassistant.components.cloudflare
pycfdns==0.0.1
Expand Down

0 comments on commit a582110

Please sign in to comment.