Skip to content

Commit

Permalink
FEAT: Add all object sensor to camera and zone devices (#224)
Browse files Browse the repository at this point in the history
* Set all sensors to be disabled by default

* Set the camera and zone to use all obj as well

* Fix precommit

* Fix spacing

* Add test for disabled all entity for binary_sensor

* Add test for disabled all entity for object count sensor

* Fix pre-commit

* Fix sorting

* Fix naming error
  • Loading branch information
NickM-27 authored Mar 11, 2022
1 parent 61adccb commit 400ab71
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def get_cameras_and_objects(config: dict[str, Any]) -> set[tuple[str, str]]:
for cam_name, cam_config in config["cameras"].items():
for obj in cam_config["objects"]["track"]:
camera_objects.add((cam_name, obj))

# add an artificial all label to track
# all objects for this camera
camera_objects.add((cam_name, "all"))
return camera_objects


Expand All @@ -139,6 +143,10 @@ def get_cameras_zones_and_objects(config: dict[str, Any]) -> set[tuple[str, str]
)
if not zone_name_objects or obj in zone_name_objects:
zone_objects.add((zone_name, obj))

# add an artificial all label to track
# all objects for this zone
zone_objects.add((zone_name, "all"))
return camera_objects.union(zone_objects)


Expand Down
5 changes: 5 additions & 0 deletions custom_components/frigate/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return self._is_on

@property
def entity_registry_enabled_default(self) -> bool:
"""Whether or not the entity is enabled by default."""
return self._obj_name != "all"

@property
def device_class(self) -> str:
"""Return the device class."""
Expand Down
5 changes: 5 additions & 0 deletions custom_components/frigate/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,8 @@ def unit_of_measurement(self) -> str:
def icon(self) -> str:
"""Return the icon of the sensor."""
return self._icon

@property
def entity_registry_enabled_default(self) -> bool:
"""Whether or not the entity is enabled by default."""
return self._obj_name != "all"
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
"binary_sensor.front_door_person_motion"
)
TEST_BINARY_SENSOR_STEPS_PERSON_MOTION_ENTITY_ID = "binary_sensor.steps_person_motion"
TEST_BINARY_SENSOR_STEPS_ALL_MOTION_ENTITY_ID = "binary_sensor.steps_all_motion"
TEST_CAMERA_FRONT_DOOR_ENTITY_ID = "camera.front_door"
TEST_CAMERA_FRONT_DOOR_PERSON_ENTITY_ID = "camera.front_door_person"
TEST_SWITCH_FRONT_DOOR_DETECT_ENTITY_ID = "switch.front_door_detect"
TEST_SWITCH_FRONT_DOOR_SNAPSHOTS_ENTITY_ID = "switch.front_door_snapshots"
TEST_SWITCH_FRONT_DOOR_RECORDINGS_ENTITY_ID = "switch.front_door_recordings"
TEST_SENSOR_STEPS_PERSON_ENTITY_ID = "sensor.steps_person"
TEST_SENSOR_STEPS_ALL_ENTITY_ID = "sensor.steps_all"
TEST_SENSOR_FRONT_DOOR_PERSON_ENTITY_ID = "sensor.front_door_person"
TEST_SENSOR_DETECTION_FPS_ENTITY_ID = "sensor.detection_fps"
TEST_SENSOR_CPU1_INTFERENCE_SPEED_ENTITY_ID = "sensor.cpu1_inference_speed"
Expand Down
21 changes: 21 additions & 0 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from . import (
TEST_BINARY_SENSOR_FRONT_DOOR_PERSON_MOTION_ENTITY_ID,
TEST_BINARY_SENSOR_STEPS_ALL_MOTION_ENTITY_ID,
TEST_BINARY_SENSOR_STEPS_PERSON_MOTION_ENTITY_ID,
TEST_CONFIG_ENTRY_ID,
TEST_SERVER_VERSION,
Expand Down Expand Up @@ -136,3 +137,23 @@ async def test_binary_sensor_unload_will_unsubscribe(hass: HomeAssistant) -> Non
await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
mock_unsubscribe.assert_called()


async def test_binary_sensor_all_can_be_enabled(hass: HomeAssistant) -> None:
"""Verify `all` binary_sensor can be enabled."""
await setup_mock_frigate_config_entry(hass)
entity_registry = er.async_get(hass)

# Test original entity is disabled as expected
entry = entity_registry.async_get(TEST_BINARY_SENSOR_STEPS_ALL_MOTION_ENTITY_ID)
assert entry
assert entry.disabled
assert entry.disabled_by == er.DISABLED_INTEGRATION
entity_state = hass.states.get(TEST_BINARY_SENSOR_STEPS_ALL_MOTION_ENTITY_ID)
assert not entity_state

# Update and test that entity is now enabled
updated_entry = entity_registry.async_update_entity(
TEST_BINARY_SENSOR_STEPS_ALL_MOTION_ENTITY_ID, disabled_by=None
)
assert not updated_entry.disabled
21 changes: 21 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
TEST_SENSOR_FRONT_DOOR_PERSON_ENTITY_ID,
TEST_SENSOR_FRONT_DOOR_PROCESS_FPS_ENTITY_ID,
TEST_SENSOR_FRONT_DOOR_SKIPPED_FPS_ENTITY_ID,
TEST_SENSOR_STEPS_ALL_ENTITY_ID,
TEST_SENSOR_STEPS_PERSON_ENTITY_ID,
TEST_SERVER_VERSION,
TEST_STATS,
Expand Down Expand Up @@ -344,3 +345,23 @@ async def test_camera_unique_id(
registry_entry = er.async_get(hass).async_get(entity_id)
assert registry_entry
assert registry_entry.unique_id == unique_id


async def test_sensor_all_can_be_enabled(hass: HomeAssistant) -> None:
"""Verify `all` object_count sensor can be enabled."""
await setup_mock_frigate_config_entry(hass)
entity_registry = er.async_get(hass)

# Test original entity is disabled as expected
entry = entity_registry.async_get(TEST_SENSOR_STEPS_ALL_ENTITY_ID)
assert entry
assert entry.disabled
assert entry.disabled_by == er.DISABLED_INTEGRATION
entity_state = hass.states.get(TEST_SENSOR_STEPS_ALL_ENTITY_ID)
assert not entity_state

# Update and test that entity is now enabled
updated_entry = entity_registry.async_update_entity(
TEST_SENSOR_STEPS_ALL_ENTITY_ID, disabled_by=None
)
assert not updated_entry.disabled

0 comments on commit 400ab71

Please sign in to comment.