2019-02-13 20:21:14 +00:00
|
|
|
"""Support for a camera of a BloomSky weather station."""
|
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
|
|
|
|
|
2019-04-12 06:37:45 +00:00
|
|
|
from . import BLOOMSKY
|
|
|
|
|
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."""
|
2019-04-12 06:37:45 +00:00
|
|
|
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__()
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = device["DeviceName"]
|
|
|
|
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__)
|
|
|
|
|
|
|
|
def camera_image(self):
|
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
|
|
|
|
|
2018-10-13 08:23:00 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._id
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 19:29:54 +00:00
|
|
|
"""Return the name of this BloomSky device."""
|
2016-02-04 20:01:45 +00:00
|
|
|
return self._name
|