Skip to content

Commit

Permalink
Bump pybotvac and use new exceptions (home-assistant#27249)
Browse files Browse the repository at this point in the history
* Bump pybotvac

* Fix tests

* Remove super calls

* Surround some more statements

* Correct logger message for vacuum
  • Loading branch information
Santobert authored and MartinHjelmare committed Oct 6, 2019
1 parent 1059cea commit dae8cd8
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 70 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/neato/.translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
}
},
"error": {
"invalid_credentials": "Invalid credentials"
"invalid_credentials": "Invalid credentials",
"unexpected_error": "Unexpected error"
},
"create_entry": {
"default": "See [Neato documentation]({docs_url})."
Expand Down
30 changes: 19 additions & 11 deletions homeassistant/components/neato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from datetime import timedelta

from requests.exceptions import HTTPError, ConnectionError as ConnError
from pybotvac.exceptions import NeatoException, NeatoLoginException, NeatoRobotException
import voluptuous as vol

from homeassistant.config_entries import SOURCE_IMPORT
Expand All @@ -20,6 +20,7 @@
NEATO_ROBOTS,
NEATO_PERSISTENT_MAPS,
NEATO_MAP_DATA,
SCAN_INTERVAL_MINUTES,
VALID_VENDORS,
)

Expand Down Expand Up @@ -103,7 +104,12 @@ async def async_setup_entry(hass, entry):
_LOGGER.debug("Failed to login to Neato API")
return False

await hass.async_add_executor_job(hub.update_robots)
try:
await hass.async_add_executor_job(hub.update_robots)
except NeatoRobotException:
_LOGGER.debug("Failed to connect to Neato API")
return False

for component in ("camera", "vacuum", "switch"):
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
Expand Down Expand Up @@ -144,17 +150,19 @@ def login(self):
self.config[CONF_USERNAME], self.config[CONF_PASSWORD], self._vendor
)
self.logged_in = True
except (HTTPError, ConnError):
_LOGGER.error("Unable to connect to Neato API")
self.logged_in = False
return

_LOGGER.debug("Successfully connected to Neato API")
self._hass.data[NEATO_ROBOTS] = self.my_neato.robots
self._hass.data[NEATO_PERSISTENT_MAPS] = self.my_neato.persistent_maps
self._hass.data[NEATO_MAP_DATA] = self.my_neato.maps
_LOGGER.debug("Successfully connected to Neato API")
self._hass.data[NEATO_ROBOTS] = self.my_neato.robots
self._hass.data[NEATO_PERSISTENT_MAPS] = self.my_neato.persistent_maps
self._hass.data[NEATO_MAP_DATA] = self.my_neato.maps
except NeatoException as ex:
if isinstance(ex, NeatoLoginException):
_LOGGER.error("Invalid credentials")
else:
_LOGGER.error("Unable to connect to Neato API")
self.logged_in = False

@Throttle(timedelta(seconds=300))
@Throttle(timedelta(minutes=SCAN_INTERVAL_MINUTES))
def update_robots(self):
"""Update the robot states."""
_LOGGER.debug("Running HUB.update_robots %s", self._hass.data[NEATO_ROBOTS])
Expand Down
49 changes: 36 additions & 13 deletions homeassistant/components/neato/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
from datetime import timedelta
import logging

from pybotvac.exceptions import NeatoRobotException

from homeassistant.components.camera import Camera

