2019-02-13 20:21:14 +00:00
|
|
|
"""Support for a camera of a BloomSky weather station."""
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
import requests
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
|
|
|
|
2020-04-05 21:16:30 +00:00
|
|
|
from . import DOMAIN
|
2019-04-12 06:37:45 +00:00
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up access to BloomSky cameras."""
|
2020-04-23 23:00:57 +00:00
|
|
|
if discovery_info is not None:
|
|
|
|
return
|
|
|
|
|
2020-04-05 21:16:30 +00:00
|
|
|
bloomsky = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
for device in bloomsky.devices.values():
|
|
|
|
add_entities([BloomSkyCamera(bloomsky, device)])
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BloomSkyCamera(Camera):
|
2016-03-07 19:29:54 +00:00
|
|
|
"""Representation of the images published from the BloomSky's camera."""
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
def __init__(self, bs, device):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Initialize access to the BloomSky camera images."""
|
2019-09-24 22:38:20 +00:00
|
|
|
super().__init__()
|
2021-07-15 13:29:12 +00:00
|
|
|
self._attr_name = device["DeviceName"]
|
2019-07-31 19:25:30 +00:00
|
|
|
self._id = device["DeviceID"]
|
2016-02-04 20:01:45 +00:00
|
|
|
self._bloomsky = bs
|
|
|
|
self._url = ""
|
|
|
|
self._last_url = ""
|
2017-04-30 05:04:49 +00:00
|
|
|
# last_image will store images as they are downloaded so that the
|
2016-02-04 20:01:45 +00:00
|
|
|
# frequent updates in home-assistant don't keep poking the server
|
2016-03-07 16:45:06 +00:00
|
|
|
# to download the same image over and over.
|
2016-02-04 20:01:45 +00:00
|
|
|
self._last_image = ""
|
|
|
|
self._logger = logging.getLogger(__name__)
|
2021-07-15 13:29:12 +00:00
|
|
|
self._attr_unique_id = self._id
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2016-03-07 16:45:06 +00:00
|
|
|
"""Update the camera's image if it has changed."""
|
2016-02-04 20:01:45 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._url = self._bloomsky.devices[self._id]["Data"]["ImageURL"]
|
2016-02-04 20:01:45 +00:00
|
|
|
self._bloomsky.refresh_devices()
|
2016-03-07 16:45:06 +00:00
|
|
|
# If the URL hasn't changed then the image hasn't changed.
|
2016-02-04 20:01:45 +00:00
|
|
|
if self._url != self._last_url:
|
|
|
|
response = requests.get(self._url, timeout=10)
|
|
|
|
self._last_url = self._url
|
|
|
|
self._last_image = response.content
|
|
|
|
except requests.exceptions.RequestException as error:
|
|
|
|
self._logger.error("Error getting bloomsky image: %s", error)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return self._last_image
|