Skip to content
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 3 commits into from May 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 27 additions & 12 deletions homeassistant/components/spaceapi/__init__.py
Expand Up @@ -28,6 +28,7 @@
ATTR_UNIT = 'unit'
ATTR_URL = 'url'
ATTR_VALUE = 'value'
ATTR_SENSOR_LOCATION = 'location'

CONF_CONTACT = 'contact'
CONF_HUMIDITY = 'humidity'
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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:
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

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."""
Expand Down Expand Up @@ -154,15 +177,7 @@ def get(self, request):
for sensor_type in is_sensors:
sensors[sensor_type] = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a duplicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Expand Down