mirror of
https://github.com/home-assistant/core.git
synced 2025-08-20 19:00:11 +00:00
.github
docs
homeassistant
auth
components
abode
ads
air_quality
alarm_control_panel
alarmdecoder
alert
alexa
ambient_station
amcrest
android_ip_webcam
androidtv
apcupsd
api
apple_tv
aqualogic
arduino
arlo
asterisk_mbox
asuswrt
august
auth
automation
axis
bbb_gpio
binary_sensor
blink
bloomsky
bmw_connected_drive
browser
calendar
camera
canary
cast
cisco_mobility_express
climate
cloud
cloudflare
coinbase
comfoconnect
config
configurator
conversation
counter
cover
cppm_tracker
daikin
danfoss_air
datadog
deconz
default_config
demo
device_sun_light_trigger
device_tracker
dialogflow
digital_ocean
discovery
dominos
doorbird
dovado
downloader
duckdns
dweet
dyson
ebusd
ecoal_boiler
ecobee
ecovacs
edp_redy
egardia
eight_sleep
elkm1
emoncms_history
emulated_hue
emulated_roku
enigma2
enocean
envisalink
esphome
eufy
evohome
fan
fastdotcom
feedreader
ffmpeg
fibaro
folder_watcher
foursquare
freebox
freedns
fritzbox
frontend
gc100
geo_location
geofency
goalfeed
google
google_assistant
google_domains
google_pubsub
googlehome
gpslogger
graphite
greeneye_monitor
group
habitica
hangouts
harmony
hassio
hdmi_cec
history
history_graph
hive
hlk_sw16
homekit
homekit_controller
homematic
homematicip_cloud
homeworks
http
huawei_lte
hue
hydrawise
idteck_prox
ifttt
ihc
image_processing
influxdb
input_boolean
input_datetime
input_number
input_select
input_text
insteon
insteon_local
insteon_plm
intent_script
introduction
ios
iota
iperf3
ipma
isy994
itach
joaoapps_join
juicenet
keyboard
keyboard_remote
kira
knx
konnected
lametric
lcn
lifx
light
lightwave
linode
lirc
litejet
locative
lock
logbook
logentries
logger
logi_circle
lovelace
luftdaten
lupusec
lutron
lutron_caseta
mailbox
mailgun
map
matrix
maxcube
media_extractor
media_player
melissa
meteo_france
microsoft_face
mobile_app
mochad
modbus
mqtt
mqtt_eventstream
mqtt_statestream
mychevy
mycroft
mysensors
mythicbeastsdns
namecheapdns
neato
ness_alarm
nest
netatmo
netgear_lte
nissan_leaf
no_ip
notify
nuheat
nuimo_controller
octoprint
onboarding
opentherm_gw
openuv
.translations
__init__.py
binary_sensor.py
config_flow.py
const.py
sensor.py
services.yaml
strings.json
owlet
owntracks
panel_custom
panel_iframe
persistent_notification
person
pilight
plant
plum_lightpad
point
prometheus
proximity
ps4
python_script
qwikswitch
rachio
rainbird
raincloud
rainmachine
raspihats
recorder
reddit
remember_the_milk
remote
rest_command
rflink
rfxtrx
ring
roku
route53
rpi_gpio
rpi_pfio
rss_feed_template
sabnzbd
satel_integra
scene
script
scsgate
sense
sensor
shell_command
shiftr
shopping_list
simplisafe
sisyphus
skybell
sleepiq
smappee
smartthings
smhi
snips
sonos
spaceapi
spc
speedtestdotnet
spider
splunk
statsd
stream
sun
switch
system_health
system_log
tado
tahoma
telegram_bot
tellduslive
tellstick
tesla
thethingsnetwork
thingspeak
thinkingcleaner
tibber
timer
tof
toon
tplink
tplink_lte
tradfri
transmission
tts
tuya
twilio
unifi
upcloud
updater
upnp
usps
utility_meter
vacuum
velbus
velux
vera
verisure
volvooncall
vultr
w800rf32
wake_on_lan
water_heater
waterfurnace
watson_iot
weather
webhook
weblink
webostv
websocket_api
wemo
wink
wirelesstag
wunderlist
xiaomi_aqara
xiaomi_miio
xs1
zabbix
zeroconf
zha
zigbee
zone
zoneminder
zwave
__init__.py
services.yaml
helpers
scripts
util
__init__.py
__main__.py
bootstrap.py
config.py
config_entries.py
const.py
core.py
data_entry_flow.py
exceptions.py
loader.py
monkey_patch.py
package_constraints.txt
requirements.py
setup.py
script
tests
virtualization
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hound.yml
.ignore
.readthedocs.yml
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
mypy.ini
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini
145 lines
4.8 KiB
Python
145 lines
4.8 KiB
Python
"""Support for OpenUV sensors."""
|
|
import logging
|
|
|
|
from homeassistant.components.openuv import (
|
|
DATA_OPENUV_CLIENT, DATA_UV, DOMAIN, SENSORS, TOPIC_UPDATE,
|
|
TYPE_CURRENT_OZONE_LEVEL, TYPE_CURRENT_UV_INDEX, TYPE_CURRENT_UV_LEVEL,
|
|
TYPE_MAX_UV_INDEX, TYPE_SAFE_EXPOSURE_TIME_1, TYPE_SAFE_EXPOSURE_TIME_2,
|
|
TYPE_SAFE_EXPOSURE_TIME_3, TYPE_SAFE_EXPOSURE_TIME_4,
|
|
TYPE_SAFE_EXPOSURE_TIME_5, TYPE_SAFE_EXPOSURE_TIME_6, OpenUvEntity)
|
|
from homeassistant.core import callback
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
from homeassistant.util.dt import as_local, parse_datetime
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DEPENDENCIES = ['openuv']
|
|
|
|
ATTR_MAX_UV_TIME = 'time'
|
|
|
|
EXPOSURE_TYPE_MAP = {
|
|
TYPE_SAFE_EXPOSURE_TIME_1: 'st1',
|
|
TYPE_SAFE_EXPOSURE_TIME_2: 'st2',
|
|
TYPE_SAFE_EXPOSURE_TIME_3: 'st3',
|
|
TYPE_SAFE_EXPOSURE_TIME_4: 'st4',
|
|
TYPE_SAFE_EXPOSURE_TIME_5: 'st5',
|
|
TYPE_SAFE_EXPOSURE_TIME_6: 'st6'
|
|
}
|
|
|
|
UV_LEVEL_EXTREME = 'Extreme'
|
|
UV_LEVEL_VHIGH = 'Very High'
|
|
UV_LEVEL_HIGH = 'High'
|
|
UV_LEVEL_MODERATE = 'Moderate'
|
|
UV_LEVEL_LOW = 'Low'
|
|
|
|
|
|
async def async_setup_platform(
|
|
hass, config, async_add_entities, discovery_info=None):
|
|
"""Set up an OpenUV sensor based on existing config."""
|
|
pass
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
"""Set up a Nest sensor based on a config entry."""
|
|
openuv = hass.data[DOMAIN][DATA_OPENUV_CLIENT][entry.entry_id]
|
|
|
|
sensors = []
|
|
for sensor_type in openuv.sensor_conditions:
|
|
name, icon, unit = SENSORS[sensor_type]
|
|
sensors.append(
|
|
OpenUvSensor(
|
|
openuv, sensor_type, name, icon, unit, entry.entry_id))
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
class OpenUvSensor(OpenUvEntity):
|
|
"""Define a binary sensor for OpenUV."""
|
|
|
|
def __init__(self, openuv, sensor_type, name, icon, unit, entry_id):
|
|
"""Initialize the sensor."""
|
|
super().__init__(openuv)
|
|
|
|
self._async_unsub_dispatcher_connect = None
|
|
self._entry_id = entry_id
|
|
self._icon = icon
|
|
self._latitude = openuv.client.latitude
|
|
self._longitude = openuv.client.longitude
|
|
self._name = name
|
|
self._sensor_type = sensor_type
|
|
self._state = None
|
|
self._unit = unit
|
|
|
|
@property
|
|
def icon(self):
|
|
"""Return the icon."""
|
|
return self._icon
|
|
|
|
@property
|
|
def should_poll(self):
|
|
"""Disable polling."""
|
|
return False
|
|
|
|
@property
|
|
def state(self):
|
|
"""Return the status of the sensor."""
|
|
return self._state
|
|
|
|
@property
|
|
def unique_id(self) -> str:
|
|
"""Return a unique, HASS-friendly identifier for this entity."""
|
|
return '{0}_{1}_{2}'.format(
|
|
self._latitude, self._longitude, self._sensor_type)
|
|
|
|
@property
|
|
def unit_of_measurement(self):
|
|
"""Return the unit the value is expressed in."""
|
|
return self._unit
|
|
|
|
async def async_added_to_hass(self):
|
|
"""Register callbacks."""
|
|
@callback
|
|
def update():
|
|
"""Update the state."""
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
|
|
self.hass, TOPIC_UPDATE, update)
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
"""Disconnect dispatcher listener when removed."""
|
|
if self._async_unsub_dispatcher_connect:
|
|
self._async_unsub_dispatcher_connect()
|
|
|
|
async def async_update(self):
|
|
"""Update the state."""
|
|
data = self.openuv.data[DATA_UV]['result']
|
|
if self._sensor_type == TYPE_CURRENT_OZONE_LEVEL:
|
|
self._state = data['ozone']
|
|
elif self._sensor_type == TYPE_CURRENT_UV_INDEX:
|
|
self._state = data['uv']
|
|
elif self._sensor_type == TYPE_CURRENT_UV_LEVEL:
|
|
if data['uv'] >= 11:
|
|
self._state = UV_LEVEL_EXTREME
|
|
elif data['uv'] >= 8:
|
|
self._state = UV_LEVEL_VHIGH
|
|
elif data['uv'] >= 6:
|
|
self._state = UV_LEVEL_HIGH
|
|
elif data['uv'] >= 3:
|
|
self._state = UV_LEVEL_MODERATE
|
|
else:
|
|
self._state = UV_LEVEL_LOW
|
|
elif self._sensor_type == TYPE_MAX_UV_INDEX:
|
|
self._state = data['uv_max']
|
|
self._attrs.update({
|
|
ATTR_MAX_UV_TIME: as_local(parse_datetime(data['uv_max_time']))
|
|
})
|
|
elif self._sensor_type in (TYPE_SAFE_EXPOSURE_TIME_1,
|
|
TYPE_SAFE_EXPOSURE_TIME_2,
|
|
TYPE_SAFE_EXPOSURE_TIME_3,
|
|
TYPE_SAFE_EXPOSURE_TIME_4,
|
|
TYPE_SAFE_EXPOSURE_TIME_5,
|
|
TYPE_SAFE_EXPOSURE_TIME_6):
|
|
self._state = data['safe_exposure_time'][EXPOSURE_TYPE_MAP[
|
|
self._sensor_type]]
|