import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.fan import (FanEntity, DOMAIN, SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util import Throttle
from datetime import timedelta
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=20)
PRODUCT_ID = 'xxx'
DEVICE_ID = 'xxx'
APP_KEY = 'xxx'
SPEED_MAX = 'max'
def _get_speed_word(speed):
if speed == 0:
return SPEED_OFF
elif 0 < speed <= 20:
return SPEED_LOW
elif 20 < speed <= 30:
return SPEED_MEDIUM
elif 30 < speed <= 40:
return SPEED_HIGH
elif 40 < speed:
return SPEED_MAX
def _get_speed_number(speed_word):
if speed_word == SPEED_OFF:
return 0
elif speed_word == SPEED_LOW:
return 10
elif speed_word == SPEED_MEDIUM:
return 30
elif speed_word == SPEED_HIGH:
return 40
elif speed_word == SPEED_MAX:
return 50
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
add_entities_callback([DreamMaker(hass, config)])
class DreamMaker(FanEntity):
def __init__(self, hass, config):
self._hass = hass
self._config = config
self._name = 'Dream Maker Fresh Air System'
self._speed = SPEED_OFF
self._state_attrs = {
'humidity' : None,
'temperature' : None,
'aqi' : None,
'aqi_out' : None,
'co2' : None,
}
self.async_update = Throttle(SCAN_INTERVAL)(self._async_update)
@property
def name(self):
return self._name
@property
def speed(self):
return self._speed
@property
def speed_list(self):
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH, SPEED_MAX]
@property
def device_state_attributes(self):
return self._state_attrs
async def async_turn_on(self, speed: str=None, **kwargs) -> None:
if(speed is None):
speed = SPEED_MEDIUM
await self.async_set_speed(speed)
async def async_turn_off(self, **kwargs) -> None:
await self.async_set_speed(SPEED_OFF)
async def async_set_speed(self, speed: str) -> None:
old_speed = self._speed
self._speed = speed
if speed == old_speed:
return
if speed == SPEED_OFF:
# power off
await self._request({"action":4,"data":{"power":0, "source":"manual"},"resource_id":9001,"version":"zeico_2.0.0"})
else:
if old_speed == SPEED_OFF:
await self._request({"action":4,"data":{"power":1, "source":"manual"},"resource_id":9001,"version":"zeico_2.0.0"})
speed_num = _get_speed_number(speed)
await self._request({"action":4,"data":{"fanspeed":speed_num},"resource_id":9001,"version":"zeico_2.0.0"})
async def _async_update(self):
data = await self._request()
self._speed = _get_speed_word(data['fanspeed'])
self._state_attrs.update({
"humidity" : data['rh'],
"temperature" : data['temp'],
"aqi" : data['pm25'],
"aqi_out" : data['pm25_out'],
"co2" : data['co2'],
})
async def _request(self, post_data=None):
websession = async_get_clientsession(self._hass, False)
method = 'get' if post_data is None else 'post'
url = 'https://api.dream-maker.com:8444/APIServer/v1/control/product/%s/device/%s' % (PRODUCT_ID, DEVICE_ID)
headers = {
"app_key": APP_KEY
}
async with websession.request(method, url, json=post_data,headers=headers) as resp:
resp_data = (await resp.json())['data']
return resp_data