2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Juicenet cloud."""
|
2017-06-05 15:39:31 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-21 08:22:13 +00:00
|
|
|
import pyjuicenet
|
2017-06-05 15:39:31 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
2019-10-21 08:22:13 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2017-06-05 15:39:31 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-10-21 08:22:13 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "juicenet"
|
2017-06-05 15:39:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{DOMAIN: vol.Schema({vol.Required(CONF_ACCESS_TOKEN): cv.string})},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2017-06-05 15:39:31 +00:00
|
|
|
|
2019-11-05 13:24:20 +00:00
|
|
|
JUICENET_COMPONENTS = ["sensor", "switch"]
|
|
|
|
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Set up the Juicenet component."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
access_token = config[DOMAIN].get(CONF_ACCESS_TOKEN)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DOMAIN]["api"] = pyjuicenet.Api(access_token)
|
2017-06-05 15:39:31 +00:00
|
|
|
|
2019-11-05 13:24:20 +00:00
|
|
|
for component in JUICENET_COMPONENTS:
|
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
|
|
|
|
2017-06-05 15:39:31 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class JuicenetDevice(Entity):
|
|
|
|
"""Represent a base Juicenet device."""
|
|
|
|
|
|
|
|
def __init__(self, device, sensor_type, hass):
|
|
|
|
"""Initialise the sensor."""
|
|
|
|
self.hass = hass
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device = device
|
2017-06-05 15:39:31 +00:00
|
|
|
self.type = sensor_type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.name()
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update state of the device."""
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device.update_state()
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _manufacturer_device_id(self):
|
|
|
|
"""Return the manufacturer device id."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.id()
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _token(self):
|
|
|
|
"""Return the device API token."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.token()
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2018-03-03 18:23:55 +00:00
|
|
|
"""Return a unique ID."""
|
2020-02-28 11:39:29 +00:00
|
|
|
return f"{self.device.id()}-{self.type}"
|