Skip to content

Commit

Permalink
Fix: Do not create movement buttons if camera does not support movement
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajNyiri committed Oct 18, 2022
1 parent e3c4921 commit 2db8a13
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions custom_components/tapo_control/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .const import DOMAIN, LOGGER
from .tapo.entities import TapoButtonEntity
from .utils import syncTime
from .utils import syncTime, check_and_create


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand All @@ -22,22 +22,29 @@ async def async_setup_entry(
entry = hass.data[DOMAIN][config_entry.entry_id]

buttons = []
buttons.append(TapoRebootButton(entry, hass))
buttons.append(TapoFormatButton(entry, hass))
buttons.append(TapoStartManualAlarmButton(entry, hass))
buttons.append(TapoStopManualAlarmButton(entry, hass))
buttons.append(TapoSyncTimeButton(entry, hass, config_entry.entry_id))
buttons.append(TapoCalibrateButton(entry, hass))
buttons.append(TapoMoveUpButton(entry, hass))
buttons.append(TapoMoveDownButton(entry, hass))
buttons.append(TapoMoveRightButton(entry, hass))
buttons.append(TapoMoveLeftButton(entry, hass))
buttons.append(TapoRebootButton(entry, hass, config_entry))
buttons.append(TapoFormatButton(entry, hass, config_entry))
buttons.append(TapoStartManualAlarmButton(entry, hass, config_entry))
buttons.append(TapoStopManualAlarmButton(entry, hass, config_entry))
buttons.append(TapoSyncTimeButton(entry, hass, config_entry))

tapoCalibrateButton = await check_and_create(
entry, hass, TapoCalibrateButton, "getPresets", config_entry
)
if tapoCalibrateButton:
buttons.append(tapoCalibrateButton)
buttons.append(TapoMoveUpButton(entry, hass, config_entry))
buttons.append(TapoMoveDownButton(entry, hass, config_entry))
buttons.append(TapoMoveRightButton(entry, hass, config_entry))
buttons.append(TapoMoveLeftButton(entry, hass, config_entry))
else:
LOGGER.info("Buttons: Camera does not support movement.")

async_add_entities(buttons)


class TapoRebootButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Reboot", entry, hass)

async def async_press(self) -> None:
Expand All @@ -49,60 +56,52 @@ def device_class(self) -> str:


class TapoFormatButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Format SD Card", entry, hass, "mdi:eraser")

async def async_press(self) -> None:
await self._hass.async_add_executor_job(self._controller.format)


class TapoSyncTimeButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant, entry_id):
self._entry_id = entry_id
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
self._entry_id = config_entry.entry_id
TapoButtonEntity.__init__(
self,
"Sync Time",
entry,
hass,
"mdi:timer-sync-outline",
self, "Sync Time", entry, hass, "mdi:timer-sync-outline",
)

async def async_press(self) -> None:
await syncTime(self._hass, self._entry_id)


class TapoStartManualAlarmButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Manual Alarm Start", entry, hass, "mdi:alarm")

async def async_press(self) -> None:
await self._hass.async_add_executor_job(self._controller.startManualAlarm)


class TapoStopManualAlarmButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(
self,
"Manual Alarm Stop",
entry,
hass,
"mdi:alarm-off",
self, "Manual Alarm Stop", entry, hass, "mdi:alarm-off",
)

async def async_press(self) -> None:
await self._hass.async_add_executor_job(self._controller.stopManualAlarm)


class TapoCalibrateButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Calibrate", entry, hass)

async def async_press(self) -> None:
await self._hass.async_add_executor_job(self._controller.calibrateMotor)


class TapoMoveUpButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Move Up", entry, hass, "mdi:arrow-up")

async def async_press(self) -> None:
Expand All @@ -112,7 +111,7 @@ async def async_press(self) -> None:


class TapoMoveDownButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Move Down", entry, hass, "mdi:arrow-down")

async def async_press(self) -> None:
Expand All @@ -122,7 +121,7 @@ async def async_press(self) -> None:


class TapoMoveRightButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Move Right", entry, hass, "mdi:arrow-right")

async def async_press(self) -> None:
Expand All @@ -132,7 +131,7 @@ async def async_press(self) -> None:


class TapoMoveLeftButton(TapoButtonEntity):
def __init__(self, entry: dict, hass: HomeAssistant):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
TapoButtonEntity.__init__(self, "Move Left", entry, hass, "mdi:arrow-left")

async def async_press(self) -> None:
Expand Down

0 comments on commit 2db8a13

Please sign in to comment.