Refactor netatmo to use hass.data (#23429)
* Refactor NETATMO_AUTH to use hass.data * Minor cleanup * Rename conf to auth and other suggestions by Martin * Revert webhook name change * Rename constant * Move auth * Don't use hass.data.get() * Fix auth string
This commit is contained in:
parent
5dbf58d67f
commit
b84ba93c42
6 changed files with 44 additions and 22 deletions
|
@ -12,6 +12,8 @@ from homeassistant.helpers import discovery
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
|
from .const import DOMAIN, DATA_NETATMO_AUTH
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DATA_PERSONS = 'netatmo_persons'
|
DATA_PERSONS = 'netatmo_persons'
|
||||||
|
@ -20,8 +22,6 @@ DATA_WEBHOOK_URL = 'netatmo_webhook_url'
|
||||||
CONF_SECRET_KEY = 'secret_key'
|
CONF_SECRET_KEY = 'secret_key'
|
||||||
CONF_WEBHOOKS = 'webhooks'
|
CONF_WEBHOOKS = 'webhooks'
|
||||||
|
|
||||||
DOMAIN = 'netatmo'
|
|
||||||
|
|
||||||
SERVICE_ADDWEBHOOK = 'addwebhook'
|
SERVICE_ADDWEBHOOK = 'addwebhook'
|
||||||
SERVICE_DROPWEBHOOK = 'dropwebhook'
|
SERVICE_DROPWEBHOOK = 'dropwebhook'
|
||||||
|
|
||||||
|
@ -83,10 +83,9 @@ def setup(hass, config):
|
||||||
"""Set up the Netatmo devices."""
|
"""Set up the Netatmo devices."""
|
||||||
import pyatmo
|
import pyatmo
|
||||||
|
|
||||||
global NETATMO_AUTH
|
|
||||||
hass.data[DATA_PERSONS] = {}
|
hass.data[DATA_PERSONS] = {}
|
||||||
try:
|
try:
|
||||||
NETATMO_AUTH = pyatmo.ClientAuth(
|
auth = pyatmo.ClientAuth(
|
||||||
config[DOMAIN][CONF_API_KEY], config[DOMAIN][CONF_SECRET_KEY],
|
config[DOMAIN][CONF_API_KEY], config[DOMAIN][CONF_SECRET_KEY],
|
||||||
config[DOMAIN][CONF_USERNAME], config[DOMAIN][CONF_PASSWORD],
|
config[DOMAIN][CONF_USERNAME], config[DOMAIN][CONF_PASSWORD],
|
||||||
'read_station read_camera access_camera '
|
'read_station read_camera access_camera '
|
||||||
|
@ -96,6 +95,9 @@ def setup(hass, config):
|
||||||
_LOGGER.error("Unable to connect to Netatmo API")
|
_LOGGER.error("Unable to connect to Netatmo API")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Store config to be used during entry setup
|
||||||
|
hass.data[DATA_NETATMO_AUTH] = auth
|
||||||
|
|
||||||
if config[DOMAIN][CONF_DISCOVERY]:
|
if config[DOMAIN][CONF_DISCOVERY]:
|
||||||
for component in 'camera', 'sensor', 'binary_sensor', 'climate':
|
for component in 'camera', 'sensor', 'binary_sensor', 'climate':
|
||||||
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
||||||
|
@ -107,7 +109,7 @@ def setup(hass, config):
|
||||||
webhook_id)
|
webhook_id)
|
||||||
hass.components.webhook.async_register(
|
hass.components.webhook.async_register(
|
||||||
DOMAIN, 'Netatmo', webhook_id, handle_webhook)
|
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(
|
hass.bus.listen_once(
|
||||||
EVENT_HOMEASSISTANT_STOP, dropwebhook)
|
EVENT_HOMEASSISTANT_STOP, dropwebhook)
|
||||||
|
|
||||||
|
@ -117,7 +119,7 @@ def setup(hass, config):
|
||||||
if url is None:
|
if url is None:
|
||||||
url = hass.data[DATA_WEBHOOK_URL]
|
url = hass.data[DATA_WEBHOOK_URL]
|
||||||
_LOGGER.info("Adding webhook for URL: %s", url)
|
_LOGGER.info("Adding webhook for URL: %s", url)
|
||||||
NETATMO_AUTH.addwebhook(url)
|
auth.addwebhook(url)
|
||||||
|
|
||||||
hass.services.register(
|
hass.services.register(
|
||||||
DOMAIN, SERVICE_ADDWEBHOOK, _service_addwebhook,
|
DOMAIN, SERVICE_ADDWEBHOOK, _service_addwebhook,
|
||||||
|
@ -126,7 +128,7 @@ def setup(hass, config):
|
||||||
def _service_dropwebhook(service):
|
def _service_dropwebhook(service):
|
||||||
"""Service to drop webhooks during runtime."""
|
"""Service to drop webhooks during runtime."""
|
||||||
_LOGGER.info("Dropping webhook")
|
_LOGGER.info("Dropping webhook")
|
||||||
NETATMO_AUTH.dropwebhook()
|
auth.dropwebhook()
|
||||||
|
|
||||||
hass.services.register(
|
hass.services.register(
|
||||||
DOMAIN, SERVICE_DROPWEBHOOK, _service_dropwebhook,
|
DOMAIN, SERVICE_DROPWEBHOOK, _service_dropwebhook,
|
||||||
|
@ -137,7 +139,8 @@ def setup(hass, config):
|
||||||
|
|
||||||
def dropwebhook(hass):
|
def dropwebhook(hass):
|
||||||
"""Drop the webhook subscription."""
|
"""Drop the webhook subscription."""
|
||||||
NETATMO_AUTH.dropwebhook()
|
auth = hass.data[DATA_NETATMO_AUTH]
|
||||||
|
auth.dropwebhook()
|
||||||
|
|
||||||
|
|
||||||
async def handle_webhook(hass, webhook_id, request):
|
async def handle_webhook(hass, webhook_id, request):
|
||||||
|
|
|
@ -8,7 +8,8 @@ from homeassistant.components.binary_sensor import (
|
||||||
from homeassistant.const import CONF_TIMEOUT
|
from homeassistant.const import CONF_TIMEOUT
|
||||||
from homeassistant.helpers import config_validation as cv
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -59,8 +60,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
module_name = None
|
module_name = None
|
||||||
|
|
||||||
import pyatmo
|
import pyatmo
|
||||||
|
|
||||||
|
auth = hass.data[DATA_NETATMO_AUTH]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = CameraData(hass, NETATMO_AUTH, home)
|
data = CameraData(hass, auth, home)
|
||||||
if not data.get_camera_names():
|
if not data.get_camera_names():
|
||||||
return None
|
return None
|
||||||
except pyatmo.NoDevice:
|
except pyatmo.NoDevice:
|
||||||
|
|
|
@ -9,7 +9,8 @@ from homeassistant.components.camera import (
|
||||||
from homeassistant.const import CONF_VERIFY_SSL
|
from homeassistant.const import CONF_VERIFY_SSL
|
||||||
from homeassistant.helpers import config_validation as cv
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -37,8 +38,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
verify_ssl = config.get(CONF_VERIFY_SSL, True)
|
verify_ssl = config.get(CONF_VERIFY_SSL, True)
|
||||||
quality = config.get(CONF_QUALITY, DEFAULT_QUALITY)
|
quality = config.get(CONF_QUALITY, DEFAULT_QUALITY)
|
||||||
import pyatmo
|
import pyatmo
|
||||||
|
|
||||||
|
auth = hass.data[DATA_NETATMO_AUTH]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = CameraData(hass, NETATMO_AUTH, home)
|
data = CameraData(hass, auth, home)
|
||||||
for camera_name in data.get_camera_names():
|
for camera_name in data.get_camera_names():
|
||||||
camera_type = data.get_camera_type(camera=camera_name, home=home)
|
camera_type = data.get_camera_type(camera=camera_name, home=home)
|
||||||
if CONF_CAMERAS in config:
|
if CONF_CAMERAS in config:
|
||||||
|
|
|
@ -14,7 +14,7 @@ from homeassistant.const import (
|
||||||
STATE_OFF, TEMP_CELSIUS, ATTR_TEMPERATURE, CONF_NAME)
|
STATE_OFF, TEMP_CELSIUS, ATTR_TEMPERATURE, CONF_NAME)
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
from . import NETATMO_AUTH
|
from .const import DATA_NETATMO_AUTH
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -68,8 +68,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the NetAtmo Thermostat."""
|
"""Set up the NetAtmo Thermostat."""
|
||||||
import pyatmo
|
import pyatmo
|
||||||
homes_conf = config.get(CONF_HOMES)
|
homes_conf = config.get(CONF_HOMES)
|
||||||
|
|
||||||
|
auth = hass.data[DATA_NETATMO_AUTH]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
home_data = HomeData(NETATMO_AUTH)
|
home_data = HomeData(auth)
|
||||||
except pyatmo.NoDevice:
|
except pyatmo.NoDevice:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -88,7 +91,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
for home in homes:
|
for home in homes:
|
||||||
_LOGGER.debug("Setting up %s ...", home)
|
_LOGGER.debug("Setting up %s ...", home)
|
||||||
try:
|
try:
|
||||||
room_data = ThermostatData(NETATMO_AUTH, home)
|
room_data = ThermostatData(auth, home)
|
||||||
except pyatmo.NoDevice:
|
except pyatmo.NoDevice:
|
||||||
continue
|
continue
|
||||||
for room_id in room_data.get_room_ids():
|
for room_id in room_data.get_room_ids():
|
||||||
|
|
5
homeassistant/components/netatmo/const.py
Normal file
5
homeassistant/components/netatmo/const.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
"""Constants used by the Netatmo component."""
|
||||||
|
DOMAIN = 'netatmo'
|
||||||
|
|
||||||
|
DATA_NETATMO = 'netatmo'
|
||||||
|
DATA_NETATMO_AUTH = 'netatmo_auth'
|
|
@ -12,7 +12,7 @@ from homeassistant.const import (
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import NETATMO_AUTH
|
from .const import DATA_NETATMO_AUTH
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -68,23 +68,26 @@ MODULE_TYPE_INDOOR = 'NAModule4'
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the available Netatmo weather sensors."""
|
"""Set up the available Netatmo weather sensors."""
|
||||||
dev = []
|
dev = []
|
||||||
|
auth = hass.data[DATA_NETATMO_AUTH]
|
||||||
|
|
||||||
if CONF_MODULES in config:
|
if CONF_MODULES in config:
|
||||||
manual_config(config, dev)
|
manual_config(auth, config, dev)
|
||||||
else:
|
else:
|
||||||
auto_config(config, dev)
|
auto_config(auth, config, dev)
|
||||||
|
|
||||||
if dev:
|
if dev:
|
||||||
add_entities(dev, True)
|
add_entities(dev, True)
|
||||||
|
|
||||||
|
|
||||||
def manual_config(config, dev):
|
def manual_config(auth, config, dev):
|
||||||
"""Handle manual configuration."""
|
"""Handle manual configuration."""
|
||||||
import pyatmo
|
import pyatmo
|
||||||
|
|
||||||
all_classes = all_product_classes()
|
all_classes = all_product_classes()
|
||||||
not_handled = {}
|
not_handled = {}
|
||||||
|
|
||||||
for data_class in all_classes:
|
for data_class in all_classes:
|
||||||
data = NetAtmoData(NETATMO_AUTH, data_class,
|
data = NetAtmoData(auth, data_class,
|
||||||
config.get(CONF_STATION))
|
config.get(CONF_STATION))
|
||||||
try:
|
try:
|
||||||
# Iterate each module
|
# Iterate each module
|
||||||
|
@ -107,12 +110,12 @@ def manual_config(config, dev):
|
||||||
_LOGGER.error('Module name: "%s" not found', module_name)
|
_LOGGER.error('Module name: "%s" not found', module_name)
|
||||||
|
|
||||||
|
|
||||||
def auto_config(config, dev):
|
def auto_config(auth, config, dev):
|
||||||
"""Handle auto configuration."""
|
"""Handle auto configuration."""
|
||||||
import pyatmo
|
import pyatmo
|
||||||
|
|
||||||
for data_class in all_product_classes():
|
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:
|
try:
|
||||||
for module_name in data.get_module_names():
|
for module_name in data.get_module_names():
|
||||||
for variable in \
|
for variable in \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue