core/homeassistant/components/blink/sensor.py

112 lines
3.4 KiB
Python
Raw Normal View History

"""Support for Blink system camera sensors."""
2021-08-11 20:41:51 +00:00
from __future__ import annotations
import logging
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import EntityCategory, UnitOfTemperature
2023-10-23 13:34:28 +00:00
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2023-10-23 13:34:28 +00:00
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DEFAULT_BRAND, DOMAIN, TYPE_TEMPERATURE, TYPE_WIFI_STRENGTH
2024-10-24 12:32:48 +00:00
from .coordinator import BlinkConfigEntry, BlinkUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
2021-08-11 20:41:51 +00:00
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key=TYPE_TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
2021-08-11 20:41:51 +00:00
),
SensorEntityDescription(
key=TYPE_WIFI_STRENGTH,
translation_key="wifi_strength",
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
2021-08-11 20:41:51 +00:00
),
)
async def async_setup_entry(
2024-10-24 12:32:48 +00:00
hass: HomeAssistant,
config_entry: BlinkConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Initialize a Blink sensor."""
2024-10-24 12:32:48 +00:00
coordinator = config_entry.runtime_data
2021-08-11 20:41:51 +00:00
entities = [
2023-10-23 13:34:28 +00:00
BlinkSensor(coordinator, camera, description)
for camera in coordinator.api.cameras
2021-08-11 20:41:51 +00:00
for description in SENSOR_TYPES
]
async_add_entities(entities)
2023-10-23 13:34:28 +00:00
class BlinkSensor(CoordinatorEntity[BlinkUpdateCoordinator], SensorEntity):
"""A Blink camera sensor."""
_attr_has_entity_name = True
2023-10-23 13:34:28 +00:00
def __init__(
self,
coordinator: BlinkUpdateCoordinator,
camera,
description: SensorEntityDescription,
) -> None:
"""Initialize sensors from Blink camera."""
2023-10-23 13:34:28 +00:00
super().__init__(coordinator)
2021-08-11 20:41:51 +00:00
self.entity_description = description
2023-10-23 13:34:28 +00:00
self._camera = coordinator.api.cameras[camera]
2023-10-24 09:38:54 +00:00
serial = self._camera.serial
self._attr_unique_id = f"{serial}-{description.key}"
self._sensor_key = (
2021-08-11 20:41:51 +00:00
"temperature_calibrated"
if description.key == "temperature"
else description.key
)
self._attr_device_info = DeviceInfo(
2023-10-24 09:38:54 +00:00
identifiers={(DOMAIN, serial)},
serial_number=serial,
name=f"{DOMAIN} {camera}",
manufacturer=DEFAULT_BRAND,
model=self._camera.camera_type,
)
2023-10-23 13:34:28 +00:00
self._update_attr()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle coordinator update."""
self._update_attr()
super()._handle_coordinator_update()
2023-10-23 13:34:28 +00:00
@callback
def _update_attr(self) -> None:
"""Update attributes for sensor."""
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
try:
2023-10-23 13:34:28 +00:00
self._attr_native_value = self._camera.attributes[self._sensor_key]
_LOGGER.debug(
"'%s' %s = %s",
self._camera.attributes["name"],
self._sensor_key,
self._attr_native_value,
)
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
except KeyError:
2023-10-23 13:34:28 +00:00
self._attr_native_value = None
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
_LOGGER.error(
2019-07-31 19:25:30 +00:00
"%s not a valid camera attribute. Did the API change?", self._sensor_key
)