mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Refactor open garage (#26179)
* Use defined constant in open garage * refactor open garage * style * style * refactor * refactor * refactor * remove vehicle state
This commit is contained in:
parent
7238eb9bac
commit
d4bd5a180c
@ -6,19 +6,20 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components.cover import (
|
from homeassistant.components.cover import (
|
||||||
CoverDevice,
|
CoverDevice,
|
||||||
|
DEVICE_CLASS_GARAGE,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
SUPPORT_OPEN,
|
SUPPORT_OPEN,
|
||||||
SUPPORT_CLOSE,
|
SUPPORT_CLOSE,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_DEVICE,
|
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
STATE_UNKNOWN,
|
|
||||||
STATE_CLOSED,
|
STATE_CLOSED,
|
||||||
STATE_OPEN,
|
STATE_OPEN,
|
||||||
CONF_COVERS,
|
CONF_COVERS,
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
|
STATE_CLOSING,
|
||||||
|
STATE_OPENING,
|
||||||
)
|
)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
@ -28,17 +29,11 @@ ATTR_DISTANCE_SENSOR = "distance_sensor"
|
|||||||
ATTR_DOOR_STATE = "door_state"
|
ATTR_DOOR_STATE = "door_state"
|
||||||
ATTR_SIGNAL_STRENGTH = "wifi_signal"
|
ATTR_SIGNAL_STRENGTH = "wifi_signal"
|
||||||
|
|
||||||
CONF_DEVICE_ID = "device_id"
|
|
||||||
CONF_DEVICE_KEY = "device_key"
|
CONF_DEVICE_KEY = "device_key"
|
||||||
|
|
||||||
DEFAULT_NAME = "OpenGarage"
|
DEFAULT_NAME = "OpenGarage"
|
||||||
DEFAULT_PORT = 80
|
DEFAULT_PORT = 80
|
||||||
|
|
||||||
STATE_CLOSING = "closing"
|
|
||||||
STATE_OFFLINE = "offline"
|
|
||||||
STATE_OPENING = "opening"
|
|
||||||
STATE_STOPPED = "stopped"
|
|
||||||
|
|
||||||
STATES_MAP = {0: STATE_CLOSED, 1: STATE_OPEN}
|
STATES_MAP = {0: STATE_CLOSED, 1: STATE_OPEN}
|
||||||
|
|
||||||
COVER_SCHEMA = vol.Schema(
|
COVER_SCHEMA = vol.Schema(
|
||||||
@ -60,16 +55,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
covers = []
|
covers = []
|
||||||
devices = config.get(CONF_COVERS)
|
devices = config.get(CONF_COVERS)
|
||||||
|
|
||||||
for device_id, device_config in devices.items():
|
for device_config in devices.values():
|
||||||
args = {
|
args = {
|
||||||
CONF_NAME: device_config.get(CONF_NAME),
|
CONF_NAME: device_config.get(CONF_NAME),
|
||||||
CONF_HOST: device_config.get(CONF_HOST),
|
CONF_HOST: device_config.get(CONF_HOST),
|
||||||
CONF_PORT: device_config.get(CONF_PORT),
|
CONF_PORT: device_config.get(CONF_PORT),
|
||||||
CONF_DEVICE_ID: device_config.get(CONF_DEVICE, device_id),
|
|
||||||
CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY),
|
CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY),
|
||||||
}
|
}
|
||||||
|
|
||||||
covers.append(OpenGarageCover(hass, args))
|
covers.append(OpenGarageCover(args))
|
||||||
|
|
||||||
add_entities(covers, True)
|
add_entities(covers, True)
|
||||||
|
|
||||||
@ -77,17 +71,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class OpenGarageCover(CoverDevice):
|
class OpenGarageCover(CoverDevice):
|
||||||
"""Representation of a OpenGarage cover."""
|
"""Representation of a OpenGarage cover."""
|
||||||
|
|
||||||
def __init__(self, hass, args):
|
def __init__(self, args):
|
||||||
"""Initialize the cover."""
|
"""Initialize the cover."""
|
||||||
self.opengarage_url = "http://{}:{}".format(args[CONF_HOST], args[CONF_PORT])
|
self.opengarage_url = "http://{}:{}".format(args[CONF_HOST], args[CONF_PORT])
|
||||||
self.hass = hass
|
|
||||||
self._name = args[CONF_NAME]
|
self._name = args[CONF_NAME]
|
||||||
self.device_id = args["device_id"]
|
|
||||||
self._device_key = args[CONF_DEVICE_KEY]
|
self._device_key = args[CONF_DEVICE_KEY]
|
||||||
self._state = None
|
self._state = None
|
||||||
self._state_before_move = None
|
self._state_before_move = None
|
||||||
self.dist = None
|
self._device_state_attributes = {}
|
||||||
self.signal = None
|
|
||||||
self._available = True
|
self._available = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -103,93 +94,91 @@ class OpenGarageCover(CoverDevice):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the device state attributes."""
|
"""Return the device state attributes."""
|
||||||
data = {}
|
return self._device_state_attributes
|
||||||
|
|
||||||
if self.signal is not None:
|
|
||||||
data[ATTR_SIGNAL_STRENGTH] = self.signal
|
|
||||||
|
|
||||||
if self.dist is not None:
|
|
||||||
data[ATTR_DISTANCE_SENSOR] = self.dist
|
|
||||||
|
|
||||||
if self._state is not None:
|
|
||||||
data[ATTR_DOOR_STATE] = self._state
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_closed(self):
|
def is_closed(self):
|
||||||
"""Return if the cover is closed."""
|
"""Return if the cover is closed."""
|
||||||
if self._state in [STATE_UNKNOWN, STATE_OFFLINE]:
|
if self._state is None:
|
||||||
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):
|
def close_cover(self, **kwargs):
|
||||||
"""Close the cover."""
|
"""Close the cover."""
|
||||||
if self._state not in [STATE_CLOSED, STATE_CLOSING]:
|
if self._state in [STATE_CLOSED, STATE_CLOSING]:
|
||||||
self._state_before_move = self._state
|
return
|
||||||
self._state = STATE_CLOSING
|
self._state_before_move = self._state
|
||||||
self._push_button()
|
self._state = STATE_CLOSING
|
||||||
|
self._push_button()
|
||||||
|
|
||||||
def open_cover(self, **kwargs):
|
def open_cover(self, **kwargs):
|
||||||
"""Open the cover."""
|
"""Open the cover."""
|
||||||
if self._state not in [STATE_OPEN, STATE_OPENING]:
|
if self._state in [STATE_OPEN, STATE_OPENING]:
|
||||||
self._state_before_move = self._state
|
return
|
||||||
self._state = STATE_OPENING
|
self._state_before_move = self._state
|
||||||
self._push_button()
|
self._state = STATE_OPENING
|
||||||
|
self._push_button()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get updated status from API."""
|
"""Get updated status from API."""
|
||||||
try:
|
try:
|
||||||
status = self._get_status()
|
status = requests.get(
|
||||||
if self._name is None:
|
"{}/jc".format(self.opengarage_url), timeout=10
|
||||||
if status["name"] is not None:
|
).json()
|
||||||
self._name = status["name"]
|
|
||||||
state = STATES_MAP.get(status.get("door"), STATE_UNKNOWN)
|
|
||||||
if self._state_before_move is not None:
|
|
||||||
if self._state_before_move != state:
|
|
||||||
self._state = state
|
|
||||||
self._state_before_move = None
|
|
||||||
else:
|
|
||||||
self._state = state
|
|
||||||
|
|
||||||
_LOGGER.debug("%s status: %s", self._name, self._state)
|
|
||||||
self.signal = status.get("rssi")
|
|
||||||
self.dist = status.get("dist")
|
|
||||||
self._available = True
|
|
||||||
except requests.exceptions.RequestException as ex:
|
except requests.exceptions.RequestException as ex:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
|
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
|
||||||
)
|
)
|
||||||
self._state = STATE_OFFLINE
|
self._available = False
|
||||||
|
return
|
||||||
|
|
||||||
def _get_status(self):
|
if self._name is None and status["name"] is not None:
|
||||||
"""Get latest status."""
|
self._name = status["name"]
|
||||||
url = "{}/jc".format(self.opengarage_url)
|
state = STATES_MAP.get(status.get("door"))
|
||||||
ret = requests.get(url, timeout=10)
|
if self._state_before_move is not None:
|
||||||
return ret.json()
|
if self._state_before_move != state:
|
||||||
|
self._state = state
|
||||||
|
self._state_before_move = None
|
||||||
|
else:
|
||||||
|
self._state = state
|
||||||
|
|
||||||
|
_LOGGER.debug("%s status: %s", self._name, self._state)
|
||||||
|
if status.get("rssi") is not None:
|
||||||
|
self._device_state_attributes[ATTR_SIGNAL_STRENGTH] = status.get("rssi")
|
||||||
|
if status.get("dist") is not None:
|
||||||
|
self._device_state_attributes[ATTR_DISTANCE_SENSOR] = status.get("dist")
|
||||||
|
if self._state is not None:
|
||||||
|
self._device_state_attributes[ATTR_DOOR_STATE] = self._state
|
||||||
|
|
||||||
|
self._available = True
|
||||||
|
|
||||||
def _push_button(self):
|
def _push_button(self):
|
||||||
"""Send commands to API."""
|
"""Send commands to API."""
|
||||||
url = "{}/cc?dkey={}&click=1".format(self.opengarage_url, self._device_key)
|
result = -1
|
||||||
try:
|
try:
|
||||||
response = requests.get(url, timeout=10).json()
|
result = requests.get(
|
||||||
if response["result"] == 2:
|
"{}/cc?dkey={}&click=1".format(self.opengarage_url, self._device_key),
|
||||||
_LOGGER.error(
|
timeout=10,
|
||||||
"Unable to control %s: Device key is incorrect", self._name
|
).json()["result"]
|
||||||
)
|
|
||||||
self._state = self._state_before_move
|
|
||||||
self._state_before_move = None
|
|
||||||
except requests.exceptions.RequestException as ex:
|
except requests.exceptions.RequestException as ex:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
|
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
|
||||||
)
|
)
|
||||||
self._state = self._state_before_move
|
if result == 1:
|
||||||
self._state_before_move = None
|
return
|
||||||
|
|
||||||
|
if result == 2:
|
||||||
|
_LOGGER.error("Unable to control %s: Device key is incorrect", self._name)
|
||||||
|
elif result > 2:
|
||||||
|
_LOGGER.error("Unable to control %s: Error code %s", self._name, result)
|
||||||
|
|
||||||
|
self._state = self._state_before_move
|
||||||
|
self._state_before_move = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||||
return "garage"
|
return DEVICE_CLASS_GARAGE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user