-
-
Notifications
You must be signed in to change notification settings - Fork 28.8k
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
Support CO2/PM2.5/Light sensors in HomeKit #13804
Support CO2/PM2.5/Light sensors in HomeKit #13804
Conversation
@@ -92,6 +93,22 @@ def get_accessory(hass, state, aid, config): | |||
return TYPES['HumiditySensor'](hass, state.entity_id, state.name, | |||
aid=aid) | |||
|
|||
entity_id = state.entity_id |
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.
Just use state.entity_id
instead for all calls.
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.
Too many state.entity_id needs more text and wrap many lines, will reduce readability. And, theoretically, state.entity_id
is low performance then entity_id if we access it many times.
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.
At least please change the log statement and TYPES call to match all other calls.
Regarding the string checks: I'm not sure we should add them. My plan is to add device_class support for sensor entities later this week. That should cover it.
density = convert_to_float(new_state.state) | ||
if density is not None: | ||
self.char_density.set_value(density) | ||
self.char_quality.set_value(AirQualitySensor.map_quality(density)) |
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.
Set char value only once. Maybe add this as well: density = AirQualitySensor.map_quality(density)
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.
It's two different chars: density(native state value in sensor) and quality (int 1-5). Or, sorry I don't understand what you mean.
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.
Didn't saw that. It might have been quite late yesterday. Disregard that comment.
_LOGGER.debug('%s: Set to %d', self.entity_id, density) | ||
|
||
@staticmethod | ||
def map_quality(density): |
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.
This method should be a function (outside the class). It would fit in homekit/utils.py
. Maybe density_to_air_quality
is a better name.
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.
done
return 3 | ||
elif density >= 116 and density <= 150: | ||
return 4 | ||
return 5 |
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.
Keep in mind that you should be able to handle float values from convert_to_float
. What happens if density = 35.1
?
You could do something like this (untested):
from collections import OrderedDict
map = OrderedDict(sorted({1: 0, 2: 35, 3: 76, 4: 116, 5: 151}.items(), reverse=True))
def density_to_air_quality(density):
for key, value in map.items():
if density > value:
return key
return 0
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.
density = 35.1
, It's my carelessness. Thank you for correcting it.
About OrderedDict
, I keep the original if
statements, since it's simple and has more theoretic performance in this simple case.
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.
Works as well.
@@ -64,3 +64,16 @@ def temperature_to_homekit(temperature, unit): | |||
def temperature_to_states(temperature, unit): | |||
"""Convert temperature back from Celsius to Home Assistant unit.""" | |||
return round(temp_util.convert(temperature, TEMP_CELSIUS, unit), 1) | |||
|
|||
|
|||
def density_to_air_quality(density): |
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.
Can you add tests for this function test_util.py
?
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.
done
Rebase and reset commits |
* Added tests * changed device_class lux to light
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 have added additional tests and changed device_class: 'lux'
to device_class: 'light'
.
Thanks 🥇 |
Good job, thanks. Actually, I have been used |
Description:
Map sensors to HomeKit AirQuality/CarbonDioxideSensor/SensorLightSensor, based on the following rules :
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5146
Example entry for
configuration.yaml
(if applicable):Checklist:
tox
.If user exposed functionality or configuration variables are added/changed:
If the code does not interact with devices: