async states
This commit is contained in:
@@ -3,14 +3,13 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.helpers import discovery
|
||||
from homeassistant.components.hassio import (
|
||||
async_get_addon_info,
|
||||
get_addons_info,
|
||||
)
|
||||
from homeassistant.const import CONF_INTERNAL_URL
|
||||
|
||||
DOMAIN = "airsend"
|
||||
AS_TYPE = ["switch", "cover", "button"]
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType):
|
||||
"""Set up the AirSend component."""
|
||||
if DOMAIN not in config:
|
||||
@@ -22,12 +21,16 @@ async def async_setup(hass: HomeAssistant, config: ConfigType):
|
||||
pass
|
||||
if internalurl == "":
|
||||
try:
|
||||
addon_info: dict = await async_get_addon_info(hass, "local_airsend")
|
||||
ip = addon_info["ip_address"]
|
||||
if ip:
|
||||
internalurl = "http://" + str(ip) + ":33863/"
|
||||
except KeyError:
|
||||
addons_info = get_addons_info(hass)
|
||||
for name, options in addons_info.items():
|
||||
if "_airsend" in name:
|
||||
ip = options["ip_address"]
|
||||
if ip:
|
||||
internalurl = "http://" + str(ip) + ":33863/"
|
||||
except:
|
||||
pass
|
||||
if internalurl != "" and not internalurl.endswith('/'):
|
||||
internalurl += "/"
|
||||
config[DOMAIN][CONF_INTERNAL_URL] = internalurl
|
||||
for plateform in AS_TYPE:
|
||||
discovery.load_platform(hass, plateform, DOMAIN, config[DOMAIN].copy(), config)
|
||||
|
||||
@@ -72,5 +72,5 @@ class AirSendButton(ButtonEntity):
|
||||
def press(self, **kwargs: Any) -> None:
|
||||
"""Handle the button press."""
|
||||
note = {"method": 1, "type": 0, "value": "TOGGLE"}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
@@ -6,7 +6,6 @@ from .device import Device
|
||||
from homeassistant.components.cover import CoverEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from homeassistant.const import CONF_DEVICES, CONF_INTERNAL_URL
|
||||
|
||||
from . import DOMAIN
|
||||
@@ -75,12 +74,12 @@ class AirSendCover(CoverEntity):
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return if the cover is closed."""
|
||||
return not self._closed
|
||||
return self._closed
|
||||
|
||||
def open_cover(self, **kwargs: Any) -> None:
|
||||
"""Open the cover."""
|
||||
note = {"method": 1, "type": 0, "value": "UP"}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self._closed = False
|
||||
if self._device.is_cover_with_position:
|
||||
self._attr_current_cover_position = 100
|
||||
@@ -89,7 +88,7 @@ class AirSendCover(CoverEntity):
|
||||
def close_cover(self, **kwargs: Any) -> None:
|
||||
"""Close cover."""
|
||||
note = {"method": 1, "type": 0, "value": "DOWN"}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self._closed = True
|
||||
if self._device.is_cover_with_position:
|
||||
self._attr_current_cover_position = 0
|
||||
@@ -98,7 +97,7 @@ class AirSendCover(CoverEntity):
|
||||
def stop_cover(self, **kwargs):
|
||||
"""Stop the cover."""
|
||||
note = {"method": 1, "type": 0, "value": "STOP"}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self._closed = False
|
||||
if self._device.is_cover_with_position:
|
||||
self._attr_current_cover_position = 50
|
||||
@@ -108,7 +107,7 @@ class AirSendCover(CoverEntity):
|
||||
"""Move the cover to a specific position."""
|
||||
position = int(kwargs["position"])
|
||||
note = {"method": 1, "type": 9, "value": position}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self._attr_current_cover_position = position
|
||||
self._closed = False
|
||||
if self._attr_current_cover_position == 0:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""AirSend device."""
|
||||
import logging
|
||||
import json
|
||||
import hashlib
|
||||
from requests import get, post, exceptions
|
||||
from . import DOMAIN
|
||||
|
||||
@@ -23,6 +24,7 @@ class Device:
|
||||
self._rtype = None
|
||||
self._apikey = None
|
||||
self._spurl = None
|
||||
self._wait = False
|
||||
self._channel = {}
|
||||
self._note = None
|
||||
try:
|
||||
@@ -41,6 +43,10 @@ class Device:
|
||||
self._spurl = options["spurl"]
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
self._wait = eval(str(options["wait"]))
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
self._channel = options["channel"]
|
||||
except KeyError:
|
||||
@@ -83,10 +89,15 @@ class Device:
|
||||
return True
|
||||
return False
|
||||
|
||||
def transfer(self, note) -> bool:
|
||||
def transfer(self, note, entity_id = None) -> bool:
|
||||
"""Send a command."""
|
||||
status_code = 404
|
||||
if self._serviceurl and self._spurl:
|
||||
ret = False
|
||||
wait = 'false, "callback":"http://127.0.0.1/"'
|
||||
if self._wait == True:
|
||||
wait = 'true'
|
||||
if self._serviceurl and self._spurl and entity_id is not None:
|
||||
uid = hashlib.sha256(entity_id.encode('utf-8')).hexdigest()[:12]
|
||||
jnote = json.dumps(note)
|
||||
if (
|
||||
self._note is not None
|
||||
@@ -101,9 +112,9 @@ class Device:
|
||||
):
|
||||
jnote = json.dumps(self._note)
|
||||
payload = (
|
||||
'{"wait": true, "channel":'
|
||||
'{"wait": '+wait+', "channel":'
|
||||
+ json.dumps(self._channel)
|
||||
+ ', "thingnotes":{"notes":['
|
||||
+ ', "thingnotes":{"uid":"0x'+uid+'", "notes":['
|
||||
+ jnote
|
||||
+ "]}}"
|
||||
)
|
||||
@@ -119,9 +130,14 @@ class Device:
|
||||
data=payload,
|
||||
timeout=6,
|
||||
)
|
||||
status_code = 500
|
||||
jdata = json.loads(response.text)
|
||||
if jdata["type"] < 0x100:
|
||||
if self._wait == True:
|
||||
ret = True
|
||||
status_code = 500
|
||||
jdata = json.loads(response.text)
|
||||
if jdata["type"] < 0x100:
|
||||
status_code = response.status_code
|
||||
else:
|
||||
ret = None
|
||||
status_code = response.status_code
|
||||
except exceptions.RequestException:
|
||||
pass
|
||||
@@ -164,9 +180,10 @@ class Device:
|
||||
try:
|
||||
response = get(cloud_url, headers=headers, timeout=10)
|
||||
status_code = response.status_code
|
||||
ret = True
|
||||
except exceptions.RequestException:
|
||||
pass
|
||||
if status_code == 200:
|
||||
return True
|
||||
return ret
|
||||
_LOGGER.error("Transfer error '%s' : '%s'", self.name, status_code)
|
||||
raise Exception("Transfer error " + self.name + " : " + str(status_code))
|
||||
|
||||
@@ -77,13 +77,13 @@ class AirSendSwitch(SwitchEntity):
|
||||
def turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the device on."""
|
||||
note = {"method": 1, "type": 0, "value": "ON"}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self._state = True
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
def turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the device off."""
|
||||
note = {"method": 1, "type": 0, "value": "OFF"}
|
||||
if self._device.transfer(note):
|
||||
if self._device.transfer(note, self.entity_id) == True:
|
||||
self._state = False
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
Reference in New Issue
Block a user