Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -8,8 +8,12 @@ import voluptuous as vol
|
||||
import aiohttp
|
||||
import async_timeout
|
||||
|
||||
from homeassistant.components.camera import Camera, PLATFORM_SCHEMA,\
|
||||
STATE_IDLE, STATE_RECORDING
|
||||
from homeassistant.components.camera import (
|
||||
Camera,
|
||||
PLATFORM_SCHEMA,
|
||||
STATE_IDLE,
|
||||
STATE_RECORDING,
|
||||
)
|
||||
from homeassistant.components.camera.const import DOMAIN
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import CONF_NAME, CONF_TIMEOUT, CONF_WEBHOOK_ID
|
||||
@@ -19,40 +23,46 @@ import homeassistant.util.dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_BUFFER_SIZE = 'buffer'
|
||||
CONF_IMAGE_FIELD = 'field'
|
||||
CONF_BUFFER_SIZE = "buffer"
|
||||
CONF_IMAGE_FIELD = "field"
|
||||
|
||||
DEFAULT_NAME = "Push Camera"
|
||||
|
||||
ATTR_FILENAME = 'filename'
|
||||
ATTR_LAST_TRIP = 'last_trip'
|
||||
ATTR_FILENAME = "filename"
|
||||
ATTR_LAST_TRIP = "last_trip"
|
||||
|
||||
PUSH_CAMERA_DATA = 'push_camera'
|
||||
PUSH_CAMERA_DATA = "push_camera"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_BUFFER_SIZE, default=1): cv.positive_int,
|
||||
vol.Optional(CONF_TIMEOUT, default=timedelta(seconds=5)): vol.All(
|
||||
cv.time_period, cv.positive_timedelta),
|
||||
vol.Optional(CONF_IMAGE_FIELD, default='image'): cv.string,
|
||||
vol.Required(CONF_WEBHOOK_ID): cv.string,
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_BUFFER_SIZE, default=1): cv.positive_int,
|
||||
vol.Optional(CONF_TIMEOUT, default=timedelta(seconds=5)): vol.All(
|
||||
cv.time_period, cv.positive_timedelta
|
||||
),
|
||||
vol.Optional(CONF_IMAGE_FIELD, default="image"): cv.string,
|
||||
vol.Required(CONF_WEBHOOK_ID): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the Push Camera platform."""
|
||||
if PUSH_CAMERA_DATA not in hass.data:
|
||||
hass.data[PUSH_CAMERA_DATA] = {}
|
||||
|
||||
webhook_id = config.get(CONF_WEBHOOK_ID)
|
||||
|
||||
cameras = [PushCamera(hass,
|
||||
config[CONF_NAME],
|
||||
config[CONF_BUFFER_SIZE],
|
||||
config[CONF_TIMEOUT],
|
||||
config[CONF_IMAGE_FIELD],
|
||||
webhook_id)]
|
||||
cameras = [
|
||||
PushCamera(
|
||||
hass,
|
||||
config[CONF_NAME],
|
||||
config[CONF_BUFFER_SIZE],
|
||||
config[CONF_TIMEOUT],
|
||||
config[CONF_IMAGE_FIELD],
|
||||
webhook_id,
|
||||
)
|
||||
]
|
||||
|
||||
async_add_entities(cameras)
|
||||
|
||||
@@ -69,19 +79,18 @@ async def handle_webhook(hass, webhook_id, request):
|
||||
camera = hass.data[PUSH_CAMERA_DATA][webhook_id]
|
||||
|
||||
if camera.image_field not in data:
|
||||
_LOGGER.warning("Webhook call without POST parameter <%s>",
|
||||
camera.image_field)
|
||||
_LOGGER.warning("Webhook call without POST parameter <%s>", camera.image_field)
|
||||
return
|
||||
|
||||
await camera.update_image(data[camera.image_field].file.read(),
|
||||
data[camera.image_field].filename)
|
||||
await camera.update_image(
|
||||
data[camera.image_field].file.read(), data[camera.image_field].filename
|
||||
)
|
||||
|
||||
|
||||
class PushCamera(Camera):
|
||||
"""The representation of a Push camera."""
|
||||
|
||||
def __init__(self, hass, name, buffer_size, timeout, image_field,
|
||||
webhook_id):
|
||||
def __init__(self, hass, name, buffer_size, timeout, image_field, webhook_id):
|
||||
"""Initialize push camera component."""
|
||||
super().__init__()
|
||||
self._name = name
|
||||
@@ -94,21 +103,20 @@ class PushCamera(Camera):
|
||||
self._current_image = None
|
||||
self._image_field = image_field
|
||||
self.webhook_id = webhook_id
|
||||
self.webhook_url = \
|
||||
hass.components.webhook.async_generate_url(webhook_id)
|
||||
self.webhook_url = hass.components.webhook.async_generate_url(webhook_id)
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity is added to hass."""
|
||||
self.hass.data[PUSH_CAMERA_DATA][self.webhook_id] = self
|
||||
|
||||
try:
|
||||
self.hass.components.webhook.async_register(DOMAIN,
|
||||
self.name,
|
||||
self.webhook_id,
|
||||
handle_webhook)
|
||||
self.hass.components.webhook.async_register(
|
||||
DOMAIN, self.name, self.webhook_id, handle_webhook
|
||||
)
|
||||
except ValueError:
|
||||
_LOGGER.error("In <%s>, webhook_id <%s> already used",
|
||||
self.name, self.webhook_id)
|
||||
_LOGGER.error(
|
||||
"In <%s>, webhook_id <%s> already used", self.name, self.webhook_id
|
||||
)
|
||||
|
||||
@property
|
||||
def image_field(self):
|
||||
@@ -142,7 +150,8 @@ class PushCamera(Camera):
|
||||
self._expired_listener()
|
||||
|
||||
self._expired_listener = async_track_point_in_utc_time(
|
||||
self.hass, reset_state, dt_util.utcnow() + self._timeout)
|
||||
self.hass, reset_state, dt_util.utcnow() + self._timeout
|
||||
)
|
||||
|
||||
self.async_schedule_update_ha_state()
|
||||
|
||||
@@ -169,8 +178,10 @@ class PushCamera(Camera):
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return {
|
||||
name: value for name, value in (
|
||||
name: value
|
||||
for name, value in (
|
||||
(ATTR_LAST_TRIP, self._last_trip),
|
||||
(ATTR_FILENAME, self._filename),
|
||||
) if value is not None
|
||||
)
|
||||
if value is not None
|
||||
}
|
||||
Reference in new issue
Block a user