Skip to content

Instantly share code, notes, and snippets.

@CarlBoneri
Last active April 1, 2021 16:36
Show Gist options
  • Save CarlBoneri/0209903da7131c484c5de9a0b2ce95a3 to your computer and use it in GitHub Desktop.
Save CarlBoneri/0209903da7131c484c5de9a0b2ce95a3 to your computer and use it in GitHub Desktop.
Access Phillips HUE from terminal... because why not
hue.env <- function(e_name = ".hue", get_var = c("api_url", "bridge_id", "username")){
if(!exists(e_name)){
assign(e_name, new.env(), envir = .GlobalEnv)
}else {
e <- get(e_name, envir = .GlobalEnv)
if(length(get_var) != 3){
e <- e[[match.arg(get_var)]]
}
return(e)
}
}
hue.get_bridge <- function(hue_env = .hue){
data <- fromJSON('https://discovery.meethue.com/')
if(!length(data)){
bridge_id <- NA
return("No bridge detected")
}else {
bridge_id <- data[['internalipaddress']]
}
assign("bridge_id", bridge_id, envir = hue.env())
assign("api_url", sprintf("http://%s/api", bridge_id), envir = hue.env())
# There's an api sandbox you can play with:
assign("sandbox", sprintf("http://%s/debug/clip.html", bridge_id), envir = hue.env())
}
# You need an application name from your dev account so go here:
# https://developers.meethue.com/develop/get-started-2/
#
#
# app_name is the name that your app will take on moving forward so don't
# worry if it doesnt exist....i think
# "my_hue_app#carl_note8"
#
hue.get_id <- function(app_name = NULL){
# Get my hue system id
#
# First attempt will fail, then press button, then get id
hue_id_json <- toJSON(list(devicetype = app_name), auto_unbox = T)
# Fails and then press link button on bridge
hue_id_call <- POST(hue.env(get_var = "api_url"), body = hue_id_json)
hue_id <- content(hue_id_call)
if(is.list(hue_id) & (names(hue_id[[1]]) == "error")){
redo <- NULL
# pause and wait for user to press the bridge button
redo <- readline("Go push the button on top of your bridge and then hit enter")
if(!is.null(redo)){
hue.get_id(app_name = app_name)
}
}else {
assign("username", unlist(hue_id, use.names = FALSE), envir = hue.env())
}
}
hue.api_url <- function(end_point = NULL){
hue_api <- stri_join_list(Map(hue.env, get_var = c("api_url","username")), collapse = "/")
if(!is.null(end_point)){
sprintf("%s/%s", hue_api, end_point)
}else {
hue_api
}
}
hue.list_groups <- function(end_point = NULL){
dat <- fromJSON(hue.api_url(end_point = end_point))
return(light_data)
}
hue.list_lights <- function(hue_env = .hue, with_groups = TRUE){
u <- hue.api_url(end_point = "lights")
dat <- fromJSON(u)
if("error" %in% names(dat)){
return("Something went wrong, please check your environment or something else....idk")
}else {
ids <- names(dat)
idx <- lapply(ids, function(i){
r <- dat[[i]]
d <- c(r["name"], r["state"])
data.frame(id = i, name = d[["name"]], unlist(d[["state"]]) %>% rbind() %>% as.data.frame())
}) %>% rbind_pages()
return(idx)
}
}
hue.get_state <- function(type = c("lights", "groups"), id = NULL, ...){
dat <- fromJSON(hue.api_url(end_point = sprintf("%s/%s", match.arg(type), id)))
dat[["state"]]
}
hue.put <- function(path = NULL, ...){
PUT(url = hue.api_url(path), body = toJSON(list(...), auto_unbox = TRUE))
}
hue.light_switch <- function(id = NULL, light_name = NULL, action = c("on", "off"), ...){
if(is.null(id)){
all_lights <- hue.list_lights()
if(!is.null(light_name)){
id <- all_lights[grep(light_name, all_lights[["name"]], ignore.case = TRUE), "id"]
}else {
id <- all_lights$id
}
}
end_point <- sprintf("lights/%s/state", id)
o <- Map(hue.put, path = end_point, on = identical(match.arg(action), "on"), ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment