Add BleBox integration (#32664)
Co-Authored-By: J. Nick Koston <nick@koston.org>
This commit is contained in:
1 parent
86d410d863
commit
8e071b69f4
16 files changed
+1176
No files matched your search
@@ -0,0 +1,121 @@
|
||||
"""The BleBox devices integration."""
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from blebox_uniapi.error import Error
|
||||
from blebox_uniapi.products import Products
|
||||
from blebox_uniapi.session import ApiHost
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from .const import DEFAULT_SETUP_TIMEOUT, DOMAIN, PRODUCT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = ["cover"]
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict):
|
||||
"""Set up the BleBox devices component."""
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Set up BleBox devices from a config entry."""
|
||||
|
||||
websession = async_get_clientsession(hass)
|
||||
|
||||
host = entry.data[CONF_HOST]
|
||||
port = entry.data[CONF_PORT]
|
||||
timeout = DEFAULT_SETUP_TIMEOUT
|
||||
|
||||
api_host = ApiHost(host, port, timeout, websession, hass.loop)
|
||||
|
||||
try:
|
||||
product = await Products.async_from_host(api_host)
|
||||
except Error as ex:
|
||||
_LOGGER.error("Identify failed at %s:%d (%s)", api_host.host, api_host.port, ex)
|
||||
raise ConfigEntryNotReady from ex
|
||||
|
||||
domain = hass.data.setdefault(DOMAIN, {})
|
||||
domain_entry = domain.setdefault(entry.entry_id, {})
|
||||
product = domain_entry.setdefault(PRODUCT, product)
|
||||
|
||||
for component in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(entry, component)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Unload a config entry."""
|
||||
unload_ok = all(
|
||||
await asyncio.gather(
|
||||
*[
|
||||
hass.config_entries.async_forward_entry_unload(entry, platform)
|
||||
for platform in PLATFORMS
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
@callback
|
||||
def create_blebox_entities(product, async_add, entity_klass, entity_type):
|
||||
"""Create entities from a BleBox product's features."""
|
||||
|
||||
entities = []
|
||||
for feature in product.features[entity_type]:
|
||||
entities.append(entity_klass(feature))
|
||||
|
||||
async_add(entities, True)
|
||||
|
||||
|
||||
class BleBoxEntity(Entity):
|
||||
"""Implements a common class for entities representing a BleBox feature."""
|
||||
|
||||
def __init__(self, feature):
|
||||
"""Initialize a BleBox entity."""
|
||||
self._feature = feature
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the internal entity name."""
|
||||
return self._feature.full_name
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique id."""
|
||||
return self._feature.unique_id
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the entity state."""
|
||||
try:
|
||||
await self._feature.async_update()
|
||||
except Error as ex:
|
||||
_LOGGER.error("Updating '%s' failed: %s", self.name, ex)
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return device information for this entity."""
|
||||
product = self._feature.product
|
||||
return {
|
||||
"identifiers": {(DOMAIN, product.unique_id)},
|
||||
"name": product.name,
|
||||
"manufacturer": product.brand,
|
||||
"model": product.model,
|
||||
"sw_version": product.firmware_version,
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
"""Config flow for BleBox devices integration."""
|
||||
import logging
|
||||
|
||||
from blebox_uniapi.error import Error, UnsupportedBoxVersion
|
||||
from blebox_uniapi.products import Products
|
||||
from blebox_uniapi.session import ApiHost
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import (
|
||||
ADDRESS_ALREADY_CONFIGURED,
|
||||
CANNOT_CONNECT,
|
||||
DEFAULT_HOST,
|
||||
DEFAULT_PORT,
|
||||
DEFAULT_SETUP_TIMEOUT,
|
||||
DOMAIN,
|
||||
UNKNOWN,
|
||||
UNSUPPORTED_VERSION,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def host_port(data):
|
||||
"""Return a list with host and port."""
|
||||
return (data[CONF_HOST], data[CONF_PORT])
|
||||
|
||||
|
||||
def create_schema(previous_input=None):
|
||||
"""Create a schema with given values as default."""
|
||||
if previous_input is not None:
|
||||
host, port = host_port(previous_input)
|
||||
else:
|
||||
host = DEFAULT_HOST
|
||||
port = DEFAULT_PORT
|
||||
|
||||
return vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_HOST, default=host): str,
|
||||
vol.Required(CONF_PORT, default=port): int,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
LOG_MSG = {
|
||||
UNSUPPORTED_VERSION: "Outdated firmware",
|
||||
CANNOT_CONNECT: "Failed to identify device",
|
||||
UNKNOWN: "Unknown error while identifying device",
|
||||
}
|
||||
|
||||
|
||||
class BleBoxConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for BleBox devices."""
|
||||
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the BleBox config flow."""
|
||||
self.device_config = {}
|
||||
|
||||
def handle_step_exception(
|
||||
self, step, exception, schema, host, port, message_id, log_fn
|
||||
):
|
||||
"""Handle step exceptions."""
|
||||
|
||||
log_fn("%s at %s:%d (%s)", LOG_MSG[message_id], host, port, exception)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=schema,
|
||||
errors={"base": message_id},
|
||||
description_placeholders={"address": f"{host}:{port}"},
|
||||
)
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle initial user-triggered config step."""
|
||||
|
||||
hass = self.hass
|
||||
schema = create_schema(user_input)
|
||||
|
||||
if user_input is None:
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=schema,
|
||||
errors={},
|
||||
description_placeholders={},
|
||||
)
|
||||
|
||||
addr = host_port(user_input)
|
||||
|
||||
for entry in hass.config_entries.async_entries(DOMAIN):
|
||||
if addr == host_port(entry.data):
|
||||
host, port = addr
|
||||
return self.async_abort(
|
||||
reason=ADDRESS_ALREADY_CONFIGURED,
|
||||
description_placeholders={"address": f"{host}:{port}"},
|
||||
)
|
||||
|
||||
websession = async_get_clientsession(hass)
|
||||
api_host = ApiHost(*addr, DEFAULT_SETUP_TIMEOUT, websession, hass.loop, _LOGGER)
|
||||
|
||||
try:
|
||||
product = await Products.async_from_host(api_host)
|
||||
|
||||
except UnsupportedBoxVersion as ex:
|
||||
return self.handle_step_exception(
|
||||
"user", ex, schema, *addr, UNSUPPORTED_VERSION, _LOGGER.debug
|
||||
)
|
||||
|
||||
except Error as ex:
|
||||
return self.handle_step_exception(
|
||||
"user", ex, schema, *addr, CANNOT_CONNECT, _LOGGER.warning
|
||||
)
|
||||
|
||||
except RuntimeError as ex:
|
||||
return self.handle_step_exception(
|
||||
"user", ex, schema, *addr, UNKNOWN, _LOGGER.error
|
||||
)
|
||||
|
||||
# Check if configured but IP changed since
|
||||
await self.async_set_unique_id(product.unique_id)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return self.async_create_entry(title=product.name, data=user_input)
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Constants for the BleBox devices integration."""
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
DEVICE_CLASS_DOOR,
|
||||
DEVICE_CLASS_GATE,
|
||||
DEVICE_CLASS_SHUTTER,
|
||||
STATE_CLOSED,
|
||||
STATE_CLOSING,
|
||||
STATE_OPEN,
|
||||
STATE_OPENING,
|
||||
)
|
||||
|
||||
DOMAIN = "blebox"
|
||||
PRODUCT = "product"
|
||||
|
||||
DEFAULT_SETUP_TIMEOUT = 3
|
||||
|
||||
# translation strings
|
||||
ADDRESS_ALREADY_CONFIGURED = "address_already_configured"
|
||||
CANNOT_CONNECT = "cannot_connect"
|
||||
UNSUPPORTED_VERSION = "unsupported_version"
|
||||
UNKNOWN = "unknown"
|
||||
|
||||
BLEBOX_TO_HASS_DEVICE_CLASSES = {
|
||||
"shutter": DEVICE_CLASS_SHUTTER,
|
||||
"gatebox": DEVICE_CLASS_DOOR,
|
||||
"gate": DEVICE_CLASS_GATE,
|
||||
}
|
||||
|
||||
BLEBOX_TO_HASS_COVER_STATES = {
|
||||
None: None,
|
||||
0: STATE_CLOSING, # moving down
|
||||
1: STATE_OPENING, # moving up
|
||||
2: STATE_OPEN, # manually stopped
|
||||
3: STATE_CLOSED, # lower limit
|
||||
4: STATE_OPEN, # upper limit / open
|
||||
# gateController
|
||||
5: STATE_OPEN, # overload
|
||||
6: STATE_OPEN, # motor failure
|
||||
# 7 is not used
|
||||
8: STATE_OPEN, # safety stop
|
||||
}
|
||||
|
||||
DEFAULT_HOST = "192.168.0.2"
|
||||
DEFAULT_PORT = 80
|
||||
@@ -0,0 +1,97 @@
|
||||
"""BleBox cover entity."""
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
ATTR_POSITION,
|
||||
STATE_CLOSED,
|
||||
STATE_CLOSING,
|
||||
STATE_OPENING,
|
||||
SUPPORT_CLOSE,
|
||||
SUPPORT_OPEN,
|
||||
SUPPORT_SET_POSITION,
|
||||
SUPPORT_STOP,
|
||||
CoverEntity,
|
||||
)
|
||||
|
||||
from . import BleBoxEntity, create_blebox_entities
|
||||
from .const import (
|
||||
BLEBOX_TO_HASS_COVER_STATES,
|
||||
BLEBOX_TO_HASS_DEVICE_CLASSES,
|
||||
DOMAIN,
|
||||
PRODUCT,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add):
|
||||
"""Set up a BleBox entry."""
|
||||
|
||||
product = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
||||
create_blebox_entities(product, async_add, BleBoxCoverEntity, "covers")
|
||||
return True
|
||||
|
||||
|
||||
class BleBoxCoverEntity(BleBoxEntity, CoverEntity):
|
||||
"""Representation of a BleBox cover feature."""
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the equivalent HA cover state."""
|
||||
return BLEBOX_TO_HASS_COVER_STATES[self._feature.state]
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class."""
|
||||
return BLEBOX_TO_HASS_DEVICE_CLASSES[self._feature.device_class]
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Return the supported cover features."""
|
||||
position = SUPPORT_SET_POSITION if self._feature.is_slider else 0
|
||||
stop = SUPPORT_STOP if self._feature.has_stop else 0
|
||||
|
||||
return position | stop | SUPPORT_OPEN | SUPPORT_CLOSE
|
||||
|
||||
@property
|
||||
def current_cover_position(self):
|
||||
"""Return the current cover position."""
|
||||
position = self._feature.current
|
||||
if position == -1: # possible for shutterBox
|
||||
return None
|
||||
|
||||
return None if position is None else 100 - position
|
||||
|
||||
@property
|
||||
def is_opening(self):
|
||||
"""Return whether cover is opening."""
|
||||
return self._is_state(STATE_OPENING)
|
||||
|
||||
@property
|
||||
def is_closing(self):
|
||||
"""Return whether cover is closing."""
|
||||
return self._is_state(STATE_CLOSING)
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return whether cover is closed."""
|
||||
return self._is_state(STATE_CLOSED)
|
||||
|
||||
async def async_open_cover(self, **kwargs):
|
||||
"""Open the cover position."""
|
||||
await self._feature.async_open()
|
||||
|
||||
async def async_close_cover(self, **kwargs):
|
||||
"""Close the cover position."""
|
||||
await self._feature.async_close()
|
||||
|
||||
async def async_set_cover_position(self, **kwargs):
|
||||
"""Set the cover position."""
|
||||
|
||||
position = kwargs[ATTR_POSITION]
|
||||
await self._feature.async_set_position(100 - position)
|
||||
|
||||
async def async_stop_cover(self, **kwargs):
|
||||
"""Stop the cover."""
|
||||
await self._feature.async_stop()
|
||||
|
||||
def _is_state(self, state_name):
|
||||
value = self.state
|
||||
return None if value is None else value == state_name
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"domain": "blebox",
|
||||
"name": "BleBox devices",
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/blebox",
|
||||
"requirements": ["blebox_uniapi==1.3.2"],
|
||||
"codeowners": [ "@gadgetmobile" ]
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "This BleBox device is already configured.",
|
||||
"address_already_configured": "A BleBox device is already configured at {address}."
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Unable to connect to the BleBox device. (Check the logs for errors.)",
|
||||
"unsupported_version": "BleBox device has outdated firmware. Please upgrade it first.",
|
||||
"unknown": "Unknown error while connecting to the BleBox device. (Check the logs for errors.)"
|
||||
},
|
||||
"flow_title": "BleBox device: {name} ({host})",
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "Set up your BleBox to integrate with Home Assistant.",
|
||||
"data": {
|
||||
"host": "IP address",
|
||||
"port": "Port"
|
||||
},
|
||||
"title": "Set up your BleBox device"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "This BleBox device is already configured.",
|
||||
"address_already_configured": "A BleBox device is already configured at {address}."
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Unable to connect to the BleBox device. (Check the logs for errors.)",
|
||||
"unsupported_version": "BleBox device has outdated firmware. Please upgrade it first.",
|
||||
"unknown": "Unknown error while connecting to the BleBox device. (Check the logs for errors.)"
|
||||
},
|
||||
"flow_title": "BleBox device: {name} ({host})",
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "Set up your BleBox to integrate with Home Assistant.",
|
||||
"data": {
|
||||
"host": "IP address",
|
||||
"port": "Port"
|
||||
},
|
||||
"title": "Set up your BleBox device"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user