Skip to content

Commit

Permalink
Version 0.2.0
Browse files Browse the repository at this point in the history
* Update general deps
* Update python-hnap to 1.0.0alpha 3
* New service for invoking hnap methods
  • Loading branch information
ldotlopez committed Apr 26, 2022
1 parent e65873e commit 864d062
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 340 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name = "pypi"

[dev-packages]
homeassistant = "*"
types-requests = "*"

[requires]
python_version = "3"
729 changes: 427 additions & 302 deletions Pipfile.lock

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions custom_components/hnap_device/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

"""Binary sensor for HNAP device integration."""

from datetime import timedelta
from typing import Optional

import requests.exceptions
Expand All @@ -38,8 +37,6 @@

PLATFORM = PLATFORM_BINARY_SENSOR

SCAN_INTERVAL = timedelta(seconds=5)


class HNAPMotion(HNapEntity, BinarySensorEntity):
def __init__(self, *args, **kwargs):
Expand All @@ -49,7 +46,7 @@ def __init__(self, *args, **kwargs):

def update(self):
try:
self._attr_is_on = self._api.is_active()
self._attr_is_on = self.device.is_active()

except requests.exceptions.ConnectionError as e:
_LOGGER.error(e)
Expand All @@ -66,15 +63,15 @@ async def async_setup_entry(
add_entities: AddEntitiesCallback,
discovery_info: Optional[DiscoveryInfoType] = None, # noqa DiscoveryInfoType | None
):
api = hass.data[DOMAIN][PLATFORM][config_entry.entry_id]
device_info = await hass.async_add_executor_job(api.get_info)
device = hass.data[DOMAIN][PLATFORM][config_entry.entry_id]
device_info = await hass.async_add_executor_job(device.client.device_info)

add_entities(
[
HNAPMotion(
unique_id=config_entry.entry_id,
device_info=device_info,
api=api,
device=device,
)
],
update_before_add=True,
Expand Down
15 changes: 8 additions & 7 deletions custom_components/hnap_device/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@


class HNAPCamera(HNapEntity, Camera):
def __init__(self, info, api, unique_id):
super().__init__(info, api, unique_id)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self._attr_supported_features = SUPPORT_STREAM

async def stream_source(self) -> Optional[str]:
return self._api.stream_url
return self.device.stream_url


async def async_setup_entry(
Expand All @@ -50,17 +50,18 @@ async def async_setup_entry(
add_entities: AddEntitiesCallback,
discovery_info: Optional[DiscoveryInfoType] = None, # noqa DiscoveryInfoType | None
):
api = hass.data[DOMAIN][PLATFORM][config_entry.entry_id]

_LOGGER.error("camera support is not implemented yet")
return

device = hass.data[DOMAIN][PLATFORM][config_entry.entry_id]
device_info = await hass.async_add_executor_job(device.client.device_info)

add_entities(
[
HNAPCamera(
info=config_entry.data,
api=api,
unique_id=config_entry.entry_id,
device_info=device_info,
device=device,
)
],
update_before_add=True,
Expand Down
19 changes: 11 additions & 8 deletions custom_components/hnap_device/hnap_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


class HNapEntity:
def __init__(self, unique_id, device_info, api, *args, **kwargs):
def __init__(self, *args, unique_id, device_info, device, **kwargs):
super().__init__(*args, **kwargs)

self._attr_unique_id = unique_id
Expand All @@ -39,20 +39,23 @@ def __init__(self, unique_id, device_info, api, *args, **kwargs):
}
self._attr_name = self._attr_device_info["name"]

self._api = api
self.device = device

self._consecutive_failures = 0
self._boot_ts = time.monotonic()

def hnap_update_success(self):
self._consecutive_failures = 0

uptime = time.monotonic() - self._boot_ts
_LOGGER.debug(f"Device uptime: {uptime:.2f}/{MAX_UPTIME_BEFORE_REBOOT}")
# Automatic reboot is still experimental
#
# uptime = time.monotonic() - self._boot_ts
# _LOGGER.debug(f"Device uptime: {uptime:.2f}/{MAX_UPTIME_BEFORE_REBOOT}")

if self.available and uptime > MAX_UPTIME_BEFORE_REBOOT:
_LOGGER.debug("Device must be rebooted")
# self._api.client.call("Reboot")
self._boot_ts = time.monotonic()
# if self.available and uptime > MAX_UPTIME_BEFORE_REBOOT:
# _LOGGER.debug("Device must be rebooted")
# self.device.client.call("Reboot")
# self._boot_ts = time.monotonic()

def hnap_update_failure(self):
self._consecutive_failures = self._consecutive_failures + 1
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hnap_device/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"documentation": "https://github.com/ldotlopez/ha-hnap-device",
"issue_tracker": "https://github.com/ldotlopez/ha-hnap-device/issues",
"requirements": [
"hnap>=1.0.0a2,<2.0.0"
"hnap>=1.0.0a3,<2.0.0"
],
"ssdp": [],
"zeroconf": [],
Expand All @@ -15,5 +15,5 @@
"@ldotlopez"
],
"iot_class": "local_polling",
"version": "0.1.1"
"version": "0.2.0"
}
8 changes: 0 additions & 8 deletions custom_components/hnap_device/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ call:
selector:
text:

# selector:
# select:
# options:
# - "off"
# - "low"
# - "medium"
# - "high"

parameters:
name: Parameters
description:
Expand Down
12 changes: 6 additions & 6 deletions custom_components/hnap_device/siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, *args, **kwargs):

def update(self):
try:
self._attr_is_on = self._api.is_playing()
self._attr_is_on = self.device.is_playing()

except requests.exceptions.ConnectionError as e:
_LOGGER.error(e)
Expand All @@ -72,14 +72,14 @@ def update(self):
self.hnap_update_success()

def turn_on(self, volume_level=1, duration=15, tone="police") -> None:
self._api.play(
self.device.play(
sound=hnap.SirenSound.fromstring(tone),
volume=int(volume_level * 100),
duration=duration,
)

def turn_off(self) -> None:
self._api.stop()
self.device.stop()


async def async_setup_entry(
Expand All @@ -88,15 +88,15 @@ async def async_setup_entry(
add_entities: AddEntitiesCallback,
discovery_info: Optional[DiscoveryInfoType] = None, # noqa DiscoveryInfoType | None
):
api = hass.data[DOMAIN][PLATFORM][config_entry.entry_id]
device_info = await hass.async_add_executor_job(api.client.device_info)
device = hass.data[DOMAIN][PLATFORM][config_entry.entry_id]
device_info = await hass.async_add_executor_job(device.client.device_info)

add_entities(
[
HNAPSiren(
unique_id=config_entry.entry_id,
device_info=device_info,
api=api,
device=device,
)
],
update_before_add=True,
Expand Down

0 comments on commit 864d062

Please sign in to comment.