-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add Nextcloud Integration #30871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Nextcloud Integration #30871
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 3296582
bring up to date
meichthys 0a5a760
add codeowner
meichthys e317646
update requirements
meichthys 42f0f30
overhaul data imports from api & sensor discovery
meichthys 84655ba
remove print statement
meichthys be097bc
delete requirements_test_all
meichthys 1f59703
add requrements_test_all.txt
meichthys b8d43f1
Update homeassistant/components/nextcloud/sensor.py
meichthys c75af07
Update homeassistant/components/nextcloud/sensor.py
meichthys 70dddc2
describe recursive function
meichthys 4211d67
Merge branch 'dev' of https://github.com/meichthys/home-assistant int…
meichthys eee5f2b
clarify that dict is returned
meichthys 62e851b
remove requirements from requirements_test_all
meichthys bb94620
Merge remote-tracking branch 'upstream/dev' into dev
meichthys ba1f48e
improve and simplify sensor naming
meichthys 2c94c73
add basic tests
meichthys 7990dad
restore pre-commit config
meichthys e802364
update requirements_test_all
meichthys 6a424fa
remove codespell requirement
meichthys 774c5f2
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
meichthys fc6efb7
update pre-commit-config
meichthys bc6ebf7
add-back codespell
meichthys c895e3f
rename class variables as suggested by @springstan
meichthys 69bb5cb
add dev branch to no-commit-to-branch git hook
meichthys 1b4d62a
move config logic to __init__.py
meichthys c1a5566
restore .pre-commit-config.yaml
meichthys 7f44aba
remove tests
meichthys d625e72
Merge branch 'dev' of https://github.com/meichthys/home-assistant int…
meichthys 1c25772
remove nextcloud test requirement
meichthys 1a33453
remove debugging code
meichthys 084fd2c
implement binary sensors
meichthys 47c5603
Merge branch 'dev' of https://github.com/meichthys/home-assistant int…
meichthys 79fa369
restore .pre-commit-config.yaml
meichthys 19a33cd
bump dependency version
meichthys 312858a
bump requirements files
meichthys 9b19cef
bump nextcloud reqirement to latest
meichthys 80329d6
update possible exceptions, use fstrings
meichthys 07cd489
add list of sensors & fix inconsistency in get_data_points
meichthys 3136c61
use domain for config
meichthys f6222ce
fix guard clause
meichthys d3705a2
repair pre-commit-config
meichthys 57d097c
Remove period from logging
meichthys 7aa0e03
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
meichthys f891393
include url in unique_id
meichthys 261f907
update requirements_all.txt
meichthys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] | ||
| 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] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
meichthys marked this conversation as resolved.
|
||
| sensors = [] | ||
|
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 | ||
|
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] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.