core/homeassistant/components/camera/bloomsky.py

62 lines
2.1 KiB
Python
Raw Normal View History

2016-02-04 20:01:45 +00:00
"""
2016-02-06 07:23:30 +00:00
Support for a camera of a BloomSky weather station.
2016-02-04 20:01:45 +00:00
For more details about this component, please refer to the documentation at
2016-02-06 07:23:30 +00:00
https://home-assistant.io/components/camera.bloomsky/
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-02-19 05:27:50 +00:00
from homeassistant.loader import get_component
2016-02-04 20:01:45 +00:00
2016-08-26 20:50:32 +00:00
DEPENDENCIES = ['bloomsky']
2016-02-04 20:01:45 +00:00
# pylint: disable=unused-argument
2016-08-26 20:50:32 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up access to BloomSky cameras."""
2016-02-18 21:10:25 +00:00
bloomsky = get_component('bloomsky')
2016-02-04 20:01:45 +00:00
for device in bloomsky.BLOOMSKY.devices.values():
2016-08-26 20:50:32 +00:00
add_devices([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 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