Open garage, move code to an external library. (#35462)

* Move opengarage code to separate library

* Move opengarage code to separate library

* Move opengarage code to separate library

* remove unique_id

* style
This commit is contained in:
Daniel Høyer Iversen 2020-05-10 21:30:16 +02:00 committed by GitHub
parent a73440ebe8
commit e4263afd64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 42 deletions

View File

@ -285,6 +285,7 @@ homeassistant/components/onboarding/* @home-assistant/core
homeassistant/components/onewire/* @garbled1 homeassistant/components/onewire/* @garbled1
homeassistant/components/onvif/* @hunterjm homeassistant/components/onvif/* @hunterjm
homeassistant/components/openerz/* @misialq homeassistant/components/openerz/* @misialq
homeassistant/components/opengarage/* @danielhiversen
homeassistant/components/opentherm_gw/* @mvn23 homeassistant/components/opentherm_gw/* @mvn23
homeassistant/components/openuv/* @bachya homeassistant/components/openuv/* @bachya
homeassistant/components/openweathermap/* @fabaff homeassistant/components/openweathermap/* @fabaff

View File

@ -1,7 +1,7 @@
"""Platform for the opengarage.io cover component.""" """Platform for the opengarage.io cover component."""
import logging import logging
import requests import opengarage
import voluptuous as vol import voluptuous as vol
from homeassistant.components.cover import ( from homeassistant.components.cover import (
@ -23,6 +23,7 @@ from homeassistant.const import (
STATE_OPEN, STATE_OPEN,
STATE_OPENING, STATE_OPENING,
) )
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -60,16 +61,19 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
devices = config.get(CONF_COVERS) devices = config.get(CONF_COVERS)
for device_config in devices.values(): for device_config in devices.values():
args = { opengarage_url = (
CONF_NAME: device_config.get(CONF_NAME), f"{'https' if device_config[CONF_SSL] else 'http'}://"
CONF_HOST: device_config.get(CONF_HOST), f"{device_config.get(CONF_HOST)}:{device_config.get(CONF_PORT)}"
CONF_PORT: device_config.get(CONF_PORT), )
CONF_SSL: device_config[CONF_SSL],
CONF_VERIFY_SSL: device_config.get(CONF_VERIFY_SSL),
CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY),
}
covers.append(OpenGarageCover(args)) open_garage = opengarage.OpenGarage(
opengarage_url,
device_config[CONF_DEVICE_KEY],
device_config[CONF_VERIFY_SSL],
async_get_clientsession(hass),
)
covers.append(OpenGarageCover(device_config.get(CONF_NAME), open_garage))
add_entities(covers, True) add_entities(covers, True)
@ -77,19 +81,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class OpenGarageCover(CoverEntity): class OpenGarageCover(CoverEntity):
"""Representation of a OpenGarage cover.""" """Representation of a OpenGarage cover."""
def __init__(self, args): def __init__(self, name, open_garage):
"""Initialize the cover.""" """Initialize the cover."""
self.opengarage_url = ( self._name = name
f"{'https' if args[CONF_SSL] else 'http'}://" self._open_garage = open_garage
f"{args[CONF_HOST]}:{args[CONF_PORT]}"
)
self._name = args[CONF_NAME]
self._device_key = args[CONF_DEVICE_KEY]
self._state = None self._state = None
self._state_before_move = None self._state_before_move = None
self._device_state_attributes = {} self._device_state_attributes = {}
self._available = True self._available = True
self._verify_ssl = args[CONF_VERIFY_SSL]
@property @property
def name(self): def name(self):
@ -113,30 +112,27 @@ class OpenGarageCover(CoverEntity):
return None return None
return self._state in [STATE_CLOSED, STATE_OPENING] return self._state in [STATE_CLOSED, STATE_OPENING]
def close_cover(self, **kwargs): async def async_close_cover(self, **kwargs):
"""Close the cover.""" """Close the cover."""
if self._state in [STATE_CLOSED, STATE_CLOSING]: if self._state in [STATE_CLOSED, STATE_CLOSING]:
return return
self._state_before_move = self._state self._state_before_move = self._state
self._state = STATE_CLOSING self._state = STATE_CLOSING
self._push_button() await self._push_button()
def open_cover(self, **kwargs): async def async_open_cover(self, **kwargs):
"""Open the cover.""" """Open the cover."""
if self._state in [STATE_OPEN, STATE_OPENING]: if self._state in [STATE_OPEN, STATE_OPENING]:
return return
self._state_before_move = self._state self._state_before_move = self._state
self._state = STATE_OPENING self._state = STATE_OPENING
self._push_button() await self._push_button()
def update(self): async def async_update(self):
"""Get updated status from API.""" """Get updated status from API."""
try: status = await self._open_garage.update_state()
status = requests.get(f"{self.opengarage_url}/jc", timeout=10).json() if status is None:
except requests.exceptions.RequestException as ex: _LOGGER.error("Unable to connect to OpenGarage device")
_LOGGER.error(
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
)
self._available = False self._available = False
return return
@ -160,19 +156,11 @@ class OpenGarageCover(CoverEntity):
self._available = True self._available = True
def _push_button(self): async def _push_button(self):
"""Send commands to API.""" """Send commands to API."""
result = -1 result = await self._open_garage.push_button()
try: if result is None:
result = requests.get( _LOGGER.error("Unable to connect to OpenGarage device")
f"{self.opengarage_url}/cc?dkey={self._device_key}&click=1",
timeout=10,
verify=self._verify_ssl,
).json()["result"]
except requests.exceptions.RequestException as ex:
_LOGGER.error(
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
)
if result == 1: if result == 1:
return return

View File

@ -2,5 +2,8 @@
"domain": "opengarage", "domain": "opengarage",
"name": "OpenGarage", "name": "OpenGarage",
"documentation": "https://www.home-assistant.io/integrations/opengarage", "documentation": "https://www.home-assistant.io/integrations/opengarage",
"codeowners": [] "codeowners": [
"@danielhiversen"
],
"requirements": ["open-garage==0.1.2"]
} }

View File

@ -993,6 +993,9 @@ onkyo-eiscp==1.2.7
# homeassistant.components.onvif # homeassistant.components.onvif
onvif-zeep-async==0.2.0 onvif-zeep-async==0.2.0
# homeassistant.components.opengarage
open-garage==0.1.2
# homeassistant.components.opencv # homeassistant.components.opencv
# opencv-python-headless==4.2.0.32 # opencv-python-headless==4.2.0.32