from .const import NEATO_DOMAIN, NEATO_MAP_DATA, NEATO_ROBOTS, NEATO_LOGIN
from .const import (
NEATO_DOMAIN,
NEATO_MAP_DATA,
NEATO_ROBOTS,
NEATO_LOGIN,
SCAN_INTERVAL_MINUTES,
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(minutes=10)
SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
Expand Down Expand Up @@ -37,9 +45,9 @@ def __init__(self, hass, robot):
"""Initialize Neato cleaning map."""
super().__init__()
self.robot = robot
self._robot_name = "{} {}".format(self.robot.name, "Cleaning Map")
self.neato = hass.data[NEATO_LOGIN] if NEATO_LOGIN in hass.data else None
self._robot_name = f"{self.robot.name} Cleaning Map"
self._robot_serial = self.robot.serial
self.neato = hass.data[NEATO_LOGIN]
self._image_url = None
self._image = None

Expand All @@ -50,16 +58,31 @@ def camera_image(self):

def update(self):
"""Check the contents of the map list."""
self.neato.update_robots()
image_url = None
map_data = self.hass.data[NEATO_MAP_DATA]
image_url = map_data[self._robot_serial]["maps"][0]["url"]
if image_url == self._image_url:
_LOGGER.debug("The map image_url is the same as old")
if self.neato is None:
_LOGGER.error("Error while updating camera")
self._image = None
self._image_url = None
return
image = self.neato.download_map(image_url)
self._image = image.read()
self._image_url = image_url

_LOGGER.debug("Running camera update")
try:
self.neato.update_robots()

image_url = None
map_data = self.hass.data[NEATO_MAP_DATA][self._robot_serial]["maps"][0]
image_url = map_data["url"]
if image_url == self._image_url:
_LOGGER.debug("The map image_url is the same as old")
return

image = self.neato.download_map(image_url)
self._image = image.read()
self._image_url = image_url

except NeatoRobotException as ex:
_LOGGER.error("Neato camera connection error: %s", ex)
self._image = None
self._image_url = None

@property
def name(self):
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/neato/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

import voluptuous as vol
from requests.exceptions import HTTPError, ConnectionError as ConnError
from pybotvac.exceptions import NeatoLoginException, NeatoRobotException

from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
Expand Down Expand Up @@ -106,7 +106,9 @@ def try_login(username, password, vendor):

try:
Account(username, password, this_vendor)
except (HTTPError, ConnError):
except NeatoLoginException:
return "invalid_credentials"
except NeatoRobotException:
return "unexpected_error"

return None
2 changes: 2 additions & 0 deletions homeassistant/components/neato/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
NEATO_MAP_DATA = "neato_map_data"
NEATO_PERSISTENT_MAPS = "neato_persistent_maps"

SCAN_INTERVAL_MINUTES = 5

VALID_VENDORS = ["neato", "vorwerk"]

MODE = {1: "Eco", 2: "Turbo"}
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/neato/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/neato",
"requirements": [
"pybotvac==0.0.15"
"pybotvac==0.0.16"
],
"dependencies": [],
"codeowners": [
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/neato/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
}
},
"error": {
"invalid_credentials": "Invalid credentials"
"invalid_credentials": "Invalid credentials",
"unexpected_error": "Unexpected error"
},
"create_entry": {
"default": "See [Neato documentation]({docs_url})."
Expand Down
35 changes: 22 additions & 13 deletions homeassistant/components/neato/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
from datetime import timedelta
import logging

import requests
from pybotvac.exceptions import NeatoRobotException

from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.helpers.entity import ToggleEntity

from .const import NEATO_DOMAIN, NEATO_LOGIN, NEATO_ROBOTS
from .const import NEATO_DOMAIN, NEATO_LOGIN, NEATO_ROBOTS, SCAN_INTERVAL_MINUTES

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(minutes=10)
SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES)

SWITCH_TYPE_SCHEDULE = "schedule"

Expand Down Expand Up @@ -44,26 +44,29 @@ def __init__(self, hass, robot, switch_type):
"""Initialize the Neato Connected switches."""
self.type = switch_type
self.robot = robot
self.neato = hass.data[NEATO_LOGIN]
self._robot_name = "{} {}".format(self.robot.name, SWITCH_TYPES[self.type][0])
self.neato = hass.data[NEATO_LOGIN] if NEATO_LOGIN in hass.data else None
self._robot_name = f"{self.robot.name} {SWITCH_TYPES[self.type][0]}"
self._state = None
self._schedule_state = None
self._clean_state = None
self._robot_serial = self.robot.serial

def update(self):
"""Update the states of Neato switches."""
if self.neato is None:
_LOGGER.error("Error while updating switches")
self._state = None
return

_LOGGER.debug("Running switch update")
self.neato.update_robots()
try:
self.neato.update_robots()
self._state = self.robot.state
except (
requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
) as ex:
_LOGGER.warning("Neato connection error: %s", ex)
except NeatoRobotException as ex:
_LOGGER.error("Neato switch connection error: %s", ex)
self._state = None
return

_LOGGER.debug("self._state=%s", self._state)
if self.type == SWITCH_TYPE_SCHEDULE:
_LOGGER.debug("State: %s", self._state)
Expand Down Expand Up @@ -104,9 +107,15 @@ def device_info(self):
def turn_on(self, **kwargs):
"""Turn the switch on."""
if self.type == SWITCH_TYPE_SCHEDULE:
self.robot.enable_schedule()
try:
self.robot.enable_schedule()
except NeatoRobotException as ex:
_LOGGER.error("Neato switch connection error: %s", ex)

def turn_off(self, **kwargs):
"""Turn the switch off."""
if self.type == SWITCH_TYPE_SCHEDULE:
self.robot.disable_schedule()
try:
self.robot.disable_schedule()
except NeatoRobotException as ex:
_LOGGER.error("Neato switch connection error: %s", ex)
Loading

0 comments on commit dae8cd8

Please sign in to comment.