Skip to content

Commit

Permalink
Change integration configuration timeout #20 #11
Browse files Browse the repository at this point in the history
- Change integration configuration, create param timeout in seconds to disconnect led strip, if zero never disconnect. #20
- Change turn on and turn off commands to MELK strip #11
  • Loading branch information
dave-code-ruiz committed Apr 26, 2023
1 parent 89bfdcc commit 73cc6b3
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 22 deletions.
6 changes: 4 additions & 2 deletions custom_components/elkbledom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from homeassistant.core import HomeAssistant, Event
from homeassistant.const import CONF_MAC, EVENT_HOMEASSISTANT_STOP

from .const import DOMAIN, CONF_RESET
from .const import DOMAIN, CONF_RESET, CONF_DELAY
from .elkbledom import BLEDOMInstance
import logging

Expand All @@ -15,8 +15,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up ElkBLEDOM from a config entry."""
if entry.options.get(CONF_RESET):
LOGGER.debug("Config Reset data: %s", entry.options.get(CONF_RESET))
if entry.options.get(CONF_DELAY):
LOGGER.debug("Config delay data: %s", entry.options.get(CONF_DELAY))

instance = BLEDOMInstance(entry.data[CONF_MAC], entry.options.get(CONF_RESET), hass)
instance = BLEDOMInstance(entry.data[CONF_MAC], entry.options.get(CONF_RESET), entry.options.get(CONF_DELAY), hass)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = instance

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down
7 changes: 4 additions & 3 deletions custom_components/elkbledom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from bluetooth_sensor_state_data import BluetoothData
from home_assistant_bluetooth import BluetoothServiceInfo

from .const import DOMAIN, CONF_RESET
from .const import DOMAIN, CONF_RESET, CONF_DELAY
import logging

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -204,15 +204,16 @@ async def async_step_init(self, _user_input=None):
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
errors = {}
options = self.config_entry.options or {CONF_RESET: False,}
options = self.config_entry.options or {CONF_RESET: False,CONF_DELAY: 120,}
if user_input is not None:
return self.async_create_entry(title="", data={CONF_RESET: user_input[CONF_RESET]})
return self.async_create_entry(title="", data={CONF_RESET: user_input[CONF_RESET], CONF_DELAY: user_input[CONF_DELAY]})

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Optional(CONF_RESET, default=options.get(CONF_RESET)): bool,
vol.Optional(CONF_DELAY, default=options.get(CONF_DELAY)): int,
}
), errors=errors
)
1 change: 1 addition & 0 deletions custom_components/elkbledom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

DOMAIN = "elkbledom"
CONF_RESET = "reset"
CONF_DELAY = "delay"

class EFFECTS (Enum):
jump_red_green_blue = 0x87
Expand Down
19 changes: 11 additions & 8 deletions custom_components/elkbledom/elkbledom.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
NAME_ARRAY = ["ELK-BLE", "LEDBLE", "MELK", "ELK-BULB"]
WRITE_CHARACTERISTIC_UUIDS = ["0000fff3-0000-1000-8000-00805f9b34fb", "0000ffe1-0000-1000-8000-00805f9b34fb", "0000fff3-0000-1000-8000-00805f9b34fb", "0000fff3-0000-1000-8000-00805f9b34fb"]
READ_CHARACTERISTIC_UUIDS = ["0000fff4-0000-1000-8000-00805f9b34fb", "0000ffe2-0000-1000-8000-00805f9b34fb", "0000fff4-0000-1000-8000-00805f9b34fb", "0000fff4-0000-1000-8000-00805f9b34fb"]
TURN_ON_CMD = [[0x7e, 0x00, 0x04, 0xf0, 0x00, 0x01, 0xff, 0x00, 0xef],[0x7e, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0xef], bytearray([0x7e, 0x07, 0x83]),[0x7e, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0xef]]
TURN_OFF_CMD = [[0x7e, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef], [0x7e, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef], bytearray([0x7e, 0x04, 0x04]), [0x7e, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef]]
TURN_ON_CMD = [[0x7e, 0x00, 0x04, 0xf0, 0x00, 0x01, 0xff, 0x00, 0xef],[0x7e, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0xef], [0x7e, 0x07, 0x83],[0x7e, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0xef]]
TURN_OFF_CMD = [[0x7e, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef], [0x7e, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef], [0x7e, 0x04, 0x04], [0x7e, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef]]

DEFAULT_ATTEMPTS = 3
DISCONNECT_DELAY = 120
#DISCONNECT_DELAY = 120
BLEAK_BACKOFF_TIME = 0.25
RETRY_BACKOFF_EXCEPTIONS = (BleakDBusError,)
WrapFuncType = TypeVar("WrapFuncType", bound=Callable[..., Any])
Expand Down Expand Up @@ -108,10 +108,11 @@ async def _async_wrap_retry_bluetooth_connection_error(
return cast(WrapFuncType, _async_wrap_retry_bluetooth_connection_error)

class BLEDOMInstance:
def __init__(self, address, reset: bool, hass) -> None:
def __init__(self, address, reset: bool, delay: int, hass) -> None:
self.loop = asyncio.get_running_loop()
self._mac = address
self._reset = reset
self._delay = delay
self._hass = hass
self._device: BLEDevice | None = None
self._device = bluetooth.async_ble_device_from_address(self._hass, address)
Expand Down Expand Up @@ -346,9 +347,11 @@ def _reset_disconnect_timer(self) -> None:
if self._disconnect_timer:
self._disconnect_timer.cancel()
self._expected_disconnect = False
self._disconnect_timer = self.loop.call_later(
DISCONNECT_DELAY, self._disconnect
)
if self._delay is not None and self._delay != 0:
LOGGER.debug("%s: Disconnected from device in %s seconds; RSSI: %s", self.name, self._delay, self.rssi)
self._disconnect_timer = self.loop.call_later(
self._delay, self._disconnect
)

def _disconnected(self, client: BleakClientWithServiceCache) -> None:
"""Disconnected callback."""
Expand All @@ -372,7 +375,7 @@ async def _execute_timed_disconnect(self) -> None:
LOGGER.debug(
"%s: Disconnecting after timeout of %s",
self.name,
DISCONNECT_DELAY,
self._delay,
)
await self._execute_disconnect()

Expand Down
3 changes: 2 additions & 1 deletion custom_components/elkbledom/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"step": {
"user": {
"data": {
"reset": "Reset color when led turn on"
"reset": "Reset color when led turn on",
"delay": "Disconnect delay (0 equal never disconnect)"
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions custom_components/elkbledom/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"mac": "Direccion MAC Bluetooth",
"name": "Nombre"
},
"title": "Elige una tira led. Asegurate de que la luz esté lista y cerca para la validacion."
"title": "Elige una tira led. Asegurate de que la luz esté lista y cerca para la validacion."
},
"validate": {
"data": {
"retry": "¿Intentar de nuevo la conexión?",
"flicker": "¿La luz vibra?"
"retry": "?Intentar de nuevo la conexión?",
"flicker": "?La luz vibra?"
},
"title": "Validacion de la conexion"
},
Expand All @@ -27,18 +27,19 @@
"connect": "No se puede conectar con la tira led"
},
"abort": {
"cannot_validate": "No se puede validar la conexión",
"cannot_validate": "No se puede validar la conexión",
"cannot_connect": "No se puede conectar con la tira led"
}
},
"options": {
"step": {
"user": {
"data": {
"reset": "Resetear color cuando se encienda el led"
"reset": "Resetear color cuando se encienda el led",
"delay": "Tiempo de espera para desconexión (0 para no desconectar nunca)"
}
}
}
},
"title": "Elkbledom"
}
}
3 changes: 2 additions & 1 deletion custom_components/elkbledom/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"step": {
"user": {
"data": {
"reset": "Réinitialiser la couleur ? l'allumage"
"reset": "Réinitialiser la couleur ? l'allumage",
"delay": "Temps d'attente pour la déconnexion (0 pour ne jamais se déconnecter)"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/elkbledom/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"step": {
"user": {
"data": {
"reset": "Zresetuj kolor gdy LED włączy się"
"reset": "Zresetuj kolor gdy LED włączy się",
"delay": "Czas oczekiwania na rozłączenie (0, aby nigdy nie rozłączać)"
}
}
}
Expand Down

0 comments on commit 73cc6b3

Please sign in to comment.