Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1c9ba8e
some sensors working in homeassistant
meichthys Jan 8, 2020
3296582
bring up to date
meichthys Feb 9, 2020
0a5a760
add codeowner
meichthys Feb 9, 2020
e317646
update requirements
meichthys Feb 9, 2020
42f0f30
overhaul data imports from api & sensor discovery
meichthys Feb 10, 2020
84655ba
remove print statement
meichthys Feb 10, 2020
be097bc
delete requirements_test_all
meichthys Feb 10, 2020
1f59703
add requrements_test_all.txt
meichthys Feb 10, 2020
b8d43f1
Update homeassistant/components/nextcloud/sensor.py
meichthys Feb 10, 2020
c75af07
Update homeassistant/components/nextcloud/sensor.py
meichthys Feb 10, 2020
70dddc2
describe recursive function
meichthys Feb 10, 2020
4211d67
Merge branch 'dev' of https://github.com/meichthys/home-assistant int…
meichthys Feb 10, 2020
eee5f2b
clarify that dict is returned
meichthys Feb 10, 2020
62e851b
remove requirements from requirements_test_all
meichthys Feb 11, 2020
bb94620
Merge remote-tracking branch 'upstream/dev' into dev
meichthys Mar 1, 2020
ba1f48e
improve and simplify sensor naming
meichthys Mar 2, 2020
2c94c73
add basic tests
meichthys Mar 2, 2020
7990dad
restore pre-commit config
meichthys Mar 3, 2020
e802364
update requirements_test_all
meichthys Mar 4, 2020
6a424fa
remove codespell requirement
meichthys Mar 4, 2020
774c5f2
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
meichthys Mar 4, 2020
fc6efb7
update pre-commit-config
meichthys Mar 4, 2020
bc6ebf7
add-back codespell
meichthys Mar 4, 2020
c895e3f
rename class variables as suggested by @springstan
meichthys Mar 10, 2020
69bb5cb
add dev branch to no-commit-to-branch git hook
meichthys Mar 10, 2020
1b4d62a
move config logic to __init__.py
meichthys Mar 11, 2020
c1a5566
restore .pre-commit-config.yaml
meichthys Mar 11, 2020
7f44aba
remove tests
meichthys Mar 11, 2020
d625e72
Merge branch 'dev' of https://github.com/meichthys/home-assistant int…
meichthys Mar 11, 2020
1c25772
remove nextcloud test requirement
meichthys Mar 11, 2020
1a33453
remove debugging code
meichthys Mar 11, 2020
084fd2c
implement binary sensors
meichthys Mar 17, 2020
47c5603
Merge branch 'dev' of https://github.com/meichthys/home-assistant int…
meichthys Mar 17, 2020
79fa369
restore .pre-commit-config.yaml
meichthys Mar 17, 2020
19a33cd
bump dependency version
meichthys Mar 22, 2020
312858a
bump requirements files
meichthys Mar 22, 2020
9b19cef
bump nextcloud reqirement to latest
meichthys Mar 22, 2020
80329d6
update possible exceptions, use fstrings
meichthys Mar 22, 2020
07cd489
add list of sensors & fix inconsistency in get_data_points
meichthys Mar 22, 2020
3136c61
use domain for config
meichthys Mar 23, 2020
f6222ce
fix guard clause
meichthys Mar 23, 2020
d3705a2
repair pre-commit-config
meichthys Mar 23, 2020
57d097c
Remove period from logging
meichthys Mar 23, 2020
7aa0e03
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
meichthys Mar 24, 2020
f891393
include url in unique_id
meichthys Mar 24, 2020
261f907
update requirements_all.txt
meichthys Mar 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ omit =
homeassistant/components/netgear_lte/*
homeassistant/components/netio/switch.py
homeassistant/components/neurio_energy/sensor.py
homeassistant/components/nextcloud/*
homeassistant/components/nfandroidtv/notify.py
homeassistant/components/niko_home_control/light.py
homeassistant/components/nilu/air_quality.py
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ homeassistant/components/netatmo/* @cgtobi
homeassistant/components/netdata/* @fabaff
homeassistant/components/nexia/* @ryannazaretian @bdraco
homeassistant/components/nextbus/* @vividboarder
homeassistant/components/nextcloud/* @meichthys
homeassistant/components/nilu/* @hfurubotten
homeassistant/components/nissan_leaf/* @filcole
homeassistant/components/nmbs/* @thibmaek
Expand Down
147 changes: 147 additions & 0 deletions homeassistant/components/nextcloud/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""The Nextcloud integration."""
from datetime import timedelta
import logging

from nextcloudmonitor import NextcloudMonitor, NextcloudMonitorError
import voluptuous as vol

from homeassistant.const import (
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_URL,
CONF_USERNAME,
)
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.event import track_time_interval

_LOGGER = logging.getLogger(__name__)

DOMAIN = "nextcloud"
NEXTCLOUD_COMPONENTS = ("sensor", "binary_sensor")
SCAN_INTERVAL = timedelta(seconds=60)

# Validate user configuration
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_URL): cv.url,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
}
)
},
extra=vol.ALLOW_EXTRA,
)
BINARY_SENSORS = (
"nextcloud_system_enable_avatars",
"nextcloud_system_enable_previews",
"nextcloud_system_filelocking.enabled",
"nextcloud_system_debug",
)

SENSORS = (
"nextcloud_system_version",
"nextcloud_system_theme",
"nextcloud_system_memcache.local",
"nextcloud_system_memcache.distributed",
"nextcloud_system_memcache.locking",
"nextcloud_system_freespace",
"nextcloud_system_cpuload",
"nextcloud_system_mem_total",
"nextcloud_system_mem_free",
"nextcloud_system_swap_total",
"nextcloud_system_swap_free",
"nextcloud_system_apps_num_installed",
"nextcloud_system_apps_num_updates_available",
"nextcloud_system_apps_app_updates_calendar",
"nextcloud_system_apps_app_updates_contacts",
"nextcloud_system_apps_app_updates_tasks",
"nextcloud_system_apps_app_updates_twofactor_totp",
"nextcloud_storage_num_users",
"nextcloud_storage_num_files",
"nextcloud_storage_num_storages",
"nextcloud_storage_num_storages_local",
"nextcloud_storage_num_storage_home",
"nextcloud_storage_num_storages_other",
"nextcloud_shares_num_shares",
"nextcloud_shares_num_shares_user",
"nextcloud_shares_num_shares_groups",
"nextcloud_shares_num_shares_link",
"nextcloud_shares_num_shares_mail",
"nextcloud_shares_num_shares_room",
"nextcloud_shares_num_shares_link_no_password",
"nextcloud_shares_num_fed_shares_sent",
"nextcloud_shares_num_fed_shares_received",
"nextcloud_shares_permissions_3_1",
"nextcloud_server_webserver",
"nextcloud_server_php_version",
"nextcloud_server_php_memory_limit",
"nextcloud_server_php_max_execution_time",
"nextcloud_server_php_upload_max_filesize",
"nextcloud_database_type",
"nextcloud_database_version",
"nextcloud_database_version",
"nextcloud_activeusers_last5minutes",
"nextcloud_activeusers_last1hour",
"nextcloud_activeusers_last24hours",
)


def setup(hass, config):
"""Set up the Nextcloud integration."""
# Fetch Nextcloud Monitor api data
conf = config[DOMAIN]

try:
ncm = NextcloudMonitor(conf[CONF_URL], conf[CONF_USERNAME], conf[CONF_PASSWORD])
except NextcloudMonitorError:
_LOGGER.error("Nextcloud setup failed - Check configuration")

hass.data[DOMAIN] = get_data_points(ncm.data)
hass.data[DOMAIN]["instance"] = conf[CONF_URL]

def nextcloud_update(event_time):
"""Update data from nextcloud api."""
try:
ncm.update()
except NextcloudMonitorError:
_LOGGER.error("Nextcloud update failed")
return False

hass.data[DOMAIN] = get_data_points(ncm.data)

# Update sensors on time interval
track_time_interval(hass, nextcloud_update, conf[CONF_SCAN_INTERVAL])

for component in NEXTCLOUD_COMPONENTS:
discovery.load_platform(hass, component, DOMAIN, {}, config)

return True


# Use recursion to create list of sensors & values based on nextcloud api data
def get_data_points(api_data, key_path="", leaf=False):
"""Use Recursion to discover data-points and values.

Get dictionary of data-points by recursing through dict returned by api until
the dictionary value does not contain another dictionary and use the
resulting path of dictionary keys and resulting value as the name/value
for the data-point.

returns: dictionary of data-point/values
"""
result = {}
for key, value in api_data.items():
if isinstance(value, dict):
if leaf:
key_path = f"{key}_"
if not leaf:
key_path += f"{key}_"
leaf = True
result.update(get_data_points(value, key_path, leaf))
else:
result[f"{DOMAIN}_{key_path}{key}"] = value
leaf = False
return result
52 changes: 52 additions & 0 deletions homeassistant/components/nextcloud/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Summary binary data from Nextcoud."""
import logging

from homeassistant.components.binary_sensor import BinarySensorDevice

from . import BINARY_SENSORS, DOMAIN

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Nextcloud sensors."""
if discovery_info is None:
return
binary_sensors = []
Comment thread
meichthys marked this conversation as resolved.
for name in hass.data[DOMAIN]:
if name in BINARY_SENSORS:
binary_sensors.append(NextcloudBinarySensor(name))
add_entities(binary_sensors, True)


class NextcloudBinarySensor(BinarySensorDevice):
"""Represents a Nextcloud binary sensor."""

def __init__(self, item):
"""Initialize the Nextcloud binary sensor."""
self._name = item
self._is_on = None

@property
def icon(self):
"""Return the icon for this binary sensor."""
return "mdi:cloud"

@property
def name(self):
"""Return the name for this binary sensor."""
return self._name

@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._is_on == "yes"

@property
def unique_id(self):
"""Return the unique ID for this binary sensor."""
return f"{self.hass.data[DOMAIN]['instance']}#{self._name}"

def update(self):
"""Update the binary sensor."""
self._is_on = self.hass.data[DOMAIN][self._name]
12 changes: 12 additions & 0 deletions homeassistant/components/nextcloud/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"domain": "nextcloud",
"name": "Nextcloud",
"documentation": "https://www.home-assistant.io/integrations/nextcloud",
"requirements": [
"nextcloudmonitor==1.1.0"
],
"dependencies": [],
"codeowners": [
"@meichthys"
]
}
52 changes: 52 additions & 0 deletions homeassistant/components/nextcloud/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Summary data from Nextcoud."""
import logging

from homeassistant.helpers.entity import Entity

from . import DOMAIN, SENSORS

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Nextcloud sensors."""
if discovery_info is None:
return
Comment thread
meichthys marked this conversation as resolved.
sensors = []
Comment thread
meichthys marked this conversation as resolved.
for name in hass.data[DOMAIN]:
if name in SENSORS:
sensors.append(NextcloudSensor(name))
add_entities(sensors, True)


class NextcloudSensor(Entity):
"""Represents a Nextcloud sensor."""

def __init__(self, item):
"""Initialize the Nextcloud sensor."""
self._name = item
self._state = None

@property
def icon(self):
"""Return the icon for this sensor."""
return "mdi:cloud"

@property
def name(self):
"""Return the name for this sensor."""
return self._name

@property
def state(self):
"""Return the state for this sensor."""
return self._state
Comment thread
MartinHjelmare marked this conversation as resolved.

@property
def unique_id(self):
"""Return the unique ID for this sensor."""
return f"{self.hass.data[DOMAIN]['instance']}#{self._name}"

def update(self):
"""Update the sensor."""
self._state = self.hass.data[DOMAIN][self._name]
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,9 @@ neurio==0.3.1
# homeassistant.components.nexia
nexia==0.7.1

# homeassistant.components.nextcloud
nextcloudmonitor==1.1.0

# homeassistant.components.niko_home_control
niko-home-control==0.2.1

Expand Down