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
Added support for sensor other than temperature and humidity #23863
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
ATTR_UNIT = 'unit' | ||
ATTR_URL = 'url' | ||
ATTR_VALUE = 'value' | ||
ATTR_SENSOR_LOCATION = 'location' | ||
|
||
CONF_CONTACT = 'contact' | ||
CONF_HUMIDITY = 'humidity' | ||
|
@@ -72,9 +73,10 @@ | |
vol.Inclusive(CONF_ICON_OPEN, CONF_ICONS): cv.url, | ||
}, required=False) | ||
|
||
SENSOR_SCHEMA = vol.Schema( | ||
{vol.In(SENSOR_TYPES): [cv.entity_id]} | ||
) | ||
SENSOR_SCHEMA = vol.Schema({ | ||
vol.In(SENSOR_TYPES): [cv.entity_id], | ||
cv.string: [cv.entity_id] | ||
}) | ||
|
||
CONFIG_SCHEMA = vol.Schema({ | ||
DOMAIN: vol.Schema({ | ||
|
@@ -105,6 +107,27 @@ class APISpaceApiView(HomeAssistantView): | |
url = URL_API_SPACEAPI | ||
name = 'api:spaceapi' | ||
|
||
@staticmethod | ||
def get_sensor_data(hass, spaceapi, sensor): | ||
"""Get data from a sensor.""" | ||
sensor_state = hass.states.get(sensor) | ||
if not sensor_state: | ||
return None | ||
sensor_data = { | ||
ATTR_NAME: sensor_state.name, | ||
ATTR_VALUE: sensor_state.state | ||
} | ||
if ATTR_SENSOR_LOCATION in sensor_state.attributes: | ||
sensor_data[ATTR_LOCATION] = \ | ||
sensor_state.attributes[ATTR_SENSOR_LOCATION] | ||
else: | ||
sensor_data[ATTR_LOCATION] = spaceapi[CONF_SPACE] | ||
# Some sensors don't have a unit of measurement | ||
if ATTR_UNIT_OF_MEASUREMENT in sensor_state.attributes: | ||
sensor_data[ATTR_UNIT] = \ | ||
sensor_state.attributes[ATTR_UNIT_OF_MEASUREMENT] | ||
return sensor_data | ||
|
||
@ha.callback | ||
def get(self, request): | ||
"""Get SpaceAPI data.""" | ||
|
@@ -154,15 +177,7 @@ def get(self, request): | |
for sensor_type in is_sensors: | ||
sensors[sensor_type] = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a duplicate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I'll fix that |
||
for sensor in spaceapi['sensors'][sensor_type]: | ||
sensor_state = hass.states.get(sensor) | ||
unit = sensor_state.attributes[ATTR_UNIT_OF_MEASUREMENT] | ||
value = sensor_state.state | ||
sensor_data = { | ||
ATTR_LOCATION: spaceapi[CONF_SPACE], | ||
ATTR_NAME: sensor_state.name, | ||
ATTR_UNIT: unit, | ||
ATTR_VALUE: value, | ||
} | ||
sensor_data = self.get_sensor_data(hass, spaceapi, sensor) | ||
sensors[sensor_type].append(sensor_data) | ||
data[ATTR_SENSORS] = sensors | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which sensors contains the location in their attributes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know none does by default, but for our hackerspace (that uses the spaceapi component already) I've set the location attribute for the sensors to make clear where these sensors are located in the space.
You can see it in action here: https://spaceapi.reaktor23.org/
And the SpaceAPI Documentation has the location marked as required, so I thought thats a nice way to provide the location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that for some sensors the
location
is required.