core/homeassistant/components/bloomsky/camera.py

60 lines
2.0 KiB
Python
Raw Normal View History

"""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
2016-08-26 20:50:32 +00:00
DEPENDENCIES = ['bloomsky']
2016-02-04 20:01:45 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up access to BloomSky cameras."""
bloomsky = hass.components.bloomsky
2016-02-04 20:01:45 +00:00
for device in bloomsky.BLOOMSKY.devices.values():
add_entities([BloomSkyCamera(bloomsky.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):
"""Initialize access to the BloomSky camera images."""
2016-02-04 20:01:45 +00:00
super(BloomSkyCamera, self).__init__()
2016-08-26 20:50:32 +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 = ""
# 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:
2016-08-26 20:50:32 +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
@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