Can one call hass.service from inside a custom component? My custom component code below fails when attempting to do so.
from homeassistant.helpers.entity import Entity
import numpy as np
import time
from PIL import Image
import logging
from collections import namedtuple
from datetime import timedelta
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
CAMERA_ENTITY_ID = 'camera.garage'
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the sensor platform."""
add_entities([GarageDoorClosedSensor()])
class GarageDoorClosedSensor(Entity):
"""Representation of a Sensor."""
def getPicArray(self,file):#
'''Return Array from image file'''
return np.array(Image.open(file).crop(self.cropBox)).astype(np.int)
def checkIfGrey(self,picArr):#
'''Return bool if grey'''
return np.max(np.sum(np.diff(picArr),2))<self.greyRGBthresh
def __init__(self):
"""Initialize the sensor."""
self.greyRGBthresh = 10
self.diffScoreThresh = 8
self.current_garage_pic_file = "/config/tmp/snapshotGarage.jpg"
self.closed_garage_pic_file = "/config/custom_components/garage_door_closed/snapshotGarageClosed.jpg"
self.cropBox=(550,100,1300,300)
im=Image.open(self.closed_garage_pic_file)
im=im.crop(self.cropBox)
im=np.array(im)
self.cloGarArr=im.astype(np.int)
#self.cloGarArr=self.getPicArray(self.closed_garage_pic_file)
self.cloGarGrey=np.max(np.sum(np.diff(self.cloGarArr),2))<self.greyRGBthresh#self.checkIfGrey(self.cloGarArr)#bool
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return 'Garage Door Closed Sensor'
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@Throttle(MIN_TIME_BETWEEN_SCANS)
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
#this no work#hass.services.call('camera', 'snapshot',{'entity_id': CAMERA_ENTITY_ID,'filename': self.current_garage_pic_file})
#time.sleep(3)
self.curGarArr=np.array(Image.open(self.current_garage_pic_file).crop(self.cropBox)).astype(np.int)#self.getPicArray(self.current_garage_pic_file)
if (np.max(np.sum(np.diff(self.cloGarArr),2))<self.greyRGBthresh)!=self.cloGarGrey:
self._state = False
else:
self.diffScore = np.sum(np.sqrt((self.curGarArr-self.cloGarArr-np.mean(self.curGarArr-self.cloGarArr))**2))/(np.prod(self.cloGarArr.shape))
if self.diffScore<self.diffScoreThresh:
self._state = True
else:
self._state = False