Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 11 additions & 8 deletions homeassistant/components/netatmo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle

from .const import DOMAIN, DATA_NETATMO_AUTH

_LOGGER = logging.getLogger(__name__)

DATA_PERSONS = 'netatmo_persons'
Expand All @@ -20,8 +22,6 @@
CONF_SECRET_KEY = 'secret_key'
CONF_WEBHOOKS = 'webhooks'

DOMAIN = 'netatmo'

SERVICE_ADDWEBHOOK = 'addwebhook'
SERVICE_DROPWEBHOOK = 'dropwebhook'

Expand Down Expand Up @@ -83,10 +83,9 @@ def setup(hass, config):
"""Set up the Netatmo devices."""
import pyatmo

global NETATMO_AUTH
hass.data[DATA_PERSONS] = {}
try:
NETATMO_AUTH = pyatmo.ClientAuth(
auth = pyatmo.ClientAuth(
config[DOMAIN][CONF_API_KEY], config[DOMAIN][CONF_SECRET_KEY],
config[DOMAIN][CONF_USERNAME], config[DOMAIN][CONF_PASSWORD],
'read_station read_camera access_camera '
Expand All @@ -96,6 +95,9 @@ def setup(hass, config):
_LOGGER.error("Unable to connect to Netatmo API")
return False

# Store config to be used during entry setup
hass.data[DATA_NETATMO_AUTH] = auth

if config[DOMAIN][CONF_DISCOVERY]:
for component in 'camera', 'sensor', 'binary_sensor', 'climate':
discovery.load_platform(hass, component, DOMAIN, {}, config)
Expand All @@ -107,7 +109,7 @@ def setup(hass, config):
webhook_id)
hass.components.webhook.async_register(
DOMAIN, 'Netatmo', webhook_id, handle_webhook)
NETATMO_AUTH.addwebhook(hass.data[DATA_WEBHOOK_URL])
auth.addwebhook(hass.data[DATA_WEBHOOK_URL])
hass.bus.listen_once(
EVENT_HOMEASSISTANT_STOP, dropwebhook)

Expand All @@ -117,7 +119,7 @@ def _service_addwebhook(service):
if url is None:
url = hass.data[DATA_WEBHOOK_URL]
_LOGGER.info("Adding webhook for URL: %s", url)
NETATMO_AUTH.addwebhook(url)
auth.addwebhook(url)

hass.services.register(
DOMAIN, SERVICE_ADDWEBHOOK, _service_addwebhook,
Expand All @@ -126,7 +128,7 @@ def _service_addwebhook(service):
def _service_dropwebhook(service):
"""Service to drop webhooks during runtime."""
_LOGGER.info("Dropping webhook")
NETATMO_AUTH.dropwebhook()
auth.dropwebhook()

hass.services.register(
DOMAIN, SERVICE_DROPWEBHOOK, _service_dropwebhook,
Expand All @@ -137,7 +139,8 @@ def _service_dropwebhook(service):

def dropwebhook(hass):
"""Drop the webhook subscription."""
NETATMO_AUTH.dropwebhook()
auth = hass.data[DATA_NETATMO_AUTH]
auth.dropwebhook()


async def handle_webhook(hass, webhook_id, request):
Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/netatmo/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from homeassistant.const import CONF_TIMEOUT
from homeassistant.helpers import config_validation as cv

from . import CameraData, NETATMO_AUTH
from .const import DATA_NETATMO_AUTH
from . import CameraData

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,8 +60,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
module_name = None

import pyatmo

auth = hass.data[DATA_NETATMO_AUTH]

try:
data = CameraData(hass, NETATMO_AUTH, home)
data = CameraData(hass, auth, home)
if not data.get_camera_names():
return None
except pyatmo.NoDevice:
Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/netatmo/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from homeassistant.const import CONF_VERIFY_SSL
from homeassistant.helpers import config_validation as cv

from . import CameraData, NETATMO_AUTH
from .const import DATA_NETATMO_AUTH
from . import CameraData

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,8 +38,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
verify_ssl = config.get(CONF_VERIFY_SSL, True)
quality = config.get(CONF_QUALITY, DEFAULT_QUALITY)
import pyatmo

auth = hass.data[DATA_NETATMO_AUTH]

try:
data = CameraData(hass, NETATMO_AUTH, home)
data = CameraData(hass, auth, home)
for camera_name in data.get_camera_names():
camera_type = data.get_camera_type(camera=camera_name, home=home)
if CONF_CAMERAS in config:
Expand Down
9 changes: 6 additions & 3 deletions homeassistant/components/netatmo/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
STATE_OFF, TEMP_CELSIUS, ATTR_TEMPERATURE, CONF_NAME)
from homeassistant.util import Throttle

from . import NETATMO_AUTH
from .const import DATA_NETATMO_AUTH

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,8 +68,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the NetAtmo Thermostat."""
import pyatmo
homes_conf = config.get(CONF_HOMES)

auth = hass.data[DATA_NETATMO_AUTH]

try:
home_data = HomeData(NETATMO_AUTH)
home_data = HomeData(auth)
except pyatmo.NoDevice:
return

Expand All @@ -88,7 +91,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
for home in homes:
_LOGGER.debug("Setting up %s ...", home)
try:
room_data = ThermostatData(NETATMO_AUTH, home)
room_data = ThermostatData(auth, home)
except pyatmo.NoDevice:
continue
for room_id in room_data.get_room_ids():
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/netatmo/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Constants used by the Netatmo component."""
DOMAIN = 'netatmo'

DATA_NETATMO = 'netatmo'
DATA_NETATMO_AUTH = 'netatmo_auth'
17 changes: 10 additions & 7 deletions homeassistant/components/netatmo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv

from . import NETATMO_AUTH
from .const import DATA_NETATMO_AUTH

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,23 +68,26 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available Netatmo weather sensors."""
dev = []
Comment thread
cgtobi marked this conversation as resolved.
auth = hass.data[DATA_NETATMO_AUTH]

if CONF_MODULES in config:
manual_config(config, dev)
manual_config(auth, config, dev)
else:
auto_config(config, dev)
auto_config(auth, config, dev)

if dev:
add_entities(dev, True)


def manual_config(config, dev):
def manual_config(auth, config, dev):
"""Handle manual configuration."""
import pyatmo

all_classes = all_product_classes()
not_handled = {}

for data_class in all_classes:
data = NetAtmoData(NETATMO_AUTH, data_class,
data = NetAtmoData(auth, data_class,
config.get(CONF_STATION))
try:
# Iterate each module
Expand All @@ -107,12 +110,12 @@ def manual_config(config, dev):
_LOGGER.error('Module name: "%s" not found', module_name)


def auto_config(config, dev):
def auto_config(auth, config, dev):
"""Handle auto configuration."""
import pyatmo

for data_class in all_product_classes():
data = NetAtmoData(NETATMO_AUTH, data_class, config.get(CONF_STATION))
data = NetAtmoData(auth, data_class, config.get(CONF_STATION))
try:
for module_name in data.get_module_names():
for variable in \
Expand Down