本文整理匯總了Python中homeassistant.exceptions.HomeAssistantError方法的典型用法代碼示例。如果您正苦於以下問題:Python exceptions.HomeAssistantError方法的具體用法?Python exceptions.HomeAssistantError怎麽用?Python exceptions.HomeAssistantError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類homeassistant.exceptions
的用法示例。
在下文中一共展示了exceptions.HomeAssistantError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def __init__(self, hass, call_device_name, call_entity_id):
"""Initialize a spotify cast device."""
self.hass = hass
# Get device name from either device_name or entity_id
device_name = None
if call_device_name is None:
entity_id = call_entity_id
if entity_id is None:
raise HomeAssistantError("Either entity_id or device_name must be specified")
entity_states = hass.states.get(entity_id)
if entity_states is None:
_LOGGER.error("Could not find entity_id: %s", entity_id)
else:
device_name = entity_states.attributes.get("friendly_name")
else:
device_name = call_device_name
if device_name is None or device_name.strip() == "":
raise HomeAssistantError("device_name is empty")
# Find chromecast device
self.castDevice = self.getChromecastDevice(device_name)
_LOGGER.debug("Found cast device: %s", self.castDevice)
self.castDevice.wait()
示例2: websocket_snapshot_image
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def websocket_snapshot_image(hass, connection, msg):
camera = _get_camera_from_entity_id(hass, msg['entity_id'])
_LOGGER.debug('snapshot_image for ' + str(camera.unique_id))
try:
image = await camera.async_get_snapshot()
connection.send_message(websocket_api.result_message(
msg['id'], {
'content_type': camera.content_type,
'content': base64.b64encode(image).decode('utf-8')
}
))
except HomeAssistantError:
connection.send_message(websocket_api.error_message(
msg['id'], 'image_fetch_failed', 'Unable to fetch image'))
示例3: websocket_video_data
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def websocket_video_data(hass, connection, msg):
camera = _get_camera_from_entity_id(hass, msg['entity_id'])
_LOGGER.debug('video_data for ' + str(camera.unique_id))
try:
video = await camera.async_get_video()
connection.send_message(websocket_api.result_message(
msg['id'], {
'content_type': 'video/mp4',
'content': base64.b64encode(video).decode('utf-8')
}
))
except HomeAssistantError:
connection.send_message(websocket_api.error_message(
msg['id'], 'video_fetch_failed', 'Unable to fetch video'))
示例4: websocket_video_url
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def websocket_video_url(hass, connection, msg):
try:
camera = get_entity_from_domain(hass, DOMAIN, msg['entity_id'])
video = camera.last_video
url = video.video_url if video is not None else None
url_type = video.content_type if video is not None else None
thumbnail = video.thumbnail_url if video is not None else None
connection.send_message(websocket_api.result_message(
msg['id'], {
'url': url,
'url_type': url_type,
'thumbnail': thumbnail,
'thumbnail_type': 'image/jpeg',
}
))
except HomeAssistantError as error:
connection.send_message(websocket_api.error_message(
msg['id'], 'video_url_ws', "Unable to fetch url ({})".format(str(error))))
_LOGGER.warning("{} video url websocket failed".format(msg['entity_id']))
示例5: websocket_snapshot_image
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def websocket_snapshot_image(hass, connection, msg):
try:
camera = get_entity_from_domain(hass, DOMAIN, msg['entity_id'])
_LOGGER.debug('snapshot_image for ' + str(camera.unique_id))
image = await camera.async_get_snapshot()
connection.send_message(websocket_api.result_message(
msg['id'], {
'content_type': camera.content_type,
'content': base64.b64encode(image).decode('utf-8')
}
))
except HomeAssistantError as error:
connection.send_message(websocket_api.error_message(
msg['id'], 'snapshot_image_ws', "Unable to take snapshot ({})".format(str(error))))
_LOGGER.warning("{} snapshot image websocket failed".format(msg['entity_id']))
示例6: websocket_video_data
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def websocket_video_data(hass, connection, msg):
try:
camera = get_entity_from_domain(hass, DOMAIN, msg['entity_id'])
_LOGGER.debug('video_data for ' + str(camera.unique_id))
video = await camera.async_get_video()
connection.send_message(websocket_api.result_message(
msg['id'], {
'content_type': 'video/mp4',
'content': base64.b64encode(video).decode('utf-8')
}
))
except HomeAssistantError as error:
connection.send_message(websocket_api.error_message(
msg['id'], 'video_data_ws', "Unable to get video data ({})".format(str(error))))
_LOGGER.warning("{} video data websocket failed".format(msg['entity_id']))
示例7: get_spotify_token
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def get_spotify_token(self):
import spotify_token as st
try:
self._access_token, self._token_expires = st.start_session(self.sp_dc, self.sp_key)
expires = self._token_expires - int(time.time())
return self._access_token, expires
except:
raise HomeAssistantError("Could not get spotify token")
示例8: getChromecastDevice
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def getChromecastDevice(self, device_name):
import pychromecast
# Get cast from discovered devices of cast platform
known_devices = self.hass.data.get(KNOWN_CHROMECAST_INFO_KEY, [])
cast_info = next((x for x in known_devices if x.friendly_name == device_name), None)
_LOGGER.debug("cast info: %s", cast_info)
if cast_info:
return pychromecast._get_chromecast_from_host(
(
cast_info.host,
cast_info.port,
cast_info.uuid,
cast_info.model_name,
cast_info.friendly_name,
)
)
_LOGGER.error(
"Could not find device %s from hass.data, falling back to pychromecast scan",
device_name,
)
# Discover devices manually
chromecasts = pychromecast.get_chromecasts()
for _cast in chromecasts:
if _cast.name == device_name:
_LOGGER.debug("Fallback, found cast device: %s", _cast)
return _cast
raise HomeAssistantError("Could not find device with name {}".format(device_name))
示例9: startSpotifyController
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def startSpotifyController(self, access_token, expires):
from pychromecast.controllers.spotify import SpotifyController
sp = SpotifyController(access_token, expires)
self.castDevice.register_handler(sp)
sp.launch_app()
if not sp.is_launched and not sp.credential_error:
raise HomeAssistantError("Failed to launch spotify controller due to timeout")
if not sp.is_launched and sp.credential_error:
raise HomeAssistantError("Failed to launch spotify controller due to credentials error")
self.spotifyController = sp
示例10: getSpotifyDeviceId
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def getSpotifyDeviceId(self, client):
# Look for device
devices_available = client.devices()
for device in devices_available["devices"]:
if device["id"] == self.spotifyController.device:
return device["id"]
_LOGGER.error(
'No device with id "{}" known by Spotify'.format(self.spotifyController.device)
)
_LOGGER.error("Known devices: {}".format(devices_available["devices"]))
raise HomeAssistantError("Failed to get device id from Spotify")
示例11: _load_json
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def _load_json(filename):
"""Wrapper, because we actually want to handle invalid json."""
try:
return load_json(filename)
except HomeAssistantError:
pass
return {}
示例12: _get_camera_from_entity_id
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def _get_camera_from_entity_id(hass, entity_id):
component = hass.data.get(DOMAIN)
if component is None:
raise HomeAssistantError('Camera component not set up')
camera = component.get_entity(entity_id)
if camera is None:
raise HomeAssistantError('Camera not found')
return camera
示例13: websocket_stream_url
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def websocket_stream_url(hass, connection, msg):
camera = _get_camera_from_entity_id(hass, msg['entity_id'])
_LOGGER.debug('stream_url for ' + str(camera.unique_id))
try:
stream = await camera.async_stream_source()
connection.send_message(websocket_api.result_message(
msg['id'], {
'url': stream
}
))
except HomeAssistantError:
connection.send_message(websocket_api.error_message(
msg['id'], 'image_fetch_failed', 'Unable to fetch stream'))
示例14: _get_base_from_entity_id
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def _get_base_from_entity_id(hass, entity_id):
component = hass.data.get(DOMAIN)
if component is None:
raise HomeAssistantError('base component not set up')
base = component.get_entity(entity_id)
if base is None:
raise HomeAssistantError('base not found')
return base
示例15: get_entity_from_domain
# 需要導入模塊: from homeassistant import exceptions [as 別名]
# 或者: from homeassistant.exceptions import HomeAssistantError [as 別名]
def get_entity_from_domain(hass, domains, entity_id):
domains = domains if isinstance(domains, list) else [domains]
for domain in domains:
component = hass.data.get(domain)
if component is None:
raise HomeAssistantError("{} component not set up".format(domain))
entity = component.get_entity(entity_id)
if entity is not None:
return entity
raise HomeAssistantError("{} not found in {}".format(entity_id, ",".join(domains)))