Skip to content

Commit

Permalink
Add test_sensor.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Feb 9, 2023
1 parent 3594f32 commit 414d416
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
14 changes: 4 additions & 10 deletions custom_components/beward/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Sensor platform for Beward devices."""
# Copyright (c) 2019-2022, Andrey "Limych" Khrolenok <andrey@khrolenok.ru>
# Copyright (c) 2019-2023, Andrey "Limych" Khrolenok <andrey@khrolenok.ru>
# Creative Commons BY-NC-SA 4.0 International Public License
# (see LICENSE.md or https://creativecommons.org/licenses/by-nc-sa/4.0/)
from __future__ import annotations
Expand Down Expand Up @@ -47,16 +47,14 @@ async def async_setup_entry(
if entry.source == SOURCE_IMPORT:
config = hass.data[DOMAIN_YAML]
for index, device_config in enumerate(config):
controller = hass.data[DOMAIN][entry.entry_id][
index
] # type: BewardController
controller: BewardController = hass.data[DOMAIN][entry.entry_id][index]
entities.extend(_setup_entities(controller, device_config))

else:
config = entry.data.copy()
config.update(entry.options)
_LOGGER.debug(hass.data[DOMAIN])
controller = hass.data[DOMAIN][entry.entry_id][0] # type: BewardController
controller: BewardController = hass.data[DOMAIN][entry.entry_id][0]
entities.extend(_setup_entities(controller, config))

if entities:
Expand Down Expand Up @@ -128,11 +126,7 @@ def _update_callback(self, update_ha_state=True) -> None:
if ding_ts is not None and event_ts is not None and ding_ts > event_ts:
event_ts = ding_ts

state = (
dt_util.as_local(event_ts.replace(microsecond=0)).isoformat()
if event_ts
else None
)
state = dt_util.as_local(event_ts.replace(microsecond=0)) if event_ts else None
if self._attr_native_value != state:
self._attr_native_value = state
_LOGGER.debug(
Expand Down
3 changes: 3 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
}
MOCK_YAML_CONFIG = MOCK_CONFIG.copy()
MOCK_YAML_CONFIG.update(MOCK_OPTIONS)

MOCK_DEVICE_ID: Final = "test_device_37354"
MOCK_DEVICE_NAME: Final = "Test Device 37354"
92 changes: 92 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# pylint: disable=protected-access,redefined-outer-name
"""Test beward sensors."""
# Copyright (c) 2019-2023, Andrey "Limych" Khrolenok <andrey@khrolenok.ru>
# Creative Commons BY-NC-SA 4.0 International Public License
# (see LICENSE.md or https://creativecommons.org/licenses/by-nc-sa/4.0/)
from __future__ import annotations

from unittest.mock import Mock, patch

from beward import BewardDoorbell
import pytest

from custom_components.beward import BewardController
from custom_components.beward.const import (
ICON_SENSOR,
SENSOR_LAST_ACTIVITY,
SENSOR_LAST_DING,
SENSOR_LAST_MOTION,
)
from custom_components.beward.sensor import BewardSensor
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util

from .const import MOCK_DEVICE_ID, MOCK_DEVICE_NAME


@pytest.fixture
def controller(hass: HomeAssistant):
"""Generate test controller."""
device = Mock(BewardDoorbell)
return BewardController(hass, MOCK_DEVICE_ID, device, MOCK_DEVICE_NAME)


async def test_last_activity_sensor(hass: HomeAssistant, controller: BewardController):
"""Test last activity sensor."""
mock_ts = dt_util.utcnow()
mock_ts_local = mock_ts.replace(microsecond=0)

sensor = BewardSensor(controller, SENSOR_LAST_ACTIVITY)

assert sensor
assert sensor.unique_id == f"{MOCK_DEVICE_ID}-{SENSOR_LAST_ACTIVITY}"
assert sensor.name == f"{MOCK_DEVICE_NAME} Last Activity"
assert sensor.icon == ICON_SENSOR
assert sensor.device_class == DEVICE_CLASS_TIMESTAMP
assert sensor.entity_id == f"sensor.{MOCK_DEVICE_ID}_{SENSOR_LAST_ACTIVITY}"
assert sensor.state is None

with patch.object(sensor, "_get_event_timestamp", return_value=mock_ts):
sensor._update_callback(True)
assert sensor.state == mock_ts_local.isoformat()


async def test_last_motion_sensor(hass: HomeAssistant, controller: BewardController):
"""Test last motion sensor."""
mock_ts = dt_util.utcnow()
mock_ts_local = mock_ts.replace(microsecond=0)

sensor = BewardSensor(controller, SENSOR_LAST_MOTION)

assert sensor
assert sensor.unique_id == f"{MOCK_DEVICE_ID}-{SENSOR_LAST_MOTION}"
assert sensor.name == f"{MOCK_DEVICE_NAME} Last Motion"
assert sensor.icon == ICON_SENSOR
assert sensor.device_class == DEVICE_CLASS_TIMESTAMP
assert sensor.entity_id == f"sensor.{MOCK_DEVICE_ID}_{SENSOR_LAST_MOTION}"
assert sensor.state is None

with patch.object(sensor, "_get_event_timestamp", return_value=mock_ts):
sensor._update_callback(True)
assert sensor.state == mock_ts_local.isoformat()


async def test_last_ding_sensor(hass: HomeAssistant, controller: BewardController):
"""Test last ding sensor."""
mock_ts = dt_util.utcnow()
mock_ts_local = mock_ts.replace(microsecond=0)

sensor = BewardSensor(controller, SENSOR_LAST_DING)

assert sensor
assert sensor.unique_id == f"{MOCK_DEVICE_ID}-{SENSOR_LAST_DING}"
assert sensor.name == f"{MOCK_DEVICE_NAME} Last Ding"
assert sensor.icon == ICON_SENSOR
assert sensor.device_class == DEVICE_CLASS_TIMESTAMP
assert sensor.entity_id == f"sensor.{MOCK_DEVICE_ID}_{SENSOR_LAST_DING}"
assert sensor.state is None

with patch.object(sensor, "_get_event_timestamp", return_value=mock_ts):
sensor._update_callback(True)
assert sensor.state == mock_ts_local.isoformat()

0 comments on commit 414d416

Please sign in to comment.