Virtual components for testing Home Assistant systems.
I've added persistent support to binary_sensor
, fan
, light
, lock
,
sensor
, switch
and device_tracker
. The persistent saving of state is
turned on by default. If you do not want this set persistent: False
in the
entity configuration.
Wherever you see /config
in this README it refers to your home-assistant
configuration directory. For me, for example, it's /home/steve/ha
that is
mapped to /config
inside my docker container.
Many thanks to:
-
JetBrains for the excellent PyCharm IDE and providing me with an open source licence to speed up the project development.
Virtual is part of the default HACS store. If you're not interested in development branches this is the easiest way to install.
Add the following to your configuration.yaml
to enable the component:
virtual:
To add multiple components repeat the platform.
switch:
- platform: virtual
name: Switch 1
- platform: virtual
name: Switch 2
By default, the code creates entities with virtual
as part of their name.
Switch 1
in the previous example will give an entity of
switch.virtual_switch_1
. If you don't want the virtual_
prefix add a !
to the device name. For example:
switch:
- platform: virtual
name: !Switch 1
By default, all devices are market as available. As shown below in each domain,
adding initial_availability: false
to configuration can override default and
set as unavailable on HA start. Availability can by set by using
the virtual.set_available
with value true
or false
.
This is fully optional and initial_availability
is not required to be set.
By default, all device states are persistent. You can change this behaviour with
the persistent
configuration option.
If you have set an initial_value
it will only be used if the device state is
not restored. The following switch will always start on.
switch:
- platform: virtual
name: Switch 1
persistent: False
initial_value: on
To add a virtual switch use the following:
switch:
- platform: virtual
name: Switch 1
initial_availability: True
To add a virtual binary_sensor use the following. It supports all standard classes.
binary_sensor:
- platform: virtual
name: 'Binary Sensor 1'
initial_value: 'on'
class: presence
initial_availability: True
Use the virtual.turn_on
, virtual.turn_off
and virtual.toggle
services to
manipulate the binary sensors.
To add a virtual sensor use the following:
- platform: virtual
name: 'Temperature 1'
class: temperature
initial_value: 37
initial_availability: true
unit_of_measurement: 'F'
Use the virtual.set
service to manipulate the sensor value.
Setting unit_of_measurement
can override default unit for selected sensor
class. This is optional ans any string is accepted. List of standard units can
be found here:
Sensor Entity
To add a virtual light use the following:
light:
- platform: virtual
name: 'Light 1'
initial_value: 'on'
support_brightness: true
initial_brightness: 100
support_color: true
initial_color: [0,255]
support_color_temp: true
initial_color_temp: 255
support_white_value: true
initial_white_value: 240
initial_availability: true
Only name
is required.
support_*
; this allows the light to have colour and temperature propertiesinitial_*
; this is to set the initial values.initial_color
is[hue (0-360), saturation (0-100)]
Note; *white_value is deprecated and will be removed in future releases.
To add a virtual lock use the following:
lock:
- platform: virtual
name: Front Door Lock
initial_availability: true
initial_value: locked
locking_time: 5
jamming_test: 5
- Persistent Configuration
initial_availibilty
: optional, defaultTrue
; is device available at start upinitial_value
: optional, defaultlocked
; any other value will result in the lock being unlocked at start up
- Per Run Configuration
name
: required; device namelocking_time
: optional, default0
seconds; any positive value will result in a locking or unlocking phase that lastslocking_time
secondsjamming_test
: optional, default0
tries; any positive value will result in a jamming failure approximately once perjamming_test
tries
To add a virtual fan use the following:
fan:
- platform: virtual
name: Office Fan
speed: True
speed_count: 5
direction: True
oscillate: True
initial_availability: true
Only name
is required. You only need one of speed
or speed_count
.
speed
; ifTrue
then fan can be set to low, medium and high speedsspeed_count
; number of speeds to allow, these will be broken down into percentages. 4 speeds = 25, 50, 75 and 100%.direction
; ifTrue
then fan can run in 2 directionsoscillate
; ifTrue
then fan can be set to oscillate
To add a virtual device tracker use the following:
device_tracker:
- platform: virtual
devices:
- name: virtual_user1
peristent: True
location: home
- name: virtual_user2
peristent: False
location: not_home
Only name
is required.
persistent
: defaultTrue
; ifTrue
then entity location is remembered across restarts otherwise entity always starts atlocation
location
: defaulthome
; this sets the device location when it is created or if the device is notpersistent
Use the device_tracker.see
service to change device locations.
The old style configuration will still work. They will be moved to home on initial creation and their location will survive restarts.
device_tracker:
- platform: virtual
devices:
- virtual_user1
- virtual_user2