Skip to content

Commit

Permalink
Add button entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 committed May 27, 2023
1 parent 3d61a22 commit 141ac65
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions custom_components/ezviz_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ATTR_TYPE_CLOUD: [
Platform.ALARM_CONTROL_PANEL,
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.CAMERA,
Platform.NUMBER,
Platform.SELECT,
Expand Down
121 changes: 121 additions & 0 deletions custom_components/ezviz_cloud/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Support for EZVIZ button controls."""
from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass
from typing import Any

from pyezviz.exceptions import HTTPError, PyEzvizError

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DATA_COORDINATOR, DOMAIN
from .coordinator import EzvizDataUpdateCoordinator
from .entity import EzvizEntity

PARALLEL_UPDATES = 1


@dataclass
class EzvizButtonEntityDescriptionMixin:
"""Mixin values for EZVIZ button entities."""

method: Callable[[Any, Any, Any], Any]


@dataclass
class EzvizButtonEntityDescription(
ButtonEntityDescription, EzvizButtonEntityDescriptionMixin
):
"""Describe a EZVIZ Button."""


BUTTON_ENTITIES = (
EzvizButtonEntityDescription(
key="ptz_up",
entity_registry_enabled_default=False,
name="PTZ up",
icon="mdi:pan",
method=lambda pyezviz_client, serial, run: pyezviz_client.ptz_control(
"UP", serial, run
),
),
EzvizButtonEntityDescription(
key="ptz_down",
entity_registry_enabled_default=False,
name="PTZ down",
icon="mdi:pan",
method=lambda pyezviz_client, serial, run: pyezviz_client.ptz_control(
"DOWN", serial, run
),
),
EzvizButtonEntityDescription(
key="ptz_left",
entity_registry_enabled_default=False,
name="PTZ left",
icon="mdi:pan",
method=lambda pyezviz_client, serial, run: pyezviz_client.ptz_control(
"LEFT", serial, run
),
),
EzvizButtonEntityDescription(
key="ptz_right",
entity_registry_enabled_default=False,
name="PTZ right",
icon="mdi:pan",
method=lambda pyezviz_client, serial, run: pyezviz_client.ptz_control(
"RIGHT", serial, run
),
),
)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up EZVIZ button based on a config entry."""
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]

async_add_entities(
EzvizButton(coordinator, camera, entity_description)
for camera in coordinator.data
for entity_description in BUTTON_ENTITIES
)


class EzvizButton(EzvizEntity, ButtonEntity):
"""Representation of a EZVIZ button entity."""

entity_description: EzvizButtonEntityDescription
_attr_has_entity_name = True

def __init__(
self,
coordinator: EzvizDataUpdateCoordinator,
serial: str,
description: EzvizButtonEntityDescription,
) -> None:
"""Initialize the button."""
super().__init__(coordinator, serial)
self._attr_unique_id = f"{serial}_{description.name}"
self.entity_description = description

def press(self) -> None:
"""Execute the button action."""
try:
self.entity_description.method(
self.coordinator.ezviz_client, self._serial, "START"
)
self.entity_description.method(
self.coordinator.ezviz_client, self._serial, "STOP"
)
except (HTTPError, PyEzvizError) as err:
raise HomeAssistantError(
f"Cannot perform PTZ action on {self.name}"
) from err
10 changes: 10 additions & 0 deletions custom_components/ezviz_cloud/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ def perform_ptz(self, direction: str, speed: int) -> None:
except HTTPError as err:
raise HTTPError("Cannot perform PTZ") from err

ir.async_create_issue(
self.hass,
DOMAIN,
"service_depreciation_ptz",
breaks_in_ha_version="2023.8.0",
is_fixable=False,
severity=ir.IssueSeverity.WARNING,
translation_key="service_depreciation_ptz",
)

def perform_sound_alarm(self, enable: int) -> None:
"""Sound the alarm on a camera."""
try:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ezviz_cloud/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"loggers": ["paho_mqtt", "pyezviz"],
"requirements": ["pyezviz==0.2.0.12"],
"version": "0.1.0.10"
"version": "0.1.0.11"
}
4 changes: 4 additions & 0 deletions custom_components/ezviz_cloud/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"service_depreciation_alarm_sound_level": {
"title": "Ezviz Alarm sound level service is being removed",
"description": "Ezviz Alarm sound level service is deprecated and will be removed in Home Assistant 2023.8; Please adjust the automation or script that uses the service and select submit below to mark this issue as resolved."
},
"service_depreciation_ptz": {
"title": "Ezviz ptz service is being removed",
"description": "Ezviz ptz service is deprecated and will be removed in Home Assistant 2023.8; Please adjust the automation or script that uses the service and select submit below to mark this issue as resolved."
}
}
}
4 changes: 4 additions & 0 deletions custom_components/ezviz_cloud/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"service_depreciation_alarm_sound_level": {
"title": "Ezviz Alarm sound level service is being removed",
"description": "Ezviz Alarm sound level service is deprecated and will be removed in Home Assistant 2023.8; Please adjust the automation or script that uses the service and select submit below to mark this issue as resolved."
},
"service_depreciation_ptz": {
"title": "Ezviz ptz service is being removed",
"description": "Ezviz ptz service is deprecated and will be removed in Home Assistant 2023.8; Please adjust the automation or script that uses the service and select submit below to mark this issue as resolved."
}
}
}

0 comments on commit 141ac65

Please sign in to comment.