|
| 1 | +"""Camera platform for Imou.""" |
| 2 | +from collections.abc import Callable |
| 3 | +import logging |
| 4 | + |
| 5 | +from homeassistant.components.camera import ENTITY_ID_FORMAT, Camera |
| 6 | +from homeassistant.config_entries import ConfigEntry |
| 7 | +from homeassistant.core import HomeAssistant |
| 8 | +from homeassistant.helpers import entity_platform |
| 9 | +from imouapi.const import PTZ_OPERATIONS |
| 10 | +import voluptuous as vol |
| 11 | + |
| 12 | +from .const import ( |
| 13 | + ATTR_PTZ_DURATION, |
| 14 | + ATTR_PTZ_HORIZONTAL, |
| 15 | + ATTR_PTZ_OPERATION, |
| 16 | + ATTR_PTZ_VERTICAL, |
| 17 | + ATTR_PTZ_ZOOM, |
| 18 | + DOMAIN, |
| 19 | + SERVIZE_PTZ_LOCATION, |
| 20 | + SERVIZE_PTZ_MOVE, |
| 21 | +) |
| 22 | +from .entity import ImouEntity |
| 23 | + |
| 24 | +_LOGGER: logging.Logger = logging.getLogger(__package__) |
| 25 | + |
| 26 | + |
| 27 | +# async def async_setup_entry(hass, entry, async_add_devices): |
| 28 | +async def async_setup_entry( |
| 29 | + hass: HomeAssistant, entry: ConfigEntry, async_add_devices: Callable |
| 30 | +): |
| 31 | + """Configure platform.""" |
| 32 | + platform = entity_platform.async_get_current_platform() |
| 33 | + |
| 34 | + # Create PTZ location service |
| 35 | + platform.async_register_entity_service( |
| 36 | + SERVIZE_PTZ_LOCATION, |
| 37 | + { |
| 38 | + vol.Required(ATTR_PTZ_HORIZONTAL, default=0): vol.Range(min=-1, max=1), |
| 39 | + vol.Required(ATTR_PTZ_VERTICAL, default=0): vol.Range(min=-1, max=1), |
| 40 | + vol.Required(ATTR_PTZ_ZOOM, default=0): vol.Range(min=0, max=1), |
| 41 | + }, |
| 42 | + "async_service_ptz_location", |
| 43 | + ) |
| 44 | + |
| 45 | + # Create PTZ move service |
| 46 | + platform.async_register_entity_service( |
| 47 | + SERVIZE_PTZ_MOVE, |
| 48 | + { |
| 49 | + vol.Required(ATTR_PTZ_OPERATION, default=0): vol.In(list(PTZ_OPERATIONS)), |
| 50 | + vol.Required(ATTR_PTZ_DURATION, default=1000): vol.Range( |
| 51 | + min=100, max=10000 |
| 52 | + ), |
| 53 | + }, |
| 54 | + "async_service_ptz_move", |
| 55 | + ) |
| 56 | + |
| 57 | + coordinator = hass.data[DOMAIN][entry.entry_id] |
| 58 | + device = coordinator.device |
| 59 | + sensors = [] |
| 60 | + for sensor_instance in device.get_sensors_by_platform("camera"): |
| 61 | + sensor = ImouCamera(coordinator, entry, sensor_instance, ENTITY_ID_FORMAT) |
| 62 | + sensors.append(sensor) |
| 63 | + coordinator.entities.append(sensor) |
| 64 | + _LOGGER.debug( |
| 65 | + "[%s] Adding %s", device.get_name(), sensor_instance.get_description() |
| 66 | + ) |
| 67 | + async_add_devices(sensors) |
| 68 | + |
| 69 | + |
| 70 | +class ImouCamera(ImouEntity, Camera): |
| 71 | + """imou camera class.""" |
| 72 | + |
| 73 | + def __init__(self, coordinator, config_entry, sensor_instance, entity_format): |
| 74 | + """Initialize.""" |
| 75 | + ImouEntity.__init__( |
| 76 | + self, coordinator, config_entry, sensor_instance, entity_format |
| 77 | + ) |
| 78 | + Camera.__init__(self) |
| 79 | + |
| 80 | + async def async_service_ptz_location(self, horizontal, vertical, zoom): |
| 81 | + """Perform PTZ location action.""" |
| 82 | + _LOGGER.debug( |
| 83 | + "[%s] invoked PTZ location action horizontal:%f, vertical:%f, zoom:%f", |
| 84 | + self.device.get_name(), |
| 85 | + horizontal, |
| 86 | + vertical, |
| 87 | + zoom, |
| 88 | + ) |
| 89 | + await self.sensor_instance.async_service_ptz_location( |
| 90 | + horizontal, |
| 91 | + vertical, |
| 92 | + zoom, |
| 93 | + ) |
| 94 | + |
| 95 | + async def async_service_ptz_move(self, operation, duration): |
| 96 | + """Perform PTZ move action.""" |
| 97 | + _LOGGER.debug( |
| 98 | + "[%s] invoked PTZ move action operation:%s, duration:%i", |
| 99 | + self.device.get_name(), |
| 100 | + operation, |
| 101 | + duration, |
| 102 | + ) |
| 103 | + await self.sensor_instance.async_service_ptz_move( |
| 104 | + operation, |
| 105 | + duration, |
| 106 | + ) |
| 107 | + |
| 108 | + async def async_camera_image(self, width=None, height=None): |
| 109 | + """Return bytes of camera image.""" |
| 110 | + return None |
0 commit comments