2019-02-13 20:21:14 +00:00
|
|
|
"""Support for a Hue API to control Home Assistant."""
|
2020-06-10 13:15:32 +00:00
|
|
|
import asyncio
|
2019-12-02 05:00:22 +00:00
|
|
|
import hashlib
|
2020-08-11 20:57:50 +00:00
|
|
|
from ipaddress import ip_address
|
2019-12-09 11:12:41 +00:00
|
|
|
import logging
|
2020-08-04 01:30:16 +00:00
|
|
|
import time
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
from homeassistant import core
|
2019-04-02 18:25:58 +00:00
|
|
|
from homeassistant.components import (
|
2019-07-31 19:25:30 +00:00
|
|
|
climate,
|
|
|
|
cover,
|
|
|
|
fan,
|
2020-07-04 22:53:36 +00:00
|
|
|
humidifier,
|
2019-07-31 19:25:30 +00:00
|
|
|
light,
|
|
|
|
media_player,
|
|
|
|
scene,
|
|
|
|
script,
|
|
|
|
)
|
2019-03-10 23:04:21 +00:00
|
|
|
from homeassistant.components.climate.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_SET_TEMPERATURE,
|
|
|
|
SUPPORT_TARGET_TEMPERATURE,
|
|
|
|
)
|
2019-02-11 18:59:34 +00:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CURRENT_POSITION,
|
|
|
|
ATTR_POSITION,
|
|
|
|
SERVICE_SET_COVER_POSITION,
|
|
|
|
SUPPORT_SET_POSITION,
|
|
|
|
)
|
2019-04-02 18:25:58 +00:00
|
|
|
from homeassistant.components.fan import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_SPEED,
|
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
)
|
2016-12-04 18:57:48 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2020-07-04 22:53:36 +00:00
|
|
|
from homeassistant.components.humidifier.const import (
|
|
|
|
ATTR_HUMIDITY,
|
|
|
|
SERVICE_SET_HUMIDITY,
|
|
|
|
)
|
2019-04-02 18:25:58 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
2019-12-02 05:00:22 +00:00
|
|
|
ATTR_COLOR_TEMP,
|
2019-12-09 11:12:41 +00:00
|
|
|
ATTR_HS_COLOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
2019-12-02 05:00:22 +00:00
|
|
|
SUPPORT_COLOR_TEMP,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-02 18:25:58 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_MEDIA_VOLUME_LEVEL,
|
|
|
|
SUPPORT_VOLUME_SET,
|
|
|
|
)
|
2019-04-02 18:25:58 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_SUPPORTED_FEATURES,
|
|
|
|
ATTR_TEMPERATURE,
|
|
|
|
HTTP_BAD_REQUEST,
|
|
|
|
HTTP_NOT_FOUND,
|
2019-12-09 11:12:41 +00:00
|
|
|
HTTP_UNAUTHORIZED,
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_CLOSE_COVER,
|
|
|
|
SERVICE_OPEN_COVER,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
SERVICE_VOLUME_SET,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
2019-11-25 11:17:08 +00:00
|
|
|
STATE_UNAVAILABLE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-08-04 01:30:16 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change_event
|
2018-10-08 18:50:24 +00:00
|
|
|
from homeassistant.util.network import is_local
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-08-04 01:30:16 +00:00
|
|
|
# How long to wait for a state change to happen
|
|
|
|
STATE_CHANGE_WAIT_TIMEOUT = 5.0
|
|
|
|
# How long an entry state's cache will be valid for in seconds.
|
|
|
|
STATE_CACHED_TIMEOUT = 2.0
|
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
STATE_BRIGHTNESS = "bri"
|
|
|
|
STATE_COLORMODE = "colormode"
|
|
|
|
STATE_HUE = "hue"
|
|
|
|
STATE_SATURATION = "sat"
|
|
|
|
STATE_COLOR_TEMP = "ct"
|
|
|
|
|
|
|
|
# Hue API states, defined separately in case they change
|
2019-07-31 19:25:30 +00:00
|
|
|
HUE_API_STATE_ON = "on"
|
|
|
|
HUE_API_STATE_BRI = "bri"
|
2019-12-02 05:00:22 +00:00
|
|
|
HUE_API_STATE_COLORMODE = "colormode"
|
2019-07-31 19:25:30 +00:00
|
|
|
HUE_API_STATE_HUE = "hue"
|
|
|
|
HUE_API_STATE_SAT = "sat"
|
2019-12-02 05:00:22 +00:00
|
|
|
HUE_API_STATE_CT = "ct"
|
|
|
|
HUE_API_STATE_EFFECT = "effect"
|
2019-04-02 18:25:58 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# Hue API min/max values - https://developers.meethue.com/develop/hue-api/lights-api/
|
|
|
|
HUE_API_STATE_BRI_MIN = 1 # Brightness
|
|
|
|
HUE_API_STATE_BRI_MAX = 254
|
|
|
|
HUE_API_STATE_HUE_MIN = 0 # Hue
|
|
|
|
HUE_API_STATE_HUE_MAX = 65535
|
|
|
|
HUE_API_STATE_SAT_MIN = 0 # Saturation
|
|
|
|
HUE_API_STATE_SAT_MAX = 254
|
|
|
|
HUE_API_STATE_CT_MIN = 153 # Color temp
|
|
|
|
HUE_API_STATE_CT_MAX = 500
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2020-05-04 05:18:33 +00:00
|
|
|
HUE_API_USERNAME = "nouser"
|
2019-12-05 23:23:54 +00:00
|
|
|
UNAUTHORIZED_USER = [
|
|
|
|
{"error": {"address": "/", "description": "unauthorized user", "type": "1"}}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class HueUnauthorizedUser(HomeAssistantView):
|
|
|
|
"""Handle requests to find the emulated hue bridge."""
|
|
|
|
|
|
|
|
url = "/api"
|
|
|
|
name = "emulated_hue:api:unauthorized_user"
|
|
|
|
extra_urls = ["/api/"]
|
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
async def get(self, request):
|
|
|
|
"""Handle a GET request."""
|
|
|
|
return self.json(UNAUTHORIZED_USER)
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
class HueUsernameView(HomeAssistantView):
|
|
|
|
"""Handle requests to create a username for the emulated hue bridge."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api"
|
|
|
|
name = "emulated_hue:api:create_username"
|
|
|
|
extra_urls = ["/api/"]
|
2016-12-04 18:57:48 +00:00
|
|
|
requires_auth = False
|
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def post(self, request):
|
2016-12-04 18:57:48 +00:00
|
|
|
"""Handle a POST request."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Only local IPs allowed", HTTP_UNAUTHORIZED)
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
try:
|
2018-10-01 06:52:42 +00:00
|
|
|
data = await request.json()
|
2016-12-04 18:57:48 +00:00
|
|
|
except ValueError:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("Invalid JSON", HTTP_BAD_REQUEST)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if "devicetype" not in data:
|
|
|
|
return self.json_message("devicetype not specified", HTTP_BAD_REQUEST)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-05 23:23:54 +00:00
|
|
|
return self.json([{"success": {"username": HUE_API_USERNAME}}])
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
|
2019-01-29 17:26:07 +00:00
|
|
|
class HueAllGroupsStateView(HomeAssistantView):
|
2019-12-02 05:00:22 +00:00
|
|
|
"""Handle requests for getting info about entity groups."""
|
2019-01-29 17:26:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/{username}/groups"
|
|
|
|
name = "emulated_hue:all_groups:state"
|
2019-01-29 17:26:07 +00:00
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@core.callback
|
|
|
|
def get(self, request, username):
|
|
|
|
"""Process a request to make the Brilliant Lightpad work."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Only local IPs allowed", HTTP_UNAUTHORIZED)
|
2019-01-29 17:26:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json({})
|
2019-01-29 17:26:07 +00:00
|
|
|
|
|
|
|
|
2018-04-28 18:39:21 +00:00
|
|
|
class HueGroupView(HomeAssistantView):
|
|
|
|
"""Group handler to get Logitech Pop working."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/{username}/groups/0/action"
|
|
|
|
name = "emulated_hue:groups:state"
|
2018-04-28 18:39:21 +00:00
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@core.callback
|
|
|
|
def put(self, request, username):
|
|
|
|
"""Process a request to make the Logitech Pop working."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Only local IPs allowed", HTTP_UNAUTHORIZED)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
return self.json(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"error": {
|
|
|
|
"address": "/groups/0/action/scene",
|
|
|
|
"type": 7,
|
|
|
|
"description": "invalid value, dummy for parameter, scene",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2018-04-28 18:39:21 +00:00
|
|
|
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
class HueAllLightsStateView(HomeAssistantView):
|
2019-12-02 05:00:22 +00:00
|
|
|
"""Handle requests for getting info about all entities."""
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/{username}/lights"
|
|
|
|
name = "emulated_hue:lights:state"
|
2016-12-04 18:57:48 +00:00
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@core.callback
|
|
|
|
def get(self, request, username):
|
|
|
|
"""Process a request to get the list of available lights."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Only local IPs allowed", HTTP_UNAUTHORIZED)
|
2018-10-08 18:50:24 +00:00
|
|
|
|
2019-12-05 23:23:54 +00:00
|
|
|
return self.json(create_list_of_entities(self.config, request))
|
|
|
|
|
|
|
|
|
|
|
|
class HueFullStateView(HomeAssistantView):
|
|
|
|
"""Return full state view of emulated hue."""
|
|
|
|
|
|
|
|
url = "/api/{username}"
|
|
|
|
name = "emulated_hue:username:state"
|
|
|
|
requires_auth = False
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-05 23:23:54 +00:00
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@core.callback
|
|
|
|
def get(self, request, username):
|
|
|
|
"""Process a request to get the list of available lights."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-05 23:23:54 +00:00
|
|
|
return self.json_message("only local IPs allowed", HTTP_UNAUTHORIZED)
|
|
|
|
if username != HUE_API_USERNAME:
|
|
|
|
return self.json(UNAUTHORIZED_USER)
|
|
|
|
|
|
|
|
json_response = {
|
|
|
|
"lights": create_list_of_entities(self.config, request),
|
2020-06-10 13:15:32 +00:00
|
|
|
"config": create_config_model(self.config, request),
|
2019-12-05 23:23:54 +00:00
|
|
|
}
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
return self.json(json_response)
|
|
|
|
|
|
|
|
|
2020-05-04 05:18:33 +00:00
|
|
|
class HueConfigView(HomeAssistantView):
|
|
|
|
"""Return config view of emulated hue."""
|
|
|
|
|
|
|
|
url = "/api/{username}/config"
|
2020-08-18 19:02:38 +00:00
|
|
|
extra_urls = ["/api/config"]
|
2020-05-04 05:18:33 +00:00
|
|
|
name = "emulated_hue:username:config"
|
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@core.callback
|
2020-08-18 19:02:38 +00:00
|
|
|
def get(self, request, username=""):
|
2020-05-04 05:18:33 +00:00
|
|
|
"""Process a request to get the configuration."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2020-05-04 05:18:33 +00:00
|
|
|
return self.json_message("only local IPs allowed", HTTP_UNAUTHORIZED)
|
|
|
|
|
2020-06-10 13:15:32 +00:00
|
|
|
json_response = create_config_model(self.config, request)
|
2020-05-04 05:18:33 +00:00
|
|
|
|
|
|
|
return self.json(json_response)
|
|
|
|
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
class HueOneLightStateView(HomeAssistantView):
|
2019-12-02 05:00:22 +00:00
|
|
|
"""Handle requests for getting info about a single entity."""
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/{username}/lights/{entity_id}"
|
|
|
|
name = "emulated_hue:light:state"
|
2016-12-04 18:57:48 +00:00
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
@core.callback
|
2017-01-11 20:25:02 +00:00
|
|
|
def get(self, request, username, entity_id):
|
2016-12-04 18:57:48 +00:00
|
|
|
"""Process a request to get the state of an individual light."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Only local IPs allowed", HTTP_UNAUTHORIZED)
|
2018-10-08 18:50:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2019-09-04 00:33:48 +00:00
|
|
|
hass_entity_id = self.config.number_to_entity_id(entity_id)
|
|
|
|
|
|
|
|
if hass_entity_id is None:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Unknown entity number: %s not found in emulated_hue_ids.json",
|
|
|
|
entity_id,
|
|
|
|
)
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Entity not found", HTTP_NOT_FOUND)
|
2019-09-04 00:33:48 +00:00
|
|
|
|
|
|
|
entity = hass.states.get(hass_entity_id)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
if entity is None:
|
2019-09-04 00:33:48 +00:00
|
|
|
_LOGGER.error("Entity not found: %s", hass_entity_id)
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Entity not found", HTTP_NOT_FOUND)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
if not self.config.is_entity_exposed(entity):
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Entity not exposed: %s", entity_id)
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Entity not exposed", HTTP_UNAUTHORIZED)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
json_response = entity_to_json(self.config, entity)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
return self.json(json_response)
|
|
|
|
|
|
|
|
|
|
|
|
class HueOneLightChangeView(HomeAssistantView):
|
2019-12-02 05:00:22 +00:00
|
|
|
"""Handle requests for setting info about entities."""
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/{username}/lights/{entity_number}/state"
|
|
|
|
name = "emulated_hue:light:state"
|
2016-12-04 18:57:48 +00:00
|
|
|
requires_auth = False
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the instance of the view."""
|
|
|
|
self.config = config
|
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def put(self, request, username, entity_number):
|
2016-12-04 18:57:48 +00:00
|
|
|
"""Process a request to set the state of an individual light."""
|
2020-08-11 20:57:50 +00:00
|
|
|
if not is_local(ip_address(request.remote)):
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Only local IPs allowed", HTTP_UNAUTHORIZED)
|
2018-10-08 18:50:24 +00:00
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
config = self.config
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2016-12-04 18:57:48 +00:00
|
|
|
entity_id = config.number_to_entity_id(entity_number)
|
|
|
|
|
|
|
|
if entity_id is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Unknown entity number: %s", entity_number)
|
|
|
|
return self.json_message("Entity not found", HTTP_NOT_FOUND)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
entity = hass.states.get(entity_id)
|
|
|
|
|
|
|
|
if entity is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Entity not found: %s", entity_id)
|
|
|
|
return self.json_message("Entity not found", HTTP_NOT_FOUND)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
if not config.is_entity_exposed(entity):
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Entity not exposed: %s", entity_id)
|
2019-12-02 05:00:22 +00:00
|
|
|
return self.json_message("Entity not exposed", HTTP_UNAUTHORIZED)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
try:
|
2018-10-01 06:52:42 +00:00
|
|
|
request_json = await request.json()
|
2016-12-04 18:57:48 +00:00
|
|
|
except ValueError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Received invalid json")
|
|
|
|
return self.json_message("Invalid JSON", HTTP_BAD_REQUEST)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# Get the entity's supported features
|
|
|
|
entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
|
|
|
|
|
|
|
|
# Parse the request
|
|
|
|
parsed = {
|
|
|
|
STATE_ON: False,
|
|
|
|
STATE_BRIGHTNESS: None,
|
|
|
|
STATE_HUE: None,
|
|
|
|
STATE_SATURATION: None,
|
|
|
|
STATE_COLOR_TEMP: None,
|
|
|
|
}
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
if HUE_API_STATE_ON in request_json:
|
|
|
|
if not isinstance(request_json[HUE_API_STATE_ON], bool):
|
|
|
|
_LOGGER.error("Unable to parse data: %s", request_json)
|
|
|
|
return self.json_message("Bad request", HTTP_BAD_REQUEST)
|
|
|
|
parsed[STATE_ON] = request_json[HUE_API_STATE_ON]
|
|
|
|
else:
|
|
|
|
parsed[STATE_ON] = entity.state != STATE_OFF
|
|
|
|
|
|
|
|
for (key, attr) in (
|
|
|
|
(HUE_API_STATE_BRI, STATE_BRIGHTNESS),
|
|
|
|
(HUE_API_STATE_HUE, STATE_HUE),
|
|
|
|
(HUE_API_STATE_SAT, STATE_SATURATION),
|
|
|
|
(HUE_API_STATE_CT, STATE_COLOR_TEMP),
|
|
|
|
):
|
|
|
|
if key in request_json:
|
|
|
|
try:
|
|
|
|
parsed[attr] = int(request_json[key])
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.error("Unable to parse data (2): %s", request_json)
|
|
|
|
return self.json_message("Bad request", HTTP_BAD_REQUEST)
|
|
|
|
|
|
|
|
if HUE_API_STATE_BRI in request_json:
|
|
|
|
if entity.domain == light.DOMAIN:
|
2020-01-29 11:11:22 +00:00
|
|
|
if entity_features & SUPPORT_BRIGHTNESS:
|
|
|
|
parsed[STATE_ON] = parsed[STATE_BRIGHTNESS] > 0
|
|
|
|
else:
|
2019-12-02 05:00:22 +00:00
|
|
|
parsed[STATE_BRIGHTNESS] = None
|
|
|
|
|
|
|
|
elif entity.domain == scene.DOMAIN:
|
|
|
|
parsed[STATE_BRIGHTNESS] = None
|
|
|
|
parsed[STATE_ON] = True
|
|
|
|
|
|
|
|
elif entity.domain in [
|
|
|
|
script.DOMAIN,
|
|
|
|
media_player.DOMAIN,
|
|
|
|
fan.DOMAIN,
|
|
|
|
cover.DOMAIN,
|
|
|
|
climate.DOMAIN,
|
2020-07-04 22:53:36 +00:00
|
|
|
humidifier.DOMAIN,
|
2019-12-02 05:00:22 +00:00
|
|
|
]:
|
2020-04-17 16:59:27 +00:00
|
|
|
# Convert 0-254 to 0-100
|
2019-12-02 05:00:22 +00:00
|
|
|
level = (parsed[STATE_BRIGHTNESS] / HUE_API_STATE_BRI_MAX) * 100
|
|
|
|
parsed[STATE_BRIGHTNESS] = round(level)
|
|
|
|
parsed[STATE_ON] = True
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2016-12-16 05:47:23 +00:00
|
|
|
# Choose general HA domain
|
|
|
|
domain = core.DOMAIN
|
|
|
|
|
2016-12-26 13:00:43 +00:00
|
|
|
# Entity needs separate call to turn on
|
|
|
|
turn_on_needed = False
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
# Convert the resulting "on" status into the service we need to call
|
2019-04-02 18:25:58 +00:00
|
|
|
service = SERVICE_TURN_ON if parsed[STATE_ON] else SERVICE_TURN_OFF
|
2016-12-04 18:57:48 +00:00
|
|
|
|
|
|
|
# Construct what we need to send to the service
|
|
|
|
data = {ATTR_ENTITY_ID: entity_id}
|
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# If the requested entity is a light, set the brightness, hue,
|
|
|
|
# saturation and color temp
|
2019-02-11 18:59:34 +00:00
|
|
|
if entity.domain == light.DOMAIN:
|
2019-04-02 18:25:58 +00:00
|
|
|
if parsed[STATE_ON]:
|
|
|
|
if entity_features & SUPPORT_BRIGHTNESS:
|
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
2020-04-17 16:59:27 +00:00
|
|
|
data[ATTR_BRIGHTNESS] = hue_brightness_to_hass(
|
|
|
|
parsed[STATE_BRIGHTNESS]
|
|
|
|
)
|
2019-12-02 05:00:22 +00:00
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
if entity_features & SUPPORT_COLOR:
|
2019-12-02 05:00:22 +00:00
|
|
|
if any((parsed[STATE_HUE], parsed[STATE_SATURATION])):
|
|
|
|
if parsed[STATE_HUE] is not None:
|
|
|
|
hue = parsed[STATE_HUE]
|
|
|
|
else:
|
|
|
|
hue = 0
|
|
|
|
|
|
|
|
if parsed[STATE_SATURATION] is not None:
|
2019-04-02 18:25:58 +00:00
|
|
|
sat = parsed[STATE_SATURATION]
|
|
|
|
else:
|
|
|
|
sat = 0
|
|
|
|
|
|
|
|
# Convert hs values to hass hs values
|
|
|
|
hue = int((hue / HUE_API_STATE_HUE_MAX) * 360)
|
2019-12-02 05:00:22 +00:00
|
|
|
sat = int((sat / HUE_API_STATE_SAT_MAX) * 100)
|
2019-04-02 18:25:58 +00:00
|
|
|
|
|
|
|
data[ATTR_HS_COLOR] = (hue, sat)
|
2016-12-16 05:47:23 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
if entity_features & SUPPORT_COLOR_TEMP:
|
|
|
|
if parsed[STATE_COLOR_TEMP] is not None:
|
|
|
|
data[ATTR_COLOR_TEMP] = parsed[STATE_COLOR_TEMP]
|
|
|
|
|
|
|
|
# If the requested entity is a script, add some variables
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == script.DOMAIN:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["variables"] = {
|
|
|
|
"requested_state": STATE_ON if parsed[STATE_ON] else STATE_OFF
|
2016-12-04 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["variables"]["requested_level"] = parsed[STATE_BRIGHTNESS]
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-03-10 23:04:21 +00:00
|
|
|
# If the requested entity is a climate, set the temperature
|
|
|
|
elif entity.domain == climate.DOMAIN:
|
|
|
|
# We don't support turning climate devices on or off,
|
|
|
|
# only setting the temperature
|
|
|
|
service = None
|
|
|
|
|
|
|
|
if entity_features & SUPPORT_TARGET_TEMPERATURE:
|
2019-04-02 18:25:58 +00:00
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
2019-03-10 23:04:21 +00:00
|
|
|
domain = entity.domain
|
|
|
|
service = SERVICE_SET_TEMPERATURE
|
2019-04-02 18:25:58 +00:00
|
|
|
data[ATTR_TEMPERATURE] = parsed[STATE_BRIGHTNESS]
|
2019-03-10 23:04:21 +00:00
|
|
|
|
2020-07-04 22:53:36 +00:00
|
|
|
# If the requested entity is a humidifier, set the humidity
|
|
|
|
elif entity.domain == humidifier.DOMAIN:
|
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
|
|
|
turn_on_needed = True
|
|
|
|
domain = entity.domain
|
|
|
|
service = SERVICE_SET_HUMIDITY
|
|
|
|
data[ATTR_HUMIDITY] = parsed[STATE_BRIGHTNESS]
|
|
|
|
|
2016-12-16 05:47:23 +00:00
|
|
|
# If the requested entity is a media player, convert to volume
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == media_player.DOMAIN:
|
2017-02-08 04:42:45 +00:00
|
|
|
if entity_features & SUPPORT_VOLUME_SET:
|
2019-04-02 18:25:58 +00:00
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
2016-12-26 13:00:43 +00:00
|
|
|
turn_on_needed = True
|
2016-12-16 05:47:23 +00:00
|
|
|
domain = entity.domain
|
|
|
|
service = SERVICE_VOLUME_SET
|
|
|
|
# Convert 0-100 to 0.0-1.0
|
2019-07-31 19:25:30 +00:00
|
|
|
data[ATTR_MEDIA_VOLUME_LEVEL] = parsed[STATE_BRIGHTNESS] / 100.0
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2016-12-26 13:00:43 +00:00
|
|
|
# If the requested entity is a cover, convert to open_cover/close_cover
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == cover.DOMAIN:
|
2016-12-26 13:00:43 +00:00
|
|
|
domain = entity.domain
|
|
|
|
if service == SERVICE_TURN_ON:
|
|
|
|
service = SERVICE_OPEN_COVER
|
|
|
|
else:
|
|
|
|
service = SERVICE_CLOSE_COVER
|
|
|
|
|
2019-02-11 18:59:34 +00:00
|
|
|
if entity_features & SUPPORT_SET_POSITION:
|
2019-04-02 18:25:58 +00:00
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
2019-02-11 18:59:34 +00:00
|
|
|
domain = entity.domain
|
|
|
|
service = SERVICE_SET_COVER_POSITION
|
2019-04-02 18:25:58 +00:00
|
|
|
data[ATTR_POSITION] = parsed[STATE_BRIGHTNESS]
|
2019-02-11 18:59:34 +00:00
|
|
|
|
2017-01-21 06:21:28 +00:00
|
|
|
# If the requested entity is a fan, convert to speed
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == fan.DOMAIN:
|
2017-02-08 04:42:45 +00:00
|
|
|
if entity_features & SUPPORT_SET_SPEED:
|
2019-04-02 18:25:58 +00:00
|
|
|
if parsed[STATE_BRIGHTNESS] is not None:
|
2017-01-21 06:21:28 +00:00
|
|
|
domain = entity.domain
|
|
|
|
# Convert 0-100 to a fan speed
|
2019-04-02 18:25:58 +00:00
|
|
|
brightness = parsed[STATE_BRIGHTNESS]
|
2017-01-21 06:21:28 +00:00
|
|
|
if brightness == 0:
|
|
|
|
data[ATTR_SPEED] = SPEED_OFF
|
2018-07-18 09:54:27 +00:00
|
|
|
elif 0 < brightness <= 33.3:
|
2017-01-21 06:21:28 +00:00
|
|
|
data[ATTR_SPEED] = SPEED_LOW
|
2018-07-18 09:54:27 +00:00
|
|
|
elif 33.3 < brightness <= 66.6:
|
2017-01-21 06:21:28 +00:00
|
|
|
data[ATTR_SPEED] = SPEED_MEDIUM
|
2018-07-18 09:54:27 +00:00
|
|
|
elif 66.6 < brightness <= 100:
|
2017-01-21 06:21:28 +00:00
|
|
|
data[ATTR_SPEED] = SPEED_HIGH
|
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# Map the off command to on
|
2016-12-04 18:57:48 +00:00
|
|
|
if entity.domain in config.off_maps_to_on_domains:
|
|
|
|
service = SERVICE_TURN_ON
|
|
|
|
|
2016-12-16 05:47:23 +00:00
|
|
|
# Separate call to turn on needed
|
2016-12-26 13:00:43 +00:00
|
|
|
if turn_on_needed:
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.services.async_call(
|
|
|
|
core.DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
)
|
2016-12-16 05:47:23 +00:00
|
|
|
|
2019-03-10 23:04:21 +00:00
|
|
|
if service is not None:
|
2020-08-04 01:30:16 +00:00
|
|
|
state_will_change = parsed[STATE_ON] != (entity.state != STATE_OFF)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.services.async_call(domain, service, data, blocking=True)
|
|
|
|
)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2020-08-04 01:30:16 +00:00
|
|
|
if state_will_change:
|
|
|
|
# Wait for the state to change.
|
|
|
|
await wait_for_state_change_or_timeout(
|
|
|
|
hass, entity_id, STATE_CACHED_TIMEOUT
|
|
|
|
)
|
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# Create success responses for all received keys
|
2019-07-31 19:25:30 +00:00
|
|
|
json_response = [
|
2020-05-04 00:27:19 +00:00
|
|
|
create_hue_success_response(
|
|
|
|
entity_number, HUE_API_STATE_ON, parsed[STATE_ON]
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
for (key, val) in (
|
|
|
|
(STATE_BRIGHTNESS, HUE_API_STATE_BRI),
|
|
|
|
(STATE_HUE, HUE_API_STATE_HUE),
|
|
|
|
(STATE_SATURATION, HUE_API_STATE_SAT),
|
|
|
|
(STATE_COLOR_TEMP, HUE_API_STATE_CT),
|
|
|
|
):
|
|
|
|
if parsed[key] is not None:
|
|
|
|
json_response.append(
|
2020-05-04 00:27:19 +00:00
|
|
|
create_hue_success_response(entity_number, val, parsed[key])
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2020-08-04 01:30:16 +00:00
|
|
|
if entity.domain in config.off_maps_to_on_domains:
|
|
|
|
# Caching is required because things like scripts and scenes won't
|
|
|
|
# report as "off" to Alexa if an "off" command is received, because
|
|
|
|
# they'll map to "on". Thus, instead of reporting its actual
|
|
|
|
# status, we report what Alexa will want to see, which is the same
|
|
|
|
# as the actual requested command.
|
|
|
|
config.cached_states[entity_id] = [parsed, None]
|
|
|
|
else:
|
|
|
|
config.cached_states[entity_id] = [parsed, time.time()]
|
2020-06-10 13:15:32 +00:00
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
return self.json(json_response)
|
|
|
|
|
|
|
|
|
2016-12-16 05:47:23 +00:00
|
|
|
def get_entity_state(config, entity):
|
|
|
|
"""Retrieve and convert state and brightness values for an entity."""
|
2020-08-04 01:30:16 +00:00
|
|
|
cached_state_entry = config.cached_states.get(entity.entity_id, None)
|
|
|
|
cached_state = None
|
|
|
|
|
|
|
|
# Check if we have a cached entry, and if so if it hasn't expired.
|
|
|
|
if cached_state_entry is not None:
|
|
|
|
entry_state, entry_time = cached_state_entry
|
|
|
|
if entry_time is None:
|
|
|
|
# Handle the case where the entity is listed in config.off_maps_to_on_domains.
|
|
|
|
cached_state = entry_state
|
|
|
|
elif time.time() - entry_time < STATE_CACHED_TIMEOUT and entry_state[
|
|
|
|
STATE_ON
|
|
|
|
] == (entity.state != STATE_OFF):
|
|
|
|
# We only want to use the cache if the actual state of the entity
|
|
|
|
# is in sync so that it can be detected as an error by Alexa.
|
|
|
|
cached_state = entry_state
|
|
|
|
else:
|
|
|
|
# Remove the now stale cached entry.
|
|
|
|
config.cached_states.pop(entity.entity_id)
|
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
data = {
|
2019-12-02 05:00:22 +00:00
|
|
|
STATE_ON: False,
|
2019-04-02 18:25:58 +00:00
|
|
|
STATE_BRIGHTNESS: None,
|
|
|
|
STATE_HUE: None,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_SATURATION: None,
|
2019-12-02 05:00:22 +00:00
|
|
|
STATE_COLOR_TEMP: None,
|
2019-04-02 18:25:58 +00:00
|
|
|
}
|
2016-12-16 05:47:23 +00:00
|
|
|
|
|
|
|
if cached_state is None:
|
2019-04-02 18:25:58 +00:00
|
|
|
data[STATE_ON] = entity.state != STATE_OFF
|
2019-12-02 05:00:22 +00:00
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
if data[STATE_ON]:
|
2020-04-17 16:59:27 +00:00
|
|
|
data[STATE_BRIGHTNESS] = hass_to_hue_brightness(
|
|
|
|
entity.attributes.get(ATTR_BRIGHTNESS, 0)
|
|
|
|
)
|
2020-04-07 19:06:05 +00:00
|
|
|
hue_sat = entity.attributes.get(ATTR_HS_COLOR)
|
2019-04-02 18:25:58 +00:00
|
|
|
if hue_sat is not None:
|
|
|
|
hue = hue_sat[0]
|
|
|
|
sat = hue_sat[1]
|
2019-12-02 05:00:22 +00:00
|
|
|
# Convert hass hs values back to hue hs values
|
2019-04-02 18:25:58 +00:00
|
|
|
data[STATE_HUE] = int((hue / 360.0) * HUE_API_STATE_HUE_MAX)
|
2019-07-31 19:25:30 +00:00
|
|
|
data[STATE_SATURATION] = int((sat / 100.0) * HUE_API_STATE_SAT_MAX)
|
2019-12-02 05:00:22 +00:00
|
|
|
else:
|
|
|
|
data[STATE_HUE] = HUE_API_STATE_HUE_MIN
|
|
|
|
data[STATE_SATURATION] = HUE_API_STATE_SAT_MIN
|
|
|
|
data[STATE_COLOR_TEMP] = entity.attributes.get(ATTR_COLOR_TEMP, 0)
|
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
else:
|
|
|
|
data[STATE_BRIGHTNESS] = 0
|
|
|
|
data[STATE_HUE] = 0
|
|
|
|
data[STATE_SATURATION] = 0
|
2019-12-02 05:00:22 +00:00
|
|
|
data[STATE_COLOR_TEMP] = 0
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# Get the entity's supported features
|
2016-12-16 05:47:23 +00:00
|
|
|
entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-02-11 18:59:34 +00:00
|
|
|
if entity.domain == light.DOMAIN:
|
2017-02-08 04:42:45 +00:00
|
|
|
if entity_features & SUPPORT_BRIGHTNESS:
|
|
|
|
pass
|
2019-03-10 23:04:21 +00:00
|
|
|
elif entity.domain == climate.DOMAIN:
|
|
|
|
temperature = entity.attributes.get(ATTR_TEMPERATURE, 0)
|
2020-04-17 16:59:27 +00:00
|
|
|
# Convert 0-100 to 0-254
|
|
|
|
data[STATE_BRIGHTNESS] = round(temperature * HUE_API_STATE_BRI_MAX / 100)
|
2020-07-04 22:53:36 +00:00
|
|
|
elif entity.domain == humidifier.DOMAIN:
|
|
|
|
humidity = entity.attributes.get(ATTR_HUMIDITY, 0)
|
|
|
|
# Convert 0-100 to 0-254
|
|
|
|
data[STATE_BRIGHTNESS] = round(humidity * HUE_API_STATE_BRI_MAX / 100)
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == media_player.DOMAIN:
|
2016-12-16 05:47:23 +00:00
|
|
|
level = entity.attributes.get(
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_MEDIA_VOLUME_LEVEL, 1.0 if data[STATE_ON] else 0.0
|
|
|
|
)
|
2020-04-17 16:59:27 +00:00
|
|
|
# Convert 0.0-1.0 to 0-254
|
|
|
|
data[STATE_BRIGHTNESS] = round(min(1.0, level) * HUE_API_STATE_BRI_MAX)
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == fan.DOMAIN:
|
2017-01-21 06:21:28 +00:00
|
|
|
speed = entity.attributes.get(ATTR_SPEED, 0)
|
2020-04-17 16:59:27 +00:00
|
|
|
# Convert 0.0-1.0 to 0-254
|
2019-04-02 18:25:58 +00:00
|
|
|
data[STATE_BRIGHTNESS] = 0
|
2017-01-21 06:21:28 +00:00
|
|
|
if speed == SPEED_LOW:
|
2019-04-02 18:25:58 +00:00
|
|
|
data[STATE_BRIGHTNESS] = 85
|
2017-01-21 06:21:28 +00:00
|
|
|
elif speed == SPEED_MEDIUM:
|
2019-04-02 18:25:58 +00:00
|
|
|
data[STATE_BRIGHTNESS] = 170
|
2017-01-21 06:21:28 +00:00
|
|
|
elif speed == SPEED_HIGH:
|
2020-04-17 16:59:27 +00:00
|
|
|
data[STATE_BRIGHTNESS] = HUE_API_STATE_BRI_MAX
|
2019-02-11 18:59:34 +00:00
|
|
|
elif entity.domain == cover.DOMAIN:
|
|
|
|
level = entity.attributes.get(ATTR_CURRENT_POSITION, 0)
|
2020-04-17 16:59:27 +00:00
|
|
|
data[STATE_BRIGHTNESS] = round(level / 100 * HUE_API_STATE_BRI_MAX)
|
2016-12-16 05:47:23 +00:00
|
|
|
else:
|
2019-04-02 18:25:58 +00:00
|
|
|
data = cached_state
|
2016-12-26 12:58:32 +00:00
|
|
|
# Make sure brightness is valid
|
2019-04-02 18:25:58 +00:00
|
|
|
if data[STATE_BRIGHTNESS] is None:
|
2020-04-17 16:59:27 +00:00
|
|
|
data[STATE_BRIGHTNESS] = HUE_API_STATE_BRI_MAX if data[STATE_ON] else 0
|
2019-12-02 05:00:22 +00:00
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
# Make sure hue/saturation are valid
|
|
|
|
if (data[STATE_HUE] is None) or (data[STATE_SATURATION] is None):
|
|
|
|
data[STATE_HUE] = 0
|
|
|
|
data[STATE_SATURATION] = 0
|
|
|
|
|
|
|
|
# If the light is off, set the color to off
|
|
|
|
if data[STATE_BRIGHTNESS] == 0:
|
|
|
|
data[STATE_HUE] = 0
|
|
|
|
data[STATE_SATURATION] = 0
|
2016-12-16 05:47:23 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
# Clamp brightness, hue, saturation, and color temp to valid values
|
|
|
|
for (key, v_min, v_max) in (
|
|
|
|
(STATE_BRIGHTNESS, HUE_API_STATE_BRI_MIN, HUE_API_STATE_BRI_MAX),
|
|
|
|
(STATE_HUE, HUE_API_STATE_HUE_MIN, HUE_API_STATE_HUE_MAX),
|
|
|
|
(STATE_SATURATION, HUE_API_STATE_SAT_MIN, HUE_API_STATE_SAT_MAX),
|
|
|
|
(STATE_COLOR_TEMP, HUE_API_STATE_CT_MIN, HUE_API_STATE_CT_MAX),
|
|
|
|
):
|
|
|
|
if data[key] is not None:
|
|
|
|
data[key] = max(v_min, min(data[key], v_max))
|
|
|
|
|
2019-04-02 18:25:58 +00:00
|
|
|
return data
|
2016-12-16 05:47:23 +00:00
|
|
|
|
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
def entity_to_json(config, entity):
|
2016-12-16 05:47:23 +00:00
|
|
|
"""Convert an entity to its Hue bridge JSON representation."""
|
2019-08-21 15:42:26 +00:00
|
|
|
entity_features = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
|
2019-12-02 05:00:22 +00:00
|
|
|
unique_id = hashlib.md5(entity.entity_id.encode()).hexdigest()
|
2020-02-24 16:47:52 +00:00
|
|
|
unique_id = f"00:{unique_id[0:2]}:{unique_id[2:4]}:{unique_id[4:6]}:{unique_id[6:8]}:{unique_id[8:10]}:{unique_id[10:12]}:{unique_id[12:14]}-{unique_id[14:16]}"
|
2019-12-02 05:00:22 +00:00
|
|
|
|
|
|
|
state = get_entity_state(config, entity)
|
|
|
|
|
|
|
|
retval = {
|
2019-11-25 11:17:08 +00:00
|
|
|
"state": {
|
|
|
|
HUE_API_STATE_ON: state[STATE_ON],
|
|
|
|
"reachable": entity.state != STATE_UNAVAILABLE,
|
2019-12-02 05:00:22 +00:00
|
|
|
"mode": "homeautomation",
|
2019-11-25 11:17:08 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"name": config.get_entity_name(entity),
|
2019-12-02 05:00:22 +00:00
|
|
|
"uniqueid": unique_id,
|
|
|
|
"manufacturername": "Home Assistant",
|
2019-07-31 19:25:30 +00:00
|
|
|
"swversion": "123",
|
|
|
|
}
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2019-12-02 05:00:22 +00:00
|
|
|
if (
|
|
|
|
(entity_features & SUPPORT_BRIGHTNESS)
|
|
|
|
and (entity_features & SUPPORT_COLOR)
|
|
|
|
and (entity_features & SUPPORT_COLOR_TEMP)
|
|
|
|
):
|
2020-01-06 13:32:10 +00:00
|
|
|
# Extended Color light (Zigbee Device ID: 0x0210)
|
2019-12-02 05:00:22 +00:00
|
|
|
# Same as Color light, but which supports additional setting of color temperature
|
|
|
|
retval["type"] = "Extended color light"
|
|
|
|
retval["modelid"] = "HASS231"
|
|
|
|
retval["state"].update(
|
|
|
|
{
|
|
|
|
HUE_API_STATE_BRI: state[STATE_BRIGHTNESS],
|
|
|
|
HUE_API_STATE_HUE: state[STATE_HUE],
|
|
|
|
HUE_API_STATE_SAT: state[STATE_SATURATION],
|
|
|
|
HUE_API_STATE_CT: state[STATE_COLOR_TEMP],
|
|
|
|
HUE_API_STATE_EFFECT: "none",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if state[STATE_HUE] > 0 or state[STATE_SATURATION] > 0:
|
|
|
|
retval["state"][HUE_API_STATE_COLORMODE] = "hs"
|
|
|
|
else:
|
|
|
|
retval["state"][HUE_API_STATE_COLORMODE] = "ct"
|
|
|
|
elif (entity_features & SUPPORT_BRIGHTNESS) and (entity_features & SUPPORT_COLOR):
|
2020-01-06 13:32:10 +00:00
|
|
|
# Color light (Zigbee Device ID: 0x0200)
|
2019-12-02 05:00:22 +00:00
|
|
|
# Supports on/off, dimming and color control (hue/saturation, enhanced hue, color loop and XY)
|
|
|
|
retval["type"] = "Color light"
|
|
|
|
retval["modelid"] = "HASS213"
|
|
|
|
retval["state"].update(
|
|
|
|
{
|
|
|
|
HUE_API_STATE_BRI: state[STATE_BRIGHTNESS],
|
|
|
|
HUE_API_STATE_COLORMODE: "hs",
|
|
|
|
HUE_API_STATE_HUE: state[STATE_HUE],
|
|
|
|
HUE_API_STATE_SAT: state[STATE_SATURATION],
|
|
|
|
HUE_API_STATE_EFFECT: "none",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
elif (entity_features & SUPPORT_BRIGHTNESS) and (
|
|
|
|
entity_features & SUPPORT_COLOR_TEMP
|
|
|
|
):
|
2020-01-06 13:32:10 +00:00
|
|
|
# Color temperature light (Zigbee Device ID: 0x0220)
|
2019-12-02 05:00:22 +00:00
|
|
|
# Supports groups, scenes, on/off, dimming, and setting of a color temperature
|
|
|
|
retval["type"] = "Color temperature light"
|
|
|
|
retval["modelid"] = "HASS312"
|
|
|
|
retval["state"].update(
|
2020-03-13 22:42:47 +00:00
|
|
|
{
|
|
|
|
HUE_API_STATE_COLORMODE: "ct",
|
|
|
|
HUE_API_STATE_CT: state[STATE_COLOR_TEMP],
|
|
|
|
HUE_API_STATE_BRI: state[STATE_BRIGHTNESS],
|
|
|
|
}
|
2019-12-02 05:00:22 +00:00
|
|
|
)
|
2020-02-02 22:52:00 +00:00
|
|
|
elif entity_features & (
|
|
|
|
SUPPORT_BRIGHTNESS
|
|
|
|
| SUPPORT_SET_POSITION
|
|
|
|
| SUPPORT_SET_SPEED
|
|
|
|
| SUPPORT_VOLUME_SET
|
|
|
|
| SUPPORT_TARGET_TEMPERATURE
|
|
|
|
):
|
2020-01-06 13:32:10 +00:00
|
|
|
# Dimmable light (Zigbee Device ID: 0x0100)
|
2019-12-02 05:00:22 +00:00
|
|
|
# Supports groups, scenes, on/off and dimming
|
|
|
|
retval["type"] = "Dimmable light"
|
|
|
|
retval["modelid"] = "HASS123"
|
|
|
|
retval["state"].update({HUE_API_STATE_BRI: state[STATE_BRIGHTNESS]})
|
2020-09-01 14:16:40 +00:00
|
|
|
elif not config.lights_all_dimmable:
|
2020-07-20 08:33:56 +00:00
|
|
|
# On/Off light (ZigBee Device ID: 0x0000)
|
|
|
|
# Supports groups, scenes and on/off control
|
|
|
|
retval["type"] = "On/Off light"
|
|
|
|
retval["productname"] = "On/Off light"
|
|
|
|
retval["modelid"] = "HASS321"
|
2020-09-01 14:16:40 +00:00
|
|
|
else:
|
|
|
|
# Dimmable light (Zigbee Device ID: 0x0100)
|
|
|
|
# Supports groups, scenes, on/off and dimming
|
|
|
|
# Reports fixed brightness for compatibility with Alexa.
|
|
|
|
retval["type"] = "Dimmable light"
|
|
|
|
retval["modelid"] = "HASS123"
|
2020-08-20 08:27:14 +00:00
|
|
|
retval["state"].update({HUE_API_STATE_BRI: HUE_API_STATE_BRI_MAX})
|
2019-12-02 05:00:22 +00:00
|
|
|
|
|
|
|
return retval
|
|
|
|
|
2016-12-04 18:57:48 +00:00
|
|
|
|
2020-05-04 00:27:19 +00:00
|
|
|
def create_hue_success_response(entity_number, attr, value):
|
2016-12-04 18:57:48 +00:00
|
|
|
"""Create a success response for an attribute set on a light."""
|
2020-05-04 00:27:19 +00:00
|
|
|
success_key = f"/lights/{entity_number}/state/{attr}"
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"success": {success_key: value}}
|
2019-12-05 23:23:54 +00:00
|
|
|
|
|
|
|
|
2020-06-10 13:15:32 +00:00
|
|
|
def create_config_model(config, request):
|
|
|
|
"""Create a config resource."""
|
|
|
|
return {
|
|
|
|
"mac": "00:00:00:00:00:00",
|
|
|
|
"swversion": "01003542",
|
|
|
|
"apiversion": "1.17.0",
|
|
|
|
"whitelist": {HUE_API_USERNAME: {"name": "HASS BRIDGE"}},
|
|
|
|
"ipaddress": f"{config.advertise_ip}:{config.advertise_port}",
|
|
|
|
"linkbutton": True,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 23:23:54 +00:00
|
|
|
def create_list_of_entities(config, request):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Create a list of all entities."""
|
2019-12-05 23:23:54 +00:00
|
|
|
hass = request.app["hass"]
|
|
|
|
json_response = {}
|
|
|
|
|
2020-06-30 18:22:17 +00:00
|
|
|
for entity in config.filter_exposed_entities(hass.states.async_all()):
|
|
|
|
number = config.entity_id_to_number(entity.entity_id)
|
|
|
|
json_response[number] = entity_to_json(config, entity)
|
2019-12-05 23:23:54 +00:00
|
|
|
|
|
|
|
return json_response
|
2020-04-17 16:59:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def hue_brightness_to_hass(value):
|
|
|
|
"""Convert hue brightness 1..254 to hass format 0..255."""
|
|
|
|
return min(255, round((value / HUE_API_STATE_BRI_MAX) * 255))
|
|
|
|
|
|
|
|
|
|
|
|
def hass_to_hue_brightness(value):
|
|
|
|
"""Convert hass brightness 0..255 to hue 1..254 scale."""
|
|
|
|
return max(1, round((value / 255) * HUE_API_STATE_BRI_MAX))
|
2020-08-04 01:30:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def wait_for_state_change_or_timeout(hass, entity_id, timeout):
|
|
|
|
"""Wait for an entity to change state."""
|
|
|
|
ev = asyncio.Event()
|
|
|
|
|
|
|
|
@core.callback
|
|
|
|
def _async_event_changed(_):
|
|
|
|
ev.set()
|
|
|
|
|
|
|
|
unsub = async_track_state_change_event(hass, [entity_id], _async_event_changed)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await asyncio.wait_for(ev.wait(), timeout=STATE_CHANGE_WAIT_TIMEOUT)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
unsub()
|