This repository has been archived by the owner on Mar 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fab17f3
commit b718105
Showing
4 changed files
with
490 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
# HASS-database-sensor | ||
Custom component for displaying the size of the HA database file, as described in [this feature request](https://community.home-assistant.io/t/database-size-sensor/40303) | ||
Custom component for displaying the size of the HA [database file](https://home-assistant.io/docs/backend/database/), as described in [this feature request](https://community.home-assistant.io/t/database-size-sensor/40303) | ||
|
||
Motivation: There are quite a few posts where people want to know the size of their HA database on disk. This custom component creates a sensor which displays the size (and perhaps other statistics in attributes) of the db file. | ||
## Motivation | ||
There are quite a few posts where people want to know the size of their HA database on disk. This custom component creates a sensor which displays the size in MB of the db file. In future other statistics can be recorded in attributes. | ||
|
||
## Usage | ||
Add the contents of **custom_components/sensors** to your config. Edit the absolute path to your database in the **database.py** file. | ||
Add the following to your config: | ||
|
||
```yaml | ||
sensor: | ||
- platform: database | ||
``` |
This file contains 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,69 @@ | ||
""" | ||
Sensor for checking the status of Hue sensors. | ||
For more details about this platform, please refer to the documentation at | ||
https://home-assistant.io/components/sensor.hue/ | ||
""" | ||
import logging | ||
import os | ||
|
||
from homeassistant.helpers.entity import Entity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
PATH = "/Users/robincole/.homeassistant/home-assistant_v2.db" | ||
|
||
|
||
def setup_platform(hass, config, add_devices, discovery_info=None): | ||
"""Set up the database sensor.""" | ||
db = Database(PATH) | ||
add_devices([db], True) | ||
|
||
|
||
class Database(Entity): | ||
"""Representation of the HA database.""" | ||
ICON = 'mdi:harddisk' | ||
|
||
def __init__(self, path): | ||
"""Initialize the data object.""" | ||
self._path = path # Need to check its a valid path | ||
self._size = None | ||
self._name = "Database_sensor" | ||
self._attributes = {} | ||
self._unit_of_measurement = 'MiB' | ||
self.update() | ||
|
||
def update(self): | ||
"""Get the size of the database.""" | ||
self._size = self.get_db_size(self._path) | ||
|
||
def get_db_size(self, path): | ||
statinfo = os.stat(path) | ||
decimals = 2 | ||
db_size = round(statinfo.st_size/1e6, decimals) | ||
return db_size | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the sensor.""" | ||
return self._name | ||
|
||
@property | ||
def state(self): | ||
"""Return the state of the sensor.""" | ||
return self._size | ||
|
||
@property | ||
def icon(self): | ||
"""Icon to use in the frontend, if any.""" | ||
return self.ICON | ||
|
||
@property | ||
def device_state_attributes(self): | ||
"""Attributes.""" | ||
return self._attributes | ||
|
||
@property | ||
def unit_of_measurement(self): | ||
"""Return the unit of measurement of this entity, if any.""" | ||
return self._unit_of_measurement |
204 changes: 204 additions & 0 deletions
204
development/.ipynb_checkpoints/HASS database sensor development-checkpoint.ipynb
This file contains 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,204 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Develop the database sensor class" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"PATH = \"/Users/robincole/.homeassistant/home-assistant_v2.db\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"statinfo = os.stat(PATH)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"db_size = round(statinfo.st_size/1e6, 1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The database size is 6.8 MB\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(\"The database size is {} MB\".format(db_size))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_db_size(path):\n", | ||
" statinfo = os.stat(path)\n", | ||
" db_size = round(statinfo.st_size/1e6, 1)\n", | ||
" return db_size" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The database size is 6.8 MB\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(\"The database size is {} MB\".format(get_db_size(PATH)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 22, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class Database(object):\n", | ||
" \"\"\"Representation of the HA database.\"\"\"\n", | ||
"\n", | ||
" def __init__(self, path):\n", | ||
" \"\"\"Initialize the data object.\"\"\"\n", | ||
" self._path = path # Need to check its a valid path\n", | ||
" self._size = None\n", | ||
" # self.update()\n", | ||
"\n", | ||
" def update(self):\n", | ||
" \"\"\"Get the size of the database.\"\"\"\n", | ||
" self._size = self.get_db_size(self._path)\n", | ||
" \n", | ||
" def get_db_size(self, path):\n", | ||
" statinfo = os.stat(path)\n", | ||
" db_size = round(statinfo.st_size/1e6, 1)\n", | ||
" return db_size\n", | ||
"\n", | ||
" @property\n", | ||
" def name(self):\n", | ||
" \"\"\"Return the name of the sensor.\"\"\"\n", | ||
" return self._name\n", | ||
"\n", | ||
" @property\n", | ||
" def state(self):\n", | ||
" \"\"\"Return the state of the sensor.\"\"\"\n", | ||
" return self._size\n", | ||
"\n", | ||
" @property\n", | ||
" def icon(self):\n", | ||
" \"\"\"Icon to use in the frontend, if any.\"\"\"\n", | ||
" return self._icon\n", | ||
"\n", | ||
" @property\n", | ||
" def device_state_attributes(self):\n", | ||
" \"\"\"Attributes.\"\"\"\n", | ||
" return self._attributes" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 23, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_db = Database(PATH)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_db.update()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 25, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"6.8" | ||
] | ||
}, | ||
"execution_count": 25, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"my_db.state" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.