diff --git a/custom_components/vimar/__init__.py b/custom_components/vimar/__init__.py index cc7411d..327c965 100755 --- a/custom_components/vimar/__init__.py +++ b/custom_components/vimar/__init__.py @@ -1,50 +1,48 @@ """Vimar Platform integration.""" -from datetime import timedelta -import logging import asyncio -import async_timeout +import logging import os +from datetime import timedelta +from typing import Tuple -from homeassistant.helpers.typing import HomeAssistantType, ConfigType -from homeassistant.const import ( - CONF_PORT, CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_TIMEOUT) -from homeassistant.exceptions import PlatformNotReady -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +import async_timeout import homeassistant.helpers.config_validation as cv import voluptuous as vol -from .vimarlink.vimarlink import (VimarLink, VimarProject, VimarApiError) - -from .const import ( - DOMAIN, - CONF_SCHEMA, - CONF_CERTIFICATE, - CONF_GLOBAL_CHANNEL_ID, - CONF_IGNORE_PLATFORM, - CONF_OVERRIDE, - DEFAULT_USERNAME, - DEFAULT_SCHEMA, - DEFAULT_PORT, - DEFAULT_CERTIFICATE, - DEFAULT_TIMEOUT, - AVAILABLE_PLATFORMS -) +from homeassistant.const import (CONF_HOST, CONF_PASSWORD, CONF_PORT, + CONF_TIMEOUT, CONF_USERNAME) +from homeassistant.exceptions import PlatformNotReady +from homeassistant.helpers.typing import ConfigType, HomeAssistantType +from homeassistant.helpers.update_coordinator import (DataUpdateCoordinator, + UpdateFailed) + +from .const import (AVAILABLE_PLATFORMS, CONF_CERTIFICATE, + CONF_GLOBAL_CHANNEL_ID, CONF_IGNORE_PLATFORM, + CONF_OVERRIDE, CONF_SCHEMA, DEFAULT_CERTIFICATE, + DEFAULT_PORT, DEFAULT_SCHEMA, DEFAULT_TIMEOUT, + DEFAULT_USERNAME, DOMAIN) +from .vimarlink.vimarlink import VimarApiError, VimarLink, VimarProject _LOGGER = logging.getLogger(__name__) -CONFIG_SCHEMA = vol.Schema({ - DOMAIN: vol.Schema({ - vol.Required(CONF_HOST): cv.string, - vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string, - vol.Required(CONF_PASSWORD): cv.string, - vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, - vol.Optional(CONF_SCHEMA, default=DEFAULT_SCHEMA): cv.string, - vol.Optional(CONF_CERTIFICATE, default=DEFAULT_CERTIFICATE): vol.Any(cv.string, None), - vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Range(min=2, max=60), - vol.Optional(CONF_GLOBAL_CHANNEL_ID): vol.Range(min=1, max=99999), - vol.Optional(CONF_IGNORE_PLATFORM, default=[]): vol.All(cv.ensure_list, [cv.string]), - vol.Optional(CONF_OVERRIDE, default=[]): cv.ensure_list - }) -}, extra=vol.ALLOW_EXTRA) +CONFIG_SCHEMA = vol.Schema( + { + DOMAIN: vol.Schema( + { + vol.Required(CONF_HOST): cv.string, + vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string, + vol.Required(CONF_PASSWORD): cv.string, + vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, + vol.Optional(CONF_SCHEMA, default=DEFAULT_SCHEMA): cv.string, + vol.Optional(CONF_CERTIFICATE, default=DEFAULT_CERTIFICATE): vol.Any(cv.string, None), + vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Range(min=2, max=60), + vol.Optional(CONF_GLOBAL_CHANNEL_ID): vol.Range(min=1, max=99999), + vol.Optional(CONF_IGNORE_PLATFORM, default=[]): vol.All(cv.ensure_list, [cv.string]), + vol.Optional(CONF_OVERRIDE, default=[]): cv.ensure_list, + } + ) + }, + extra=vol.ALLOW_EXTRA, +) @asyncio.coroutine @@ -111,14 +109,13 @@ async def async_api_update(): ignored_platforms = vimarconfig.get(CONF_IGNORE_PLATFORM) for device_type, platform in AVAILABLE_PLATFORMS.items(): - if (not ignored_platforms or platform not in ignored_platforms): + if not ignored_platforms or platform not in ignored_platforms: device_count = vimarproject.platform_exists(device_type) if device_count: _LOGGER.debug("load platform %s with %d %s", platform, device_count, device_type) - hass.async_create_task(hass.helpers.discovery.async_load_platform( - platform, DOMAIN, {"hass_data_key": device_type}, config)) + hass.async_create_task(hass.helpers.discovery.async_load_platform(platform, DOMAIN, {"hass_data_key": device_type}, config)) else: - _LOGGER.warning('ignore platform: %s', platform) + _LOGGER.warning("ignore platform: %s", platform) # States are in the format DOMAIN.OBJECT_ID. # hass.states.async_set("vimar.Hello_World", "Works!") @@ -144,9 +141,8 @@ async def async_api_update(): # return True -async def _validate_vimar_credentials(hass: HomeAssistantType, vimarconfig: ConfigType) -> [VimarProject, VimarLink]: +async def _validate_vimar_credentials(hass: HomeAssistantType, vimarconfig: ConfigType) -> Tuple[VimarProject, VimarLink]: """Validate Vimar credential config.""" - schema = vimarconfig.get(CONF_SCHEMA) host = vimarconfig.get(CONF_HOST) port = vimarconfig.get(CONF_PORT) @@ -160,8 +156,7 @@ async def _validate_vimar_credentials(hass: HomeAssistantType, vimarconfig: Conf device_overrides = vimarconfig.get(CONF_OVERRIDE, []) # initialize a new VimarLink object - vimarconnection = VimarLink( - schema, host, port, username, password, certificate, timeout) + vimarconnection = VimarLink(schema, host, port, username, password, certificate, timeout) # will hold all the devices and their states vimarproject = VimarProject(vimarconnection, device_overrides) @@ -181,8 +176,7 @@ async def _validate_vimar_credentials(hass: HomeAssistantType, vimarconfig: Conf if not valid_certificate: raise PlatformNotReady else: - _LOGGER.info( - "Vimar CA Certificate is already in place: %s", certificate) + _LOGGER.info("Vimar CA Certificate is already in place: %s", certificate) # Verify that passed in configuration works # starting it outside MainThread diff --git a/custom_components/vimar/climate.py b/custom_components/vimar/climate.py index 642649a..b87c64a 100755 --- a/custom_components/vimar/climate.py +++ b/custom_components/vimar/climate.py @@ -1,43 +1,30 @@ """Platform for climate integration.""" import logging -from homeassistant.components.climate.const import ( - CURRENT_HVAC_COOL, - CURRENT_HVAC_HEAT, - CURRENT_HVAC_IDLE, - CURRENT_HVAC_OFF, - HVAC_MODE_AUTO, - HVAC_MODE_COOL, - HVAC_MODE_HEAT, - HVAC_MODE_OFF, - SUPPORT_TARGET_TEMPERATURE, - SUPPORT_FAN_MODE, - SUPPORT_AUX_HEAT, - FAN_ON, - FAN_OFF, - FAN_LOW, - FAN_MEDIUM, - FAN_HIGH, -) + +from homeassistant.components.climate.const import (CURRENT_HVAC_COOL, + CURRENT_HVAC_HEAT, + CURRENT_HVAC_IDLE, + CURRENT_HVAC_OFF, FAN_HIGH, + FAN_LOW, FAN_MEDIUM, + FAN_OFF, FAN_ON, + HVAC_MODE_AUTO, + HVAC_MODE_COOL, + HVAC_MODE_HEAT, + HVAC_MODE_OFF, + SUPPORT_AUX_HEAT, + SUPPORT_FAN_MODE, + SUPPORT_TARGET_TEMPERATURE) from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT -from .const import ( - # DOMAIN, - VIMAR_CLIMATE_COOL, - VIMAR_CLIMATE_COOL_I, - VIMAR_CLIMATE_COOL_II, - VIMAR_CLIMATE_HEAT, - VIMAR_CLIMATE_HEAT_I, - VIMAR_CLIMATE_HEAT_II, - VIMAR_CLIMATE_AUTO, - VIMAR_CLIMATE_AUTO_I, - VIMAR_CLIMATE_AUTO_II, - VIMAR_CLIMATE_MANUAL, - VIMAR_CLIMATE_MANUAL_I, - VIMAR_CLIMATE_MANUAL_II, - VIMAR_CLIMATE_OFF, - VIMAR_CLIMATE_OFF_I, - VIMAR_CLIMATE_OFF_II, -) + +from .const import (VIMAR_CLIMATE_AUTO, VIMAR_CLIMATE_AUTO_I, # DOMAIN, + VIMAR_CLIMATE_AUTO_II, VIMAR_CLIMATE_COOL, + VIMAR_CLIMATE_COOL_I, VIMAR_CLIMATE_COOL_II, + VIMAR_CLIMATE_HEAT, VIMAR_CLIMATE_HEAT_I, + VIMAR_CLIMATE_HEAT_II, VIMAR_CLIMATE_MANUAL, + VIMAR_CLIMATE_MANUAL_I, VIMAR_CLIMATE_MANUAL_II, + VIMAR_CLIMATE_OFF, VIMAR_CLIMATE_OFF_I, + VIMAR_CLIMATE_OFF_II) from .vimar_entity import VimarEntity, vimar_setup_platform try: diff --git a/custom_components/vimar/cover.py b/custom_components/vimar/cover.py index 98d11fd..cce26ad 100755 --- a/custom_components/vimar/cover.py +++ b/custom_components/vimar/cover.py @@ -1,18 +1,13 @@ """Platform for cover integration.""" import logging -from homeassistant.components.cover import ( - SUPPORT_CLOSE, - SUPPORT_OPEN, - SUPPORT_STOP, - SUPPORT_SET_POSITION, - SUPPORT_OPEN_TILT, - SUPPORT_CLOSE_TILT, - SUPPORT_STOP_TILT, - SUPPORT_SET_TILT_POSITION, - ATTR_POSITION, - ATTR_TILT_POSITION, -) + +from homeassistant.components.cover import (ATTR_POSITION, ATTR_TILT_POSITION, + SUPPORT_CLOSE, SUPPORT_CLOSE_TILT, + SUPPORT_OPEN, SUPPORT_OPEN_TILT, + SUPPORT_SET_POSITION, + SUPPORT_SET_TILT_POSITION, + SUPPORT_STOP, SUPPORT_STOP_TILT) from .vimar_entity import VimarEntity, vimar_setup_platform diff --git a/custom_components/vimar/light.py b/custom_components/vimar/light.py index 9720af4..2a9fd8b 100755 --- a/custom_components/vimar/light.py +++ b/custom_components/vimar/light.py @@ -1,8 +1,10 @@ """Platform for light integration.""" import logging -from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR + import homeassistant.util.color as color_util +from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR + from .vimar_entity import VimarEntity, vimar_setup_platform try: diff --git a/custom_components/vimar/media_player.py b/custom_components/vimar/media_player.py index dcdb762..45add0e 100755 --- a/custom_components/vimar/media_player.py +++ b/custom_components/vimar/media_player.py @@ -1,22 +1,20 @@ """Platform for media player integration.""" import logging + from homeassistant.components.media_player.const import ( - MEDIA_TYPE_MUSIC, MEDIA_TYPE_CHANNEL, + MEDIA_TYPE_MUSIC, + SUPPORT_NEXT_TRACK, + SUPPORT_SELECT_SOURCE, + SUPPORT_TURN_OFF, + SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, SUPPORT_VOLUME_STEP, - SUPPORT_TURN_ON, - SUPPORT_TURN_OFF, - SUPPORT_SELECT_SOURCE, - SUPPORT_NEXT_TRACK, -) -from homeassistant.const import ( - # STATE_IDLE, - STATE_OFF, - STATE_PLAYING, ) +from homeassistant.const import STATE_OFF, STATE_PLAYING # STATE_IDLE, + from .vimar_entity import VimarEntity, vimar_setup_platform try: diff --git a/custom_components/vimar/scene.py b/custom_components/vimar/scene.py index 2036c9a..f9dcd5e 100755 --- a/custom_components/vimar/scene.py +++ b/custom_components/vimar/scene.py @@ -1,7 +1,9 @@ """Platform for scene integration.""" import logging + from homeassistant.components.scene import Scene + from .vimar_entity import VimarEntity, vimar_setup_platform _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/vimar/sensor.py b/custom_components/vimar/sensor.py index 4bd20d1..79fbe74 100755 --- a/custom_components/vimar/sensor.py +++ b/custom_components/vimar/sensor.py @@ -5,16 +5,16 @@ # from datetime import timedelta from homeassistant.const import ( - POWER_KILO_WATT, - ENERGY_KILO_WATT_HOUR, - TEMP_CELSIUS, - SPEED_METERS_PER_SECOND, - DEVICE_CLASS_ENERGY, DEVICE_CLASS_CURRENT, - DEVICE_CLASS_TIMESTAMP, + DEVICE_CLASS_ENERGY, + DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_POWER, DEVICE_CLASS_TEMPERATURE, - DEVICE_CLASS_ILLUMINANCE, + DEVICE_CLASS_TIMESTAMP, + ENERGY_KILO_WATT_HOUR, + POWER_KILO_WATT, + SPEED_METERS_PER_SECOND, + TEMP_CELSIUS, ) try: @@ -23,6 +23,7 @@ from homeassistant.const import VOLT as ELECTRIC_POTENTIAL_VOLT from homeassistant.helpers.entity import Entity + from .const import DOMAIN from .vimar_entity import VimarEntity, vimar_setup_platform diff --git a/custom_components/vimar/switch.py b/custom_components/vimar/switch.py index 3b261ec..18c1442 100755 --- a/custom_components/vimar/switch.py +++ b/custom_components/vimar/switch.py @@ -1,7 +1,9 @@ """Platform for switch integration.""" import logging + from homeassistant.helpers.entity import ToggleEntity + from .vimar_entity import VimarEntity, vimar_setup_platform _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/vimar/vimar_entity.py b/custom_components/vimar/vimar_entity.py index 030ecae..5581f50 100755 --- a/custom_components/vimar/vimar_entity.py +++ b/custom_components/vimar/vimar_entity.py @@ -1,10 +1,11 @@ """Insteon base entity.""" import logging -from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.entity import Entity -from .vimarlink.vimarlink import (VimarLink, VimarProject) +from homeassistant.helpers.typing import HomeAssistantType + from .const import DOMAIN +from .vimarlink.vimarlink import VimarLink, VimarProject _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/vimar/vimarlink/__init__.py b/custom_components/vimar/vimarlink/__init__.py index 7bf69b2..9d4f093 100755 --- a/custom_components/vimar/vimarlink/__init__.py +++ b/custom_components/vimar/vimarlink/__init__.py @@ -1 +1 @@ -"""VIMAR by-me link library""" +"""VIMAR by-me link library.""" diff --git a/custom_components/vimar/vimarlink/vimarlink.py b/custom_components/vimar/vimarlink/vimarlink.py index 90edeb2..7654db4 100755 --- a/custom_components/vimar/vimarlink/vimarlink.py +++ b/custom_components/vimar/vimarlink/vimarlink.py @@ -2,24 +2,18 @@ import logging import sys - # for communicating with vimar webserver import xml.etree.cElementTree as xmlTree from xml.etree import ElementTree + import requests from requests.exceptions import HTTPError -from .const import ( - DEVICE_TYPE_LIGHTS, - DEVICE_TYPE_COVERS, - DEVICE_TYPE_SWITCHES, - DEVICE_TYPE_CLIMATES, - DEVICE_TYPE_MEDIA_PLAYERS, - DEVICE_TYPE_SCENES, - # DEVICE_TYPE_FANS, - DEVICE_TYPE_SENSORS, - DEVICE_TYPE_OTHERS, -) +from .const import (DEVICE_TYPE_CLIMATES, # DEVICE_TYPE_FANS, + DEVICE_TYPE_COVERS, DEVICE_TYPE_LIGHTS, + DEVICE_TYPE_MEDIA_PLAYERS, DEVICE_TYPE_OTHERS, + DEVICE_TYPE_SCENES, DEVICE_TYPE_SENSORS, + DEVICE_TYPE_SWITCHES) _LOGGER = logging.getLogger(__name__) MAX_ROWS_PER_REQUEST = 300 diff --git a/examples/example.py b/examples/example.py index 46492f6..d0a8545 100755 --- a/examples/example.py +++ b/examples/example.py @@ -1,8 +1,8 @@ """Vimar Platform example without HA.""" # import async_timeout -import os -import configparser import argparse +import configparser +import os # those imports only work in that directory # this will be easier to use, as soon as we have a separate python